Cattle off network SQL combat two brush | Day2

Disclaimer: This article is a blogger original article, please indicate the source. https://blog.csdn.net/weixin_44915703/article/details/91352022

Cattle off network SQL combat two brush | Day2

"Cattle off network SQL combat two brush" is a series of learning notes Bowen, SQL parsing six topic today is the first day ~ 7-12 questions! The other series of blog posts can be viewed in "my blog" in ~

The format of each note roughly, three major sections:

  • Outline
  • Title (Title Description, ideas, codes, related reference materials / Q)
  • review

Outline today!

First, outline

Question number Knowledge Point
7 COUNT () function, GROUP BY, HAVING clause
8 DISTINCT (GROUP BY to heavy usage), ORDER BY
9 INNER JOIN / parallel query
10 LEFT JOIN、NOT IN、 IS NULL
11 "Not equal to" usage
12 MAX()、GROUP BY

Second, the title

7. Find the number rose rose more than 15 times the salary of the employee number and its corresponding t emp_no

  • Title Description

Find salary rose more than 15 times the number of employees and its corresponding number emp_no rose t
the CREATE TABLE salaries(
emp_noint (11) the NOT NULL,
salaryint (11) the NOT NULL,
from_dateDATE the NOT NULL,
to_dateDATE the NOT NULL,
PRIMARY KEY ( emp_no, from_date));

  • Output Description
emp_no t
10001 17
10004 16
10009 18
  • Thinking
  1. "Rose more than 15 times" with the COUNT () function counts with GROUP BY use, because it is in accordance with the group for each employee, the group statistics;
  2. The output of description, the result of COUNT () is calculated, and designated as T;
  3. In use the having clause gropuby limit t> 15, the return condition is satisfied emp_no.
  • Code
SELECT emp_no, COUNT(emp_no) AS t
FROM salaries
GROUP BY emp_no
HAVING t > 15
  • Q & A - the HAVING VS the WHERE
    in a sql statement can have the where clause and having clause. having and where clause similar, are defined for setting conditions;
    1) the role of the where clause is before grouping query results will not meet the conditions where the line is removed, i.e., prior to filtering data packets, the condition can not contain poly-function, where the use of a specific line condition display;
    effect 2) having filter satisfying the condition clause is a group, i.e., filtering data packets, after the conditions often include poly-function.
    Source: https://www.cnblogs.com/cuihongyu3503319/p/9322457.html

8. identify the current salary salary of all employees

  • topic

Find all current employees (to_date = '9999-01-01') specific salary salary, the same salary for the show only once, and in reverse order to display
the CREATE TABLE salaries(
emp_noint (11) the NOT NULL,
salaryint (11) the NOT NULL ,
from_dateDATE the NOT NULL,
to_dateDATE the NOT NULL,
PRIMARY KEY ( emp_no, from_date))

  • Output Description
salary
94692
94409
88958
88070
74057
72527
59755
43311
25828
  • Thinking
  1. "For the same salary only shown once," to use DISTINCT heavy;
  2. "Displayed in reverse order," sort using ORDER BY.
  • Code
SELECT DISTINCT salary FROM salaries
WHERE to_date = '9999-01-01'
ORDER BY salary DESC
  • Other ideas and solution - the GROUP BY instead of DISTINCT
    'Superyouyo' pointed out, when a large amount of data, distinct efficiency is not high, can solve the problem by repeating group by. It makes my eyes bright ✨ ah!
SELECT salary FROM salaries 
WHERE to_date = '9999-01-01'
GROUP BY salary
ORDER BY salary DESC

Source: https://www.nowcoder.com/questionTerminal/ae51e6d057c94f6d891735a48d1c2397

9. Get all sectors of the current manager of the current salary situation

  • Title Description

Get current salary of all sectors of the current manager, given dept_no, emp_no and the salary, this represents = TO_DATE '9999-01-01'
the CREATE TABLE dept_manager(
dept_nochar (. 4) the NOT NULL,
emp_noint (. 11) the NOT NULL,
from_dateDATE the NOT NULL,
to_datethe NOT NULL DATE,
a PRIMARY KEY ( emp_no, dept_no));
the CREATE TABLE salaries(
emp_noint (. 11) the NOT NULL,
salaryint (. 11) the NOT NULL,
from_dateDATE the NOT NULL,
to_dateDATE the NOT NULL,
a PRIMARY KEY ( emp_no, from_date));

  • Output Description
dept_no emp_no salary
d001 10002 72527
d004 10004 74057
d003 10005 94692
d002 10006 43311
d006 10010 94409
  • Thinking
  1. INNER JOIN to join two tables, or with parallel query;
  2. Two "current" - the current manager, current salary.
  • Code
  1. Method One: INNER JOIN
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'
  1. Method Two: parallel query
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'

10. Get all non-manager employees emp_no

  • Title Description

获取所有非manager的员工emp_no
CREATE TABLE dept_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 TABLE employees (
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));

  • Output Description
emp_no
10001
10003
10007
10008
10009
10011
  • Thinking
  1. 思路一:employees 表 LEFT JOIN dept_emp 表,通过判断 dept_emp对应为空的记录,可以筛选出 「非manager的员工」;
  2. 思路二:在表employees中排除dept_emp表中的记录,用NOT IN字段。
  • 代码
  1. 方法一:IS NULL
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,详见参考。

  1. 方法二: NOT IN
SELECT emp_no FROM employees 
WHERE emp_no NOT IN (SELECT emp_no FROM dept_manager)
  • 参考
  1. 如果对LEFT JOIN掌握不足,可以参考题5,可跳转**《牛客网SQL实战二刷 | Day1》**查看,这道题是在T5的基础上,又复杂一些,筛选出为NULL的记录。
    来源:https://blog.csdn.net/weixin_44915703/article/details/91348696)

  2. 关于几种JOINS
    在这里插入图片描述

  3. 关于 IS NULL 和 = NULL
    1)“ = NULL ” 是错误的语法!
    2) SQL中,使用 is NULL 或 is not NULL判断字段是否为空
    来源:https://blog.csdn.net/qq_16234613/article/details/65627166

11. 获取所有员工当前的manager

  • 题目

获取所有员工当前的manager,如果当前的manager是自己的话结果不显示,当前表示to_date=‘9999-01-01’。
结果第一列给出当前员工的emp_no,第二列给出其manager对应的manager_no。
CREATE TABLE dept_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 TABLE dept_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
  • 思路
  1. INNER JOIN 通过关键字 dept_no连接两张表;
  2. 「如果当前的manager是自己的话结果不显示」,即dept_manager表的emp_no 与 dept_emp表的emp_no相同的记录,应去掉。
  • 代码
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

12. 获取所有部门中当前员工薪水最高的相关信息

  • 题目

获取所有部门中当前员工薪水最高的相关信息,给出dept_no, emp_no以及其对应的salary
CREATE TABLE dept_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 TABLE salaries (
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
  • 思路
  1. 「薪水最高」,即MAX(salary);
  2. 根据输出描述,可见需按照dept_no 分组,即group by dept_no;
  3. 两张表通过INNER JOIN 连接。
  • 代码
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作为去重的新功能!

Guess you like

Origin blog.csdn.net/weixin_44915703/article/details/91352022