首页 小组 文章 相册 留言本 用户 搜索 我的社区 在线学堂 商城 购物车 支付钱包

[打卡]子查询————烤盘饭吃到吐+1

2024-05-14 12:09:52
3
147

#单行子查询

#单行子查询操作符:= != <> > >= < <=

#习题:查询工资大于149号员工工资的员工信息

SELECT employee_id,last_name,salary
FROM employees
GROUP BY employee_id
HAVING salary>10500;

SELECT employee_id,last_name,salary
FROM employees
WHERE salary>(
SELECT salary
FROM employees
WHERE employee_id=149
);

#习题:返回job——id与141号员工相同,salar比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
);

/*

子查询编写技巧(步骤)

1 从里往外写

2 从外往里写

*/

#中英互换

#习题:返回公司工资最少的员工的last_name和salary

SELECT last_name,salary
FROM employees
WHERE salary=(
SELECT MIN(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
GROUP BY department_id
HAVING MIN(salary) >(
SELECT MIN(salary)
FROM employees
WHERE department_id=110
);

#习题:查询最低工资大于60号部门最低工资的部门id和其最低工资并且没有空部门

#方式1:
SELECT department_id,MIN(salary)
FROM employees
GROUP BY department_id
HAVING MIN(salary) >(
SELECT MIN(salary)
FROM employees
WHERE department_id=60
)
AND department_id IS NOT NULL;

#方式2:
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=60
)

#非法使用子查询
SELECT employee_id,last_name
FROM employees
WHERE salary =(
SELECT MIN(salary)
FROM employees
GROUP BY department_id
);

评论 (3)
意见反馈