Previous: [SQL Actual Combat (1)] [SQL Actual Combat on Niuke Net]
++++++++++++++++++Start line++++++++++++++++++
Article Directory
1. Topic
1.1 Question 6
Title description
Find out the current (to_date='9999-01-01') specific salary situation of all employees, display the same salary only once, and display it in reverse order
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`));
Output description
select distinct salary from salaries
where to_date='9999-01-01'
order by salary desc;
Note: WHERE statement is before GROUP BY statement, SQL will calculate WHERE statement before grouping. After the HAVING statement is the GROUP BY statement, SQL will calculate the HAVING statement after grouping.
1.2 Question 7
Title description
Get the current (dept_manager.to_date='9999-01-01') manager's current (salaries.to_date='9999-01-01') salary in all departments, and give dept_no,
emp_no and salary (please note that the same person may (There are multiple salary records)
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 `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`));
answer
SELECT d.dept_no, d.emp_no, s.salary
FROM dept_manager AS d, salaries AS s
WHERE d.emp_no = s.emp_no
AND d.to_date = s.to_date
AND d.to_date = '9999-01-01'
1.3 Question 8
Title description
Get all non-manager employees 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`));
answer
select e.emp_no
from employees e left join dept_manager d
on d.emp_no = e.emp_no
where d.dept_no is null
1.4 Question 9
Title description
Get the current (dept_manager.to_date='9999-01-01') manager of all employees. If the employee is a manager, it will not be displayed (that is, if the current manager is itself, the result will not be displayed). The first column of the output result gives the emp_no of the current employee, and the second column gives the emp_no corresponding to its manager.
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`));
answer
select dept_emp.emp_no as emp_no,dept_manager.emp_no as manager_no
from dept_emp, dept_manager
where dept_emp.dept_no=dept_manager.dept_no
and dept_emp.emp_no!=dept_manager.emp_no
and dept_manager.to_date='9999-01-01';
1.5 Question 10
Title description
Get the current (dept_emp.to_date ='9999-01-01') employee's current (salaries.to_date='9999-01-01') related information with the highest salary in all departments, and give dept_no, emp_no and their corresponding 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`));
answer
select a.dept_no, b.emp_no, max(b.salary)
from dept_emp a inner join salaries b on
a.emp_no=b.emp_no
and a.to_date=b.to_date
and a.to_date='9999-01-01'
group by dept_no;
1.6 Question 11
Title description
Obtain from the titles table and group by title. The number of each group is greater than or equal to 2, and the title and the corresponding number t are given.
CREATE
TABLE IF NOT EXISTS "titles" (
`emp_no` int(11) NOT NULL,
`title` varchar(50) NOT NULL,
`from_date` date NOT NULL,
`to_date` date DEFAULT NULL);
answer
SELECT title, COUNT(title) t
FROM titles
GROUP BY title
HAVING t >= 2
1.7 Question 12
Title description
Obtain from the titles table and group by title. The number of each group is greater than or equal to 2, and the title and the corresponding number t are given.
Note that the repeated emp_no is ignored (that is, the repeated title of emp_no is not counted, and the number t corresponding to the title does not increase).
CREATE TABLE IF NOT EXISTS `titles` (
`emp_no` int(11) NOT NULL,
`title` varchar(50) NOT NULL,
`from_date` date NOT NULL,
`to_date` date DEFAULT NULL);
answer
SELECT title, COUNT(DISTINCT emp_no) t
FROM titles
GROUP BY title
HAVING t>=2;
1.8 Question 13
Title description
Find all employees in the employees table whose emp_no is odd and last_name is not Mary (note the case), and arrange them in reverse order according to hire_date (the title cannot use the mod function)
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`));
answer
SELECT * FROM employees
WHERE emp_no % 2 = 1
AND last_name<>'Mary'
ORDER BY hire_date DESC;
1.9 Question 14
Title description
Calculate the current (titles.to_date='9999-01-01') average salary corresponding to the current (salaries.to_date='9999-01-01') salary of the employees corresponding to each title type. The result gives title and average salary avg.
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`));
CREATE TABLE IF NOT EXISTS "titles" (
`emp_no` int(11) NOT NULL,
`title` varchar(50) NOT NULL,
`from_date` date NOT NULL,
`to_date` date DEFAULT NULL);
answer
select title, avg(salary)
from salaries, titles
where salaries.to_date='9999-01-01'
and titles.to_date='9999-01-01'
and salaries.emp_no=titles.emp_no
group by titles.title;
1.10 Question 15
Title description
Get the current (to_date='9999-01-01') emp_no of the employee with the second highest salary and its corresponding salary salary
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`));
answer
SELECT emp_no, salary FROM salaries
WHERE salary = (SELECT salary
FROM salaries WHERE to_date = '9999-01-01'
GROUP BY salary
ORDER BY salary DESC
LIMIT 1,1 )
++++++++++++++++++End line++++++++++++++++++
Next:【SQL combat (3)】【 Niukenet sql actual combat]