查找出工资比Abel高的员工名字和工资
方式一 多次查询
SELECT salary `salary_abel` FROM employees WHERE last_name = 'Abel'; SELECT last_name, salary FROM employees WHERE salary > salary_abel;
方式二 自连接
SELECT e2.last_name, e2.salary FROM employees e1 , employees e2 WHERE e2.salary > e1.salary AND e1.last_name = 'Abel';
方式三 子查询
SELECT last_name,salary FROM employees WHERE salary > ( SELECT salary FROM employees WHERE last_name = 'Abel' );
外查询(或主查询)/ 内查询(或子查询)
子查询 在主查询之前执行
子查询的结果被主查询(外查询)使用
角度一:单行子查询
VS 多行子查询
按照内查询的结果返回一条还是多条记录,将子查询分为 单行子查询
和多行子查询
角度二:相关(或关联)子查询
和 不相关(或非关联)子查询
按照内查询是否被执行多次,将子查询分为 相关(或关联)子查询 和 不相关(或非关联)子查询
比如:相关子查询的需求:查询工资大于本部门
平均工资的员工信息(外查询和内查询具有关联性,内查询执行次数和外查询相关)
不相关子查询的需求:查询工资大于本公司
平均工资的员工信息(外查询和内查询不具备关联性,一次执行内查询即可完成所有外查询)
= != > >= < <=
查询与141号员工 manager_id和department_id相同的其他员工的 employee_id, manager_id, department_id
分析:与141号相同的 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;
方式1和方式2的区别:
方式1的适用范围广,效率略低;方式2的使用范围窄(子查询条件可以合并),效率略高
查询最低工资大于50号部门最低工资的部门id和其最低工资
SELECT department_id, MIN(salary) FROM employees GROUP BY department_id HAVING MIN(salary) > (SELECT MIN(salary) FROM employees WHERE department_id = 50 );
注意
GROUP BY 不能忘记(SELECT中有的,GROUP BY中一定有;反之不必!)
显示员工的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' );
子查询不返回任何行
多行子查询
操作符 | 含义 |
---|---|
IN | 等于列表中的任意一个 |
ANY | 需要和单行比较操作符一起使用,和子查询返回的任意一个值 比较 |
ALL | 需要和单行比较操作符一起使用,和子查询返回的所有 值比较 |
SOME | 实际上是ANY的别名,作用相同,一般常使用ANY |
查询其它job_id中比job_id为’IT_PROG’部门任一工资低的员工的员工号/姓名/job_id及salary
SELECT employee_id, last_name,job_id,salary FROM employees WHERE salary < ANY (SELECT salary FROM employees WHERE job_id = 'IT_PROG') AND job_id <> 'IT_PROG';
查询其它job_id中比job_id为’IT_PROG’部门所有工资都低的员工的员工号/姓名/job_id以及salary
SELECT employee_id, last_name,job_id,salary FROM employees WHERE salary < ALL (SELECT salary FROM employees WHERE job_id = 'IT_PROG') AND job_id <> 'IT_PROG';
查询平均工资最低的部门id
#方式1: SELECT department_id FROM employees GROUP BY department_id HAVING AVG(salary) = ( SELECT MIN(avg_sal) FROM ( SELECT AVG(salary) avg_sal FROM employees GROUP BY department_id ) dept_avg_sal )
#方式2: SELECT department_id FROM employees GROUP BY department_id HAVING AVG(salary) <= ALL ( SELECT AVG(salary) avg_sal FROM employees GROUP BY department_id )
SELECT last_name FROM employees WHERE employee_id NOT IN ( SELECT manager_id FROM employees );
如果子查询的执行依赖于外部查询,通常情况下都是因为子查询中的表用到了外部的表,并进行了条件关联,因此每执行一次外部查询,子查询都要重新计算一次,这样的子查询就称之为关联子查询
。
相关子查询按照一行接一行的顺序执行,主查询的每一行都执行一次子查询。
# 子查询中使用主查询中的列 SELECT column1,column2,... FROM table1 `outer` WHERE column1 operator ( SELECT column1, column2 FROM table2 WHERE expr1 = `outer`.expr2 )
查询员工中工资大于本部门平均工资的员工的last_name,salary和其department_id
#方式1:相关子查询 SELECT last_name, salary, department_id FROM employees outer WHERE salary > ( SELECT AVG(salary) FROM employees WHERE department_id = outer.department_id );
#方式2:在FROM中使用子查询 SELECT last_name, salary, e1.department_id FROM employees e1, (SELECT department_id, AVG(salary) dept_avg_sal FROM employees GROUP BY department_id) e2 WHERE e1.`department_id` = e2.`department_id` AND e2.dept_avg_sal < e1.`salary`;
查询员工的id,salary 按照department_name排序
SELECT employee_id, salary FROM employees e ORDER BY ( SELECT department_name FROM departments d WHERE e.`department_id` = d.`department_id` );
若employees表中employee_id与job_history表中employee_id相同的数目不小于2,输出这些相同id的员工的employee_id,last_name和其job_id
SELECT e.employee_id, last_name, e.job_id FROM employees e WHERE 2 <= ( SELECT COUNT(*) FROM job_history WHERE employee_id = e.employee_id );
关键子查询通常也会和EXISTS操作符一起来使用,用来检查在子查询中是否存在满足条件的行。
如果在子查询中不存在满足条件的行:
如果在子查询中存在满足条件的行:
NOT EXISTS 关键字表示如果不存在某种条件,则返回TRUE,否则返回FALSE
查询公司管理者的 employee_id,last_name,job_id, department_id信息
# 方式一: SELECT employee_id, last_name, job_id, department_id FROM employees e1 WHERE EXISTS ( SELECT * FROM employees e2 WHERE e2.manager_id = e1.employee_id );
# 方式二:自连接 SELECT DISTINCT e1.employee_id, e1.last_name, e1.job_id, e1.department_id FROM employees e1 JOIN employees e2 WHERE e1.employee_id = e2.manager_id;
# 方式三: SELECT DISTINCT e1.employee_id, e1.last_name, e1.job_id, e1.department_id FROM employees e1 WHERE e1.employee_id IN ( SELECT DISTINCT manager_id FROM employees );
查询departments表中,不存在于employees表中的部门的department_id和department_name
SELECT department_id, department_name FROM departments d WHERE NOT EXISTS ( SELECT 'X' FROM employees WHERE department_id = d.department_id );
UPDATE table1 alias1 SET column = ( SELECT expression FROM table2 alias2 WHERE alias1.column = alias2.column );
使用相关子查询依据一个表中的数据更新另一个表的数据
在employees中增加一个department_name字段,数据为员工对应的部门名称
# 1) ALTER TABLE employees ADD(department_name VARCHAR2(14)); # 2) UPDATE employees e SET department_name = ( SELECT department_name FROM departments d WHERE e.department_id = d.department_id );
DELETE FROM table1 alias1 WHERE column operator ( SELECT expression FROM table2 alias2 WHERE alias1.column = alias2.column );
使用相关子查询依据一个表中的数据删除另一个表的数据
DELETE FROM employees e WHERE employee_id IN ( SELECT employee_id FROM emp_history WHERE employee_id = e.employee_id );
谁的工资比Abel高?
# 方式1:自连接 SELECT e2.last_name,e2.salary FROM employees e1,employees e2 WHERE e1.last_name = 'Abel' AND e1.`salary` < e2.`salary`
# 方式2:子查询 SELECT last_name,salary FROM employees WHERE salary > ( SELECT salary FROM employees WHERE last_name = 'Abel' );
问题:以上两种方式有好坏之分吗?
解答:自连接方式好!
题目中可以使用子查询,也可以使用自连接。一般情况建议你使用自连接,因为在许多 DBMS 的处理过程中,对于自连接的处理速度要比子查询快得多。
可以这样理解:子查询实际上是通过未知表进行查询后的条件判断,而自连接是通过已知的自身数据表进行条件判断,因此在大部分 DBMS 中都对自连接处理进行了优化。