# 查询工资大于149号员工工资的员工信息 SELECT employee_id, last_name, salary FROM employees WHERE salary > ( SELECT salary FROM employees WHERE employee_id =149 ); +-------------+-----------+----------+ | employee_id | last_name | salary | +-------------+-----------+----------+ |100| King |24000.00| |101| Kochhar |17000.00| |102| De Haan |17000.00| |108| Greenberg |12000.00|
HAVING中的子查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# 查询最低工资大于110号部门最低工资的部门id和其最低工资 SELECT department_id, MIN(salary) FROM employees GROUPBY department_id HAVINGMIN(salary) > ( SELECTMIN(salary) FROM employees WHERE department_id =110 ); +---------------+-------------+ | department_id |MIN(salary) | +---------------+-------------+ |70|10000.00| |90|17000.00| +---------------+-------------+
CASE中使用子查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# 查询员工id,姓名,lacation # 其中若员工department_id与location_id为1800的department_id相同 # 则lacation为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 ) AS "location" FROM employees; +-------------+-------------+----------+ | employee_id | last_name | location | +-------------+-------------+----------+ |100| King | USA | |101| Kochhar | USA | |102| De Haan | USA |