SQL training

 

Title description

A summary of the employees table is as follows:

The table creation statement is as follows:

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`));

 

1. Find all the information of the latest employee in employees

select * from employees order by hire_date desc limit 1;

 

2. Find all the information of the third-to-last employee in employees

select * from employees where hire_date = (select distinct(hire_date) from employees order by hire_date desc limit 2,1);

 

Title description

There is a summary of the salary table salaries for all employees as follows:

There is a brief description of the dept_manager table of the leadership of each department as follows:

The table creation statement is as follows:

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 `dept_manager` (
`dept_no` char(4) NOT NULL,
`emp_no` int(11) NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));

 

3. Find current salary details and department number dept_no

Please find the salary details of each department leader and its corresponding department number dept_no, the output results are sorted in ascending order of salaries.emp_no, and please note that the dept_no column in the output result is the last column

select dept_manager.emp_no, salary, from_date, dept_manager.to_date, dept_no from salaries, dept_manager where salaries.emp_no =
dept_manager.emp_no order by dept_manager.emp_no asc;

 

Title description

There is an employee table, and the employees profile is as follows:

There is a department table, and the dept_emp profile is as follows:

 

The table creation statement is as follows:

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`));


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`));

Fourth, find the employee information of all assigned departments

Please find the last_name, first_name and dept_no of all the employees in the assigned department. The employees in the unassigned department will not be displayed.

select last_name, first_name, dept_no from employees, dept_emp where employees.emp_no = dept_emp.emp_no;

Please find the last_name, first_name and dept_no of all employees who have been assigned to the department, including employees who have not been assigned to a specific department.

select last_name, first_name, dept_no from employees left join dept_emp on employees.emp_no = dept_emp.emp_no;

 

Title description

There is a salary table, the salaries are as follows:

 

The table creation statement is as follows:

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`));

 

Please find the employee number emp_no whose salary has changed more than 15 times and the corresponding number of changes t. The output of the above example is as follows:

5. Find the employee number emp_no whose salary has changed more than 15 times and the corresponding number of changes t

select emp_no, count(*) as t from salaries group by emp_no having t > 15;

Please find out the specific salary situation of all employees, and display the same salary only once, and display it in reverse order

select distinct(salary) from salaries order by salary desc;

Please find out all employees who are not department leaders emp_no

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

 

Title description

There is an employee table dept_emp profile as follows:

The first line indicates that the department with employee number 10001 is the d001 department.

 

There is a brief description of the department manager table dept_manager as follows:

The first line indicates that the manager of department d001 is the employee numbered 10002.

 

Get all employees and their corresponding managers. If the employee is a manager, it will not be displayed. The above example is as follows:

Six, get the current manager of all employees

SELECT de.emp_no, dm.emp_no AS manager_no 
FROM dept_emp AS de INNER JOIN dept_manager AS dm
ON de.dept_no = dm.dept_no 
WHERE de.emp_no <> dm.emp_no;

 

Title description

There is an employee table dept_emp profile as follows:

There is a summary of the salary table salaries as follows:

 

Get the relevant information about the highest salary of employees in all departments, give dept_no, emp_no and their corresponding salary, in ascending order of department number, the output of the above example is as follows:

 

 

Seven, get the relevant information about the highest salary of the current employees in each department

select 
    t1.dept_no,t1.emp_no,t1.salary
from 
    (select d.emp_no,d.dept_no,s.salary from dept_emp d join salaries s on d.emp_no=s.emp_no WHERE d.to_date = '9999-01-01' AND s.to_date = '9999-01-01') t1
    join 
    (select d.dept_no,max(salary)as salary from dept_emp d join salaries s on d.emp_no=s.emp_no WHERE d.to_date = '9999-01-01' AND s.to_date = '9999-01-01' group by d.dept_no) t2
on 
    t1.dept_no=t2.dept_no and t1.salary= t2.salary
order by 
    t1.dept_no asc;

 

Please find all the employees in the employees table whose emp_no is odd and last_name is not Mary, and arrange them in reverse order according to hire_date

select * from employees where emp_no % 2 = 1 and last_name != 'Mary' order by hire_date desc;

 

Title description

There is a brief summary of the titles of the employee title table as follows:

 

There is a summary of the salary table salaries as follows:

 

The table creation statement is as follows:

CREATE TABLE titles (
`emp_no` int(11) NOT NULL,
`title` varchar(50) NOT NULL,
`from_date` date NOT NULL,
`to_date` date DEFAULT NULL);

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`));

8. Calculate the average salary avg corresponding to the employee salary corresponding to each title type

Please calculate the average salary avg corresponding to the employee salary corresponding to each title type. The result is given title and average salary avg, and sorted in ascending order of avg, the output of the above example is as follows:

select title, avg(salary) as avg from salaries, titles where titles.emp_no = salaries.emp_no group by titles.title order by avg;

Please get the emp_no of the employee with the second highest salary and its corresponding salary salary

select emp_no, salary from salaries order by salary desc limit 1,1;

 

Title description

There is a profile of employees table employees as follows:

There is a summary of the salary table salaries as follows:

 

Please find the employee number emp_no, salary salary, last_name and first_name of the second highest salary. You cannot use order by to complete . The output of the above example is:

9. Find the information of the employee with the second highest salary, which cannot be completed with order by 

select s.emp_no, s.salary, e.last_name, e.first_name from salaries s join
employees e on s.emp_no = e.emp_no and s.emp_no = (select s1.emp_no from salaries s1
join salaries s2 on s1.salary <= s2.salary group by s1.emp_no having count(1) = 2);

 

Title description

There is a profile of employees table employees as follows:

 

A brief description of the departments table is as follows:

 

There is a brief description of the department employee relationship table dept_emp as follows:

Please find the last_name and first_name of all employees and the corresponding dept_name, including employees who have not been assigned a department. The output of the above example is as follows:

10. Find the last_name and first_name of all employees and the corresponding dept_name, including employees who have no assigned department temporarily

select last_name, first_name, t.dept_name from employees left join (select emp_no, dept_emp.dept_no as dept_no, dept_name from dept_emp, departments where dept_emp.dept_no = departments.dept_no) t on employees.emp_no = t.emp_no;

Title description

There is a profile of employees table employees as follows:

 

There is a summary of the salary table salaries as follows:

 

Please find the salary increase of all employees since joining, give the employee number emp_no and its corresponding salary increase growth, and in ascending order according to the growth, the output of the above example is

(Note: There may be employees with records in the employees table and salaries table, with corresponding employee numbers and salary increase records, but have resigned, the latest to_date!='9999-01-01' in the salaries table of the resigned employees, Such data is not displayed in the search result, the above emp_no is 2 is like this)

11. Find the salary increase of all employees since joining the company

SELECT sCurrent.emp_no, (sCurrent.salary-sStart.salary) AS growth
FROM (SELECT s.emp_no, s.salary FROM employees e INNER JOIN salaries s ON e.emp_no = s.emp_no WHERE s.to_date = '9999-01-01') AS sCurrent
INNER JOIN (SELECT s.emp_no, s.salary FROM employees e INNER JOIN salaries s ON e.emp_no = s.emp_no WHERE s.from_date = e.hire_date) AS sStart
ON sCurrent.emp_no = sStart.emp_no
ORDER BY growth

 

 

 

Guess you like

Origin blog.csdn.net/xiao__jia__jia/article/details/113789478