sql8&10&11&12,3+1

简介: sql8&10&11&12,3+1

简说Python,号主老表,Python终身学习者,数据分析爱好者,从18年开始分享Python知识,原创文章227篇,写过Python、SQL、Excel入门文章,也写过Web开发、数据分析文章,老表还总结整理了一份2022Python学习资料和电子书资源,关注后私信回复:2022 即可领取。

SQL8 找出所有员工当前薪水salary情况

image.png我的思路: 首先筛选出题目中两个关键信息:只要薪水这一列、需要去重降序排列;按要求,我们 select salary,利用group by进行去重,order by salary desc降序排列,同时我们用where to_date = '9999-01-01'筛选出了当前还在公司的员工。

我的题解:

select salary
from salaries
where to_date = '9999-01-01'
group by salary
order by salary desc;

涉及知识点:

  • 完整sql执行顺序(每天看一遍,不信记不住):
from -> where -> group by -> having -> select -> order by -> limit
  • group by单列分组 == 去重
  • order by 列名 desc  降序排序
  • distinct也可以去重复,但是效率相比group by会低一些(数据量大时)

提交结果:

image.png

SQL10 获取所有非manager的员工emp_no

image.pngimage.png

我的思路: 在员工表employees中取出员工编号不在部门领导表dept_manager中的员工编号即可。

我的题解:

select emp_no
from employees
where emp_no not in (select emp_no
                 from dept_manager
                 )
;

涉及知识点:

  • 完整sql执行顺序(每天看一遍,不信记不住):
from -> where -> group by -> having -> select -> order by -> limit
  • where筛选+子查询
  • not in / in 可以判断一个值在不在一个集合中

提交结果:

image.png

其他题解:也是筛选思路,先将dept_manager和employees连接起来,然后筛选出部门编号为空的员工编号即可,利用group by去重。

select b.emp_no
from dept_manager as a 
right join employees as b
on a.emp_no = b.emp_no
where a.dept_no is null
group by b.emp_no
;

注意: != 和=用来判断具体的值,而NULL需要用is或者is not判断

SQL11 获取所有员工当前的manager

image.pngimage.png

我的思路: 筛选思路,先将dept_emp和dept_manager通过dept_no连接起来,并要求emp_no不相等(去除管理者),然后筛选出dept_manager中员工编号emp_no不为空对应的两表中的员工编号即可。

我的题解:

select a.emp_no, b.emp_no
from dept_emp as a
left join dept_manager as b
on a.dept_no = b.dept_no
and a.emp_no != b.emp_no
where b.emp_no is not null 
;

涉及知识点:

  • 完整sql执行顺序(每天看一遍,不信记不住):
from -> where -> group by -> having -> select -> order by -> limit
  • join 连接表

提交结果:

image.png其他题解:同样思路,可以直接join两表。

select a.emp_no, b.emp_no
from dept_emp as a
join dept_manager as b
on a.dept_no = b.dept_no
and a.emp_no != b.emp_no;

SQL12 获取每个部门中当前员工薪水最高的相关信息(困难)

image.pngimage.png

我的思路: 这里稍微有些复杂,首先需要取出的是当前每个部门中薪水最高的员工部门编号、员工编号、员工薪水,所以我们需要将dept_emp、salaries两表连接,限制to_date='9999-01-01'表示当前员工,然后where进行筛选最大薪水,这里用了一个字查询去找每个部门的最高薪水(其实就是从连接表中分组筛选出每组的最大薪水)。

我的题解:

select dept_no, emp_no, salary
from (
select a.dept_no, a.emp_no, b.salary
from dept_emp as a
join salaries as b
on a.emp_no = b.emp_no
and a.to_date='9999-01-01'
and b.to_date='9999-01-01') as a2 
where a2.salary = (
select max(b.salary)
from dept_emp as a
join salaries as b
on a.emp_no = b.emp_no
and a.to_date='9999-01-01'
and b.to_date='9999-01-01'
where a2.dept_no = a.dept_no
group by a.dept_no
)
order by dept_no
;

涉及知识点:

  • 完整sql执行顺序(每天看一遍,不信记不住):
from -> where -> group by -> having -> select -> order by -> limit
  • 子查询
  • max聚合函数
  • group by 分组,其中非group by 的列在select里出现就必须是在聚合函数里。

提交结果:

image.png

题目地址:https://www.nowcoder.com/ta/sql

相关文章
|
4月前
|
SQL 存储 关系型数据库
什么是SQL?
什么是SQL?
24 0
|
3月前
|
SQL 数据库 索引
SQL常用知识
SQL常用知识
|
4月前
|
SQL 数据库 索引
八、SQL-Limite
八、SQL-Limite
24 0
|
SQL 存储 数据库
SQL 能做什么?
SQL 能做什么?
86 0
|
SQL Oracle 关系型数据库
SQL必知必会(三)
作用是从一个或多个表中检索信息
|
存储 SQL 数据库
SQL必知必会(二)
表中的数据都是按行来存储的,所保存的每个记录存储在自己的行内。如果将表想象为网格,网格中垂直的列为表列,水平行为表行。
|
SQL
SQL日常
SQL日常
98 0
|
SQL 存储 监控
xttdbopen.sql
connect / as sysdba; alter database mount;alter database open; exit
735 0