「牛客网SQL实战二刷」是个系列学习笔记博文,每天解析6道SQL题目~ 今天是第7-12 题!该系列的其他博文,可在「我的博客」 中查看~
每篇笔记的格式大致为,三大板块:
「往期回顾」❤️
《牛客网SQL实战二刷 | 完整解析 – 目录索引》
上今天的大纲!
题号 | 知识点 |
---|---|
7 | COUNT()函数、GROUP BY 、HAVING子句 |
8 | DISTINCT(GROUP BY去重的用法)、ORDER BY |
9 | INNER JOIN/ 并列查询 |
10 | LEFT JOIN、NOT IN、 IS NULL |
11 | “不等于”的用法 |
12 | MAX()、GROUP BY |
查找薪水涨幅超过15次的员工号emp_no以及其对应的涨幅次数t
CREATE TABLEsalaries
(
emp_no
int(11) NOT NULL,
salary
int(11) NOT NULL,
from_date
date NOT NULL,
to_date
date NOT NULL,
PRIMARY KEY (emp_no
,from_date
));
emp_no | t |
---|---|
10001 | 17 |
10004 | 16 |
10009 | 18 |
SELECT emp_no, COUNT(emp_no) AS t
FROM salaries
GROUP BY emp_no
HAVING t > 15
找出所有员工当前(to_date=‘9999-01-01’)具体的薪水salary情况,对于相同的薪水只显示一次,并按照逆序显示
CREATE TABLEsalaries
(
emp_no
int(11) NOT NULL,
salary
int(11) NOT NULL,
from_date
date NOT NULL,
to_date
date NOT NULL,
PRIMARY KEY (emp_no
,from_date
))
salary |
---|
94692 |
94409 |
88958 |
88070 |
74057 |
72527 |
59755 |
43311 |
25828 |
SELECT DISTINCT salary FROM salaries
WHERE to_date = '9999-01-01'
ORDER BY salary DESC
SELECT salary FROM salaries
WHERE to_date = '9999-01-01'
GROUP BY salary
ORDER BY salary DESC
来源:https://www.nowcoder.com/questionTerminal/ae51e6d057c94f6d891735a48d1c2397
获取所有部门当前manager的当前薪水情况,给出dept_no, emp_no以及salary,当前表示to_date=‘9999-01-01’
CREATE TABLEdept_manager
(
dept_no
char(4) NOT NULL,
emp_no
int(11) NOT NULL,
from_date
date NOT NULL,
to_date
date NOT NULL,
PRIMARY KEY (emp_no
,dept_no
));
CREATE TABLEsalaries
(
emp_no
int(11) NOT NULL,
salary
int(11) NOT NULL,
from_date
date NOT NULL,
to_date
date NOT NULL,
PRIMARY KEY (emp_no
,from_date
));
dept_no | emp_no | salary |
---|---|---|
d001 | 10002 | 72527 |
d004 | 10004 | 74057 |
d003 | 10005 | 94692 |
d002 | 10006 | 43311 |
d006 | 10010 | 94409 |
SELECT dm.dept_no, dm.emp_no, s.salary
FROM dept_manager AS dm INNER JOIN salaries AS s
ON dm.emp_no = s.emp_no AND dm.to_date = '9999-01-01' AND s.to_date = '9999-01-01'
SELECT dm.dept_no, dm.emp_no, s.salary
FROM dept_manager AS dm, salaries AS s
WHERE dm.emp_no = s.emp_no AND dm.to_date = '9999-01-01' AND s.to_date = '9999-01-01'
获取所有非manager的员工emp_no
CREATE TABLEdept_manager
(
dept_no
char(4) NOT NULL,
emp_no
int(11) NOT NULL,
from_date
date NOT NULL,
to_date
date NOT NULL,
PRIMARY KEY (emp_no
,dept_no
));
CREATE TABLEemployees
(
emp_no
int(11) NOT NULL,
birth_date
date NOT NULL,
first_name
varchar(14) NOT NULL,
last_name
varchar(16) NOT NULL,
gender
char(1) NOT NULL,
hire_date
date NOT NULL,
PRIMARY KEY (emp_no
));
emp_no |
---|
10001 |
10003 |
10007 |
10008 |
10009 |
10011 |
SELECT e.emp_no
FROM employees AS e LEFT JOIN dept_manager AS dm ON e.emp_no = dm.emp_no
WHERE dm.dept_no IS NULL
注意⚠️,是 IS NULL, 不能用 = NULL,详见参考。
SELECT emp_no FROM employees
WHERE emp_no NOT IN (SELECT emp_no FROM dept_manager)
如果对LEFT JOIN掌握不足,可以参考题5,可跳转**《牛客网SQL实战二刷 | Day1》**查看,这道题是在T5的基础上,又复杂一些,筛选出为NULL的记录。
来源:https://blog.csdn.net/weixin_44915703/article/details/91348696)
关于 IS NULL 和 = NULL
1)“ = NULL ” 是错误的语法!
2) SQL中,使用 is NULL 或 is not NULL判断字段是否为空
来源:https://blog.csdn.net/qq_16234613/article/details/65627166
获取所有员工当前的manager,如果当前的manager是自己的话结果不显示,当前表示to_date=‘9999-01-01’。
结果第一列给出当前员工的emp_no,第二列给出其manager对应的manager_no。
CREATE TABLEdept_emp
(
emp_no
int(11) NOT NULL,
dept_no
char(4) NOT NULL,
from_date
date NOT NULL,
to_date
date NOT NULL,
PRIMARY KEY (emp_no
,dept_no
));
CREATE TABLEdept_manager
(
dept_no
char(4) NOT NULL,
emp_no
int(11) NOT NULL,
from_date
date NOT NULL,
to_date
date NOT NULL,
PRIMARY KEY (emp_no
,dept_no
));
emp_no | manager_no |
---|---|
10001 | 10002 |
10003 | 10004 |
10009 | 10010 |
SELECT de.emp_no, dm.emp_no AS manager_no
FROM dept_emp AS de INNER JOIN dept_manager AS dm ON dm.dept_no = de.dept_no
WHERE dm.to_date = '9999-01-01' AND de.to_date = '9999-01-01' AND de.emp_no <> dm.emp_no
获取所有部门中当前员工薪水最高的相关信息,给出dept_no, emp_no以及其对应的salary
CREATE TABLEdept_emp
(
emp_no
int(11) NOT NULL,
dept_no
char(4) NOT NULL,
from_date
date NOT NULL,
to_date
date NOT NULL,
PRIMARY KEY (emp_no
,dept_no
));
CREATE TABLEsalaries
(
emp_no
int(11) NOT NULL,
salary
int(11) NOT NULL,
from_date
date NOT NULL,
to_date
date NOT NULL,
PRIMARY KEY (emp_no
,from_date
));
dept_no | emp_no | salary |
---|---|---|
d001 | 10001 | 88958 |
d002 | 10006 | 43311 |
d003 | 10005 | 94692 |
d004 | 10004 | 74057 |
d005 | 10007 | 88070 |
d006 | 10009 | 95409 |
SELECT de.dept_no, de.emp_no, MAX(salary)
FROM dept_emp AS de INNER JOIN salaries AS s ON de.emp_no = s.emp_no
WHERE de.to_date = '9999-01-01' and s.to_date = '9999-01-01'
GROUP BY de.dept_no
总结Day2的知识点~
知识点 | 题号 |
---|---|
HAVING子句 | 7 |
去重 | 8 |
“为空” | 10 |
表连接 | 9、10 |
“不等于”/ “不在某子集” | 10、11 |
GROUP BY | 7、8、12 |
函数 | 7「COUNT()」、12「MAX()」 |
除此之外,特别的收获,GROUP BY作为去重的新功能!