0


SQL高级查询02

** SQL查询语句的下载脚本链接!!!**

【免费】SQL练习资源-具体练习操作可以查看我发布的文章资源-CSDN文库​编辑https://download.csdn.net/download/Z0412_J0103/89908378https://download.csdn.net/download/Z0412_J0103/89908378


目录

1 员工薪水升序排列(从小到大)

SELECT employee_id, first_name, salary

FROM employees

ORDER BY salary ASC;

展示结果:

2 按部门升序,相同部门按工资降序(部门相同时,则按照薪资从大到小排序)

SELECT department_id, salary

FROM employees

ORDER BY department_id ASC, salary DESC;

展示结果:

3 查询出部门信息

SELECT d.department_id,

d.department_name,

d.manager_id,

d.location_id

FROM departments d;

展示结果:

4 计算出每个员工的年薪

SELECT employee_id 员工编号, first_name 姓, salary 薪资, salary * 12 年薪

FROM employees

WHERE salary * 12 > 200000

ORDER BY 年薪 DESC;

展示结果:

5 查询全体员工的平均薪资

SELECT AVG(salary) 平均薪资, first_name

FROM employees;

-- 可以使用ROUND函数来保留的小数位

SELECT ROUND(AVG(salary), 2) 平均薪资

FROM employees;

展示结果:

6 查询全体员工的薪资总和,平均薪资,最大薪资,最低薪资,人员数量(多行函数可以一起查询)

SELECT SUM(salary) 薪资总和,

AVG(salary) 平均薪资,

MAX(salary) 最大薪资,

MIN(salary) 最低薪资,

COUNT(salary) 人员数量

FROM employees;

展示结果:

7 查询commission_pct字段非null值的总行数(多行函数会忽略null值)

SELECT COUNT(commission_pct) 非NULL的行数,

COUNT(*) 整表的行数

FROM employees;

展示结果:

8 共有多少个工作岗位(可以用distinct去除重复)

SELECT COUNT(DISTINCT job_id)

FROM employees;

展示结果:

9 50部门的人数

SELECT COUNT(*)

FROM employees

WHERE department_id = 50;

展示结果:

10 每个部门的人数(分组字段,可以和多行函数一起查询)

SELECT department_id, COUNT(*)

FROM employees

WHERE department_id IS NOT NULL

GROUP BY department_id;

展示结果:

11 每个工作岗位的最高工资

SELECT job_id, MAX(salary)

FROM employees

GROUP BY job_id;

展示结果:

12 每个部门中,每个岗位的人数,按人数排序(先根据部门分组,之后对每个部门的人根据岗位分组)

SELECT department_id, job_id, COUNT(*) c

FROM employees

WHERE department_id IS NOT NULL

GROUP BY department_id, job_id

ORDER BY c;

展示结果:

13 只有一个人的部门

-- ①对员工表按照部门分组

SELECT department_id,

COUNT(*) 部门人数

FROM employees

WHERE department_id IS NOT NULL

GROUP BY department_id;

展示结果:

-- ②在筛选分组之后的人数为1的部门

SELECT department_id,

COUNT(*) 部门人数

FROM employees

WHERE department_id IS NOT NULL -- 分组之前筛选

GROUP BY department_id

HAVING 部门人数 = 1;

展示结果:

14 平均工资大于8000的工作岗位

-- ①先按照岗位分组,查询出每个岗位的平均薪资

SELECT job_id, ROUND(AVG(salary), 2) 平均薪资

FROM employees

GROUP BY job_id;

展示结果:

-- ②查询出平均薪资大于8000的岗位

SELECT job_id, ROUND(AVG(salary), 2) 平均薪资

FROM employees

GROUP BY job_id

HAVING 平均薪资 > 8000

ORDER BY 平均薪资 DESC;

展示结果:

15 拿最低工资的所有员工

-- ①最低薪资是多少

SELECT MIN(salary)

FROM employees;

展示结果:

-- ②用查询出来的最低薪资,来作为查询条件,过滤出最低薪资的员工记录

SELECT employee_id, first_name, salary

FROM employees

WHERE salary = ( -- 主查询语句(后执行)

SELECT MIN(salary)

FROM employees -- 子查询语句(先执行)

);

展示结果:

16 工资低于平均工资的员工

-- ①查询出来平均工资

SELECT AVG(salary)

FROM employees;

展示结果:

-- ②以查询出来的平均薪资为条件,过滤出低于平均薪资的员工记录

SELECT employee_id, first_name, salary

FROM employees

WHERE salary < (

SELECT AVG(salary)

FROM employees

)

ORDER BY salary DESC;

展示结果:

17 只有一个人的部门中的员工

-- ①分组查询只有一个人的部门id

SELECT department_id

FROM employees

WHERE department_id IS NOT NULL

GROUP BY department_id

HAVING COUNT(*) = 1;

展示结果:

-- ②用于部门id,过滤出员工

SELECT employee_id, first_name, department_id

FROM employees

WHERE department_id IN (

SELECT department_id

FROM employees

WHERE department_id IS NOT NULL

GROUP BY department_id

HAVING COUNT(*) = 1

);

展示结果:

18 每个部门中,拿最低工资的员工

-- ①分组求每个部门的最低薪资

SELECT department_id, MIN(salary)

FROM employees

WHERE department_id IS NOT NULL

GROUP BY department_id;

展示结果:

-- ②用部门id和工资值过滤员工信息

SELECT employee_id, first_name, salary, department_id

FROM employees

WHERE (department_id, salary) IN (

SELECT department_id, MIN(salary)

FROM employees

WHERE department_id IS NOT NULL

GROUP BY department_id

);

展示结果:

上一篇文章:SQL语句高级查询(适用于新手)-CSDN博客https://blog.csdn.net/Z0412_J0103/article/details/143138250?spm=1001.2014.3001.5501**下一篇文章: **SQL高级查询03-CSDN博客https://blog.csdn.net/Z0412_J0103/article/details/143186604

标签: sql 数据库 spring

本文转载自: https://blog.csdn.net/Z0412_J0103/article/details/143173858
版权归原作者 小星袁 所有, 如有侵权,请联系我们删除。

“SQL高级查询02”的评论:

还没有评论