#单行子查询
#单行操作符: = != > >= < <=
#子查询的编写技巧(步骤):1、从里往外写 2、从外往里写
#查询工资大于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 = (
SELECT MIN(salary)
FROM employees
);
#查询最低工资大于50号部门最低工资的部门id和其最低工资
SELECT department_id,MIN(salary)
FROM employees
WHERE department_id IS NOT NULL
GROUP BY department_id
HAVING MIN(salary) > (
SELECT MIN(salary)
FROM employees
WHERE department_id = 50
);
#显示员工的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'
);
单行子查询
最新推荐文章于 2024-08-25 15:00:09 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)