谁的工资比Abel的高?
方式1:
SELECT salary
FROM employees
WHERE last_name ='Abel';SELECT last_name,salary
FROM employees
WHERE salary >11000;
方式2:自连接
SELECT e2.last_name,e2.salary
FROM employees e1,employees e2
WHERE e2.`salary`> e1.`salary`#多表的连接条件AND e1.last_name ='Abel';
方式3:子查询
SELECT last_name,salary
FROM employees
WHERE salary >(SELECT salary
FROM employees
WHERE last_name ='Abel');
角度1:从内查询返回的结果的条目数
单行子查询 vs 多行子查询
角度2:内查询是否被执行多次
相关子查询 vs 不相关子查询
比如:相关子查询的需求:查询工资大于本部门平均工资的员工信息。
不相关子查询的需求:查询工资大于本公司平均工资的员工信息。
单行子查询
查询工资大于149号员工工资的员工的信息
SELECT employee_id,last_name,salary
FROM employees
WHERE salary >(SELECT salary
FROM employees
WHERE employee_id =149);
返回job_id与141号员工相同,salary比143号员工多的员工姓名,job_id和工资
SELECT last_name,job_id,salary
FROM employees
WHERE job_id =(SELECT job_id
FROM employees
WHERE employee_id =141)AND salary >(SELECT salary
FROM employees
WHERE employee_id =143);
返回公司工资最少的员工的last_name,job_id和salary
SELECT last_name,job_id,salary
FROM employees
WHERE salary =(SELECTMIN(salary)FROM employees
);
查询与141号员工的manager_id和department_id相同的其他员工的employee_id,manager_id,department_id。
方式1:
SELECT employee_id,manager_id,department_id
FROM employees
WHERE manager_id =(SELECT manager_id
FROM employees
WHERE employee_id =141)AND department_id =(SELECT department_id
FROM employees
WHERE employee_id =141)AND employee_id <>141;
方式2:了解
SELECT employee_id,manager_id,department_id
FROM employees
WHERE(manager_id,department_id)=(SELECT manager_id,department_id
FROM employees
WHERE employee_id =141)AND employee_id <>141;
查询最低工资大于110号部门最低工资的部门id和其最低工资
SELECT department_id,MIN(salary)FROM employees
WHERE department_id ISNOTNULLGROUPBY department_id
HAVINGMIN(salary)>(SELECTMIN(salary)FROM employees
WHERE department_id =110);
显式员工的employee_id,last_name和location。
其中,若员工department_id与location_id为1800的department_id相同,
则location为’Canada’,其余则为’USA’。
SELECT employee_id,last_name,CASE department_id WHEN(SELECT department_id FROM departments WHERE location_id =1800)THEN'Canada'ELSE'USA'END"location"FROM employees;
子查询中的空值问题
SELECT last_name, job_id
FROM employees
WHERE job_id =(SELECT job_id
FROM employees
WHERE last_name ='Haas');
非法使用子查询
错误:Subquery returns more than 1rowSELECT employee_id, last_name
FROM employees
WHERE salary =(SELECTMIN(salary)FROM employees
GROUPBY department_id);
多行子查询
IN:
SELECT employee_id, last_name
FROM employees
WHERE salary IN(SELECTMIN(salary)FROM employees
GROUPBY department_id);
返回其它job_id中比job_id为‘IT_PROG’部门任一工资低的员工的员工号、姓名、job_id 以及salary
SELECT employee_id,last_name,job_id,salary
FROM employees
WHERE job_id <>'IT_PROG'AND salary <ANY(SELECT salary
FROM employees
WHERE job_id ='IT_PROG');
返回其它job_id中比job_id为‘IT_PROG’部门所有工资低的员工的员工号、姓名、job_id 以及salary
SELECT employee_id,last_name,job_id,salary
FROM employees
WHERE job_id <>'IT_PROG'AND salary <ALL(SELECT salary
FROM employees
WHERE job_id ='IT_PROG');
查询平均工资最低的部门id,MySQL中聚合函数是不能嵌套使用的。
方式1:
SELECT department_id
FROM employees
GROUPBY department_id
HAVINGAVG(salary)=(SELECTMIN(avg_sal)FROM(SELECTAVG(salary) avg_sal
FROM employees
GROUPBY department_id
) t_dept_avg_sal
);
方式2:
SELECT department_id
FROM employees
GROUPBY department_id
HAVINGAVG(salary)<=ALL(SELECTAVG(salary) avg_sal
FROM employees
GROUPBY department_id
)
空值问题
SELECT last_name
FROM employees
WHERE employee_id NOTIN(SELECT manager_id
FROM employees
);
相关子查询
回顾:查询员工中工资大于公司平均工资的员工的last_name,salary和其department_id
SELECT last_name,salary,department_id
FROM employees
WHERE salary >(SELECTAVG(salary)FROM employees
);
查询员工中工资大于本部门平均工资的员工的last_name,salary和其department_id
方式1:使用相关子查询
SELECT last_name,salary,department_id
FROM employees e1
WHERE salary >(SELECTAVG(salary)FROM employees e2
WHERE department_id = e1.`department_id`);
方式2:在FROM中声明子查询
SELECT e.last_name,e.salary,e.department_id
FROM employees e,(SELECT department_id,AVG(salary) avg_sal
FROM employees
GROUPBY department_id) t_dept_avg_sal
WHERE e.department_id = t_dept_avg_sal.department_id
AND e.salary > t_dept_avg_sal.avg_sal
查询员工的id,salary,按照department_name 排序
SELECT employee_id,salary
FROM employees e
ORDERBY(SELECT department_name
FROM departments d
WHERE e.`department_id`= d.`department_id`)ASC;
若employees表中employee_id与job_history表中employee_id相同的数目不小于2,
输出这些相同id的员工的employee_id,last_name和其job_id
SELECT*FROM job_history;SELECT employee_id,last_name,job_id
FROM employees e
WHERE2<=(SELECTCOUNT(*)FROM job_history j
WHERE e.`employee_id`= j.`employee_id`)
EXISTS 与 NOT EXISTS关键字
查询公司管理者的employee_id,last_name,job_id,department_id信息
方式1:自连接
SELECTDISTINCT mgr.employee_id,mgr.last_name,mgr.job_id,mgr.department_id
FROM employees emp JOIN employees mgr
ON emp.manager_id = mgr.employee_id;
方式2:子查询
SELECT employee_id,last_name,job_id,department_id
FROM employees
WHERE employee_id IN(SELECTDISTINCT manager_id
FROM employees
);
方式3:使用EXISTSSELECT employee_id,last_name,job_id,department_id
FROM employees e1
WHEREEXISTS(SELECT*FROM employees e2
WHERE e1.`employee_id`= e2.`manager_id`);
查询departments表中,不存在于employees表中的部门的department_id和department_name
方式1:
SELECT d.department_id,d.department_name
FROM employees e RIGHTJOIN departments d
ON e.`department_id`= d.`department_id`WHERE e.`department_id`ISNULL;
方式2:
SELECT department_id,department_name
FROM departments d
WHERENOTEXISTS(SELECT*FROM employees e
WHERE d.`department_id`= e.`department_id`);SELECTCOUNT(*)FROM departments;