"Cattle off network SQL combat two brush" is a series of learning notes Bowen, SQL parsing six topics every day ~ ~ Today is the first title 19-24
The format of each note roughly, three major sections:
- Outline
- Title (Title Description, ideas, codes, related reference materials / Q)
- review
First, outline
Question number | Knowledge Point |
---|---|
19 | LEFT JOIN |
20 | Nested SELECT |
21 | Nested SELECT |
22 | GROUP BY, COUNT() |
23 | Table reuse, DISTINCT, ORDER BY, GROUP BY |
24 | NOT IN |
Second, the title
19. Find all the staff and first_name last_name and corresponding dept_name
- Title Description
Find last_name and first_name and corresponding dept_name all employees, including temporary no employees assigned sector of
the CREATE TABLEdepartments
(
dept_no
char (4) the NOT NULL,
dept_name
VARCHAR (40) the NOT NULL,
PRIMARY KEY (dept_no
));
the CREATE TABLEdept_emp
(
emp_no
int (11) the NOT NULL,
dept_no
char (. 4) the NOT NULL,
from_date
DATE the NOT NULL,
to_date
DATE the NOT NULL,
a PRIMARY KEY (emp_no
,dept_no
));
the CREATE TABLEemployees
(
emp_no
int (. 11) the NOT NULL,
birth_date
DATE the NOT NULL,
first_name
VARCHAR (14) the NOT NULL,
last_name
VARCHAR (16) NULL the NOT,
gender
char (. 1) the NOT NULL,
hire_date
DATE the NOT NULL,
a PRIMARY KEY (emp_no
));
- Output Description
last_name | first_name | dept_name |
---|---|---|
Facello | Georgi | Marketing |
Omission | Omission | Omission |
Lock | Mary | NULL |
- Thinking
- The first table LEFT JOIN employees and dept_emp table, and obtain last_name FIRST_NAME;
- The second table LEFT JOIN departments give dept_name,;
- Because Find all employees, "not including temporary staff distribution sector", so use LEFT JOIN.
- Code
SELECT e.last_name, e.first_name, d.dept_name
FROM employees AS e LEFT JOIN dept_emp AS de ON e.emp_no = de.emp_no
LEFT JOIN departments AS d ON de.dept_no = d.dept_no
20. Find emp_no employee number 10001 from their salary salary increase since the entry value growth
- Title Description
Find emp_no employee number 10001 from their salary salary increase since the entry value Growth
the CREATE TABLEsalaries
(
emp_no
int (11) the NOT NULL,
salary
int (11) the NOT NULL,
from_date
DATE the NOT NULL,
to_date
DATE the NOT NULL,
PRIMARY KEY (emp_no
,from_date
));
- Output Description
growth |
---|
28841 |
-
Ideas
were found emp_no 10001 employees current salary and the entry salary, do difference is the growth -
Code
SELECT (
(SELECT salary FROM salaries WHERE emp_no = 10001 ORDER BY to_date DESC LIMIT 1 ) -
(SELECT salary FROM salaries WHERE emp_no = 10001 ORDER BY from_date ASC LIMIT 1)
)AS growth
21. Find all the staff since the entry salary increase case
- Title Description
Find a salary increase of all employees from entry since, given the employee number emp_no and its corresponding salary increase growth, and in ascending order according to Growth
the CREATE TABLEemployees
(
emp_no
int (11) the NOT NULL,
birth_date
DATE the NOT NULL,
first_name
VARCHAR (14) the NOT NULL,
last_name
VARCHAR (16) the NOT NULL,
gender
char (. 1) the NOT NULL,
hire_date
DATE the NOT NULL,
a PRIMARY KEY (emp_no
));
the CREATE TABLEsalaries
(
emp_no
int (. 11) the NOT NULL,
salary
int (. 11) the NOT NULL,
from_date
DATE the NOT NULL,
to_date
DATE the NOT NULL,
a PRIMARY KEY (emp_no
,from_date
));
- Output Description
emp_no | growth |
---|---|
10011 | 0 |
Omission | Omission |
54496 | 10004 |
-
Thinking
-
Produced two tables, a record current salary, a record entry salary;
-
Emp_no connected with two tables, corresponding to the deviation to make salary growth.
-
Code
SELECT sCurrent.emp_no, (sCurrent.salary - sStart.salary) AS growth
FROM (SELECT e.emp_no, s.salary FROM employees AS e, salaries AS s WHERE e.emp_no = s.emp_no AND s.to_date = '9999-01-01') AS sCurrent,
(SELECT e.emp_no, s.salary FROM employees AS e, salaries AS s WHERE e.emp_no = s.emp_no AND e.hire_date = s.from_date) AS sStart
WHERE sCurrent.emp_no = sStart.emp_no
ORDER BY growth
22. The statistical sum corresponding to the number of employees rose in all sectors
- Title Description
The sum of the number of times corresponding to various departments of employees statistical gains, coding dept_no given sector, the department name and the number dept_name SUM
the CREATE TABLEdepartments
(
dept_no
char (. 4) the NOT NULL,
dept_name
VARCHAR (40) the NOT NULL,
a PRIMARY KEY (dept_no
));
the CREATE TABLEdept_emp
(
emp_no
int ( . 11) the NOT NULL,
dept_no
char (. 4) the NOT NULL,
from_date
DATE the NOT NULL,
to_date
DATE the NOT NULL,
a PRIMARY KEY (emp_no
,dept_no
));
the CREATE TABLEsalaries
(
emp_no
int (. 11) the NOT NULL,
salary
int (. 11) the NOT NULL,
from_date
DATE the NOT NULL,
to_date
DATE the NOT NULL,
a PRIMARY KEY (emp_no
,from_date
));
- Output Description
dept_no | dept_name | sum |
---|---|---|
d001 | Marketing | 24 |
d002 | Finance | 14 |
d003 | Human Resources | 13 |
d004 | Production | 24 |
d005 | Development | 25 |
d006 | Quality Management | 25 |
- Thinking
- First connection table and salaries dept_emp table, and to obtain, emp_no, dept_no;
- Reconnection departments table, to give dept_name,;
- According dept_no packet, () with the count COUNT with GROUP BY.
- Code
SELECT de.dept_no, d.dept_name, COUNT(s.emp_no) AS sum
FROM (dept_emp AS de INNER JOIN salaries AS s ON de.emp_no = s.emp_no)
INNER JOIN departments AS d ON de.dept_no = d.dept_no
GROUP BY de.dept_no
23. The salary of all employees are ranked according to 1-N in accordance with the salary
- Title Description
All current employees (to_date = '9999-01-01') according to the salary paid in accordance with the ranking of 1-N, the same salary in parallel and arranged in ascending order emp_no
the CREATE TABLEsalaries
(
emp_no
int (. 11) the NOT NULL,
salary
int (. 11) the NOT NULL,
from_date
DATE the NOT NULL,
to_date
DATE the NOT NULL,
a PRIMARY KEY (emp_no
,from_date
));
- Output Description
emp_no | salary | rank |
---|---|---|
10005 | 94692 | 1 |
10009 | 94409 | 2 |
10010 | 94409 | 2 |
10001 | 88958 | 3 |
10007 | 88070 | 4 |
10004 | 74057 | 5 |
10002 | 72527 | 6 |
10003 | 43311 | 7 |
10006 | 43311 | 7 |
10011 | 25828 | 8 |
- Thinking
- Re-rank comparison with salaries table;
- It is the second rank in the table equal to the number greater than the number of the current salary, since the ranking parallel, should be used to re DISTINCT;
- According emp_no packet;
- Emp_no salary and arranged in reverse order.
- Code
SELECT s1.emp_no, s1.salary, COUNT(DISTINCT s2.salary) AS rank
FROM salaries AS s1, salaries AS s2
WHERE s1.to_date = '9999-01-01' AND s2.to_date = '9999-01-01' AND s1.salary <= s2.salary
GROUP BY s1.emp_no
ORDER BY s1.salary DESC, s1.emp_no
24. Get the current salaries of all non-manager employees
- Title Description
获取所有非manager员工当前的薪水情况,给出dept_no、emp_no以及salary ,当前表示to_date=‘9999-01-01’
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
));
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
));
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 |
d004 | 10003 | 43311 |
d005 | 10007 | 88070 |
d006 | 10009 | 95409 |
- 思路
- 连接dept_emp表和salaries表得到dept_no,emp_no和salary
- 「所有非manager员工」,因此要排除dept_manager表中的员工emp_no
- 代码
SELECT de.dept_no, de.emp_no, s.salary
FROM dept_emp AS de INNER JOIN salaries AS s ON de.emp_no = s.emp_no AND s.to_date = '9999-01-01'
WHERE de.emp_no NOT IN (SELECT emp_no FROM dept_manager)
三、回顾
知识点 | 题号 |
---|---|
LEFT JOIN | 19 |
ORDER BY | 23 |
表的复用 | 23 |
DISTINCT | 23 |
NOT IN | 24 |
函数 | 22「COUNT()」 |
SELECT 嵌套 | 20, 21 |
GROUP BY | 22, 23 |