标签:mamicode nbsp ima HERE sel rac 操作 weight manager
子查询语法
SELECT select_list FROM table WHERE expr operator (SELECT select_list FROM table);
示例:
SELECT last_name FROM employees WHERE salary > ( SELECT salary FROM employees WHERE last_name = ‘Abel‘ );
注意事项
子查询类型
单行子查询
示例:
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 );
在子查询中使用组函数
SELECT last_name, job_id, salary FROM employees WHERE salary = ( SELECT MIN(salary) FROM employees );
子查询中的 HAVING 子句
SELECT department_id, MIN(salary) FROM employees GROUP BY department_id HAVING MIN(salary) > ( SELECT MIN(salary) FROM employees WHERE department_id = 50 );
非法使用子查询
子查询中的空值问题
SELECT last_name, job_id FROM employees WHERE job_id = ( SELECT job_id FROM employees WHERE last_name = ‘Haas‘ );
多行子查询
在多行子查询中使用 ANY 操作符
示例:返回其它部门中比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‘;
在多行子查询中使用 ALL 操作符
示例:返回其它部门中比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‘;
子查询中的空值问题
SELECT emp.last_name FROM employees emp WHERE emp.employee_id NOT IN ( SELECT mgr.manager_id FROM employees mgr );
标签:mamicode nbsp ima HERE sel rac 操作 weight manager
原文地址:https://www.cnblogs.com/loaderman/p/11738014.html