Database multi-table join query practice

Database multi-table connection query exercise
insert image description here
insert image description here
1. Create database dept, emp
insert image description here
2. Insert data
insert image description here
insert image description here

1. Find the name of the oldest employee in the sales department

mysql> select name,age from emp 
inner join dept on dept.dept1=emp.dept2 
where dept_name='销售' 
order by age desc limit 1;

insert image description here

2. Find the name of the employee with the minimum salary in the financial department

mysql> select name from emp 
where incoming=(select min(incoming) from emp);

insert image description here

3. List the names of the departments whose total income of each department is higher than 9000

mysql> select dept_name as 部门名称,sum(incoming) as 总工资 from emp 
inner join dept on emp.dept2=dept.dept1 
group by dept_name having 总工资>9000;

insert image description here

4. Ask for the salary between 7500 and 8500 yuan, the name and department of the oldest person

mysql> select name from emp 
where incoming in (7500,8500) 
order by age desc limit 1;

insert image description here

5. Find out when the lowest-paid employees in the sales department joined

mysql> select worktime_start from emp 
inner join dept on emp.dept2=dept.dept1 
where dept_name='销售' 
order by incoming limit 1;

insert image description here

6. Names of employees in the financial department whose income exceeds 2,000 yuan

mysql> select name as 姓名 from emp 
inner join dept on emp.dept2=dept.dept1 
where dept_name='财务' and incoming>2000;

insert image description here

7. List the average income of each department and the name of the department

mysql> select dept_name as 部门名称, avg(incoming) as 平均工资 from emp
    -> inner join dept on emp.dept2=dept.dept1
    -> group by dept_name;

.

8. The employee number of the employee who joined the IT technology department

mysql> select sid from emp
    -> inner join dept on emp.dept2=dept.dept1
    -> where dept_name='IT技术';

insert image description here

9. The sum of the income of the financial department;

mysql> select sum(incoming) as 收入总和 from emp
    -> inner join dept on emp.dept2=dept.dept1
    -> where dept_name='财务';

insert image description here

10. First sort by the size of the department number, and then sort the employee information table from early to late according to the entry time

mysql> select * from emp order by dept2 desc,worktime_start;

insert image description here

11. Find out which department has no employees yet;

mysql> select dept_name from emp right join dept on emp.dept2=dept.dept1 where sid is null;

insert image description here

12. List the department numbers and department names of department employees whose income is greater than 7000;

mysql> select dept2 as 部门编号,dept_name as 部门名称 from emp
    -> inner join dept on emp.dept2=dept.dept1
    -> where incoming>7000;

insert image description here

13. List the total employee income and department name of each department;

mysql> select dept_name as 部门名称,sum(incoming) as 总收入 from emp                                            -> inner join dept on emp.dept2=dept.dept1                                                                  -> group by dept_name;

insert image description here

14. List the name and department name of the oldest employee in each department;

mysql> select emp.name as 姓名, dept.dept_name as 部门名称 from dept
    -> inner join (select max(age) as age,dept2 from emp group by dept2) as em on dept.dept1=em.dept2           
    -> inner join emp on dept.dept1=emp.dept2 and em.age=emp.age;

insert image description here

15. Find Li Si's income and department name

mysql> select incoming as 收入,dept_name from emp
    -> inner join dept on emp.dept2=dept.dept1
    -> where name='李四';

insert image description here

16. List the names of employees with the highest income in each department, department name, income, and in descending order of income

mysql> select name as 姓名,incoming as 收入,dept_name as 部门名称 from emp
    -> inner join dept on emp.dept2=dept.dept1                                      -> inner join
    -> (select max(incoming) as max_incom, dept2 from emp group by dept2)
    -> as em
    -> on emp.dept2=em.dept2 and emp.incoming=em.max_incom
    -> order by incoming desc;

insert image description here

17. List the names of departments with more than 1 employee

mysql> select dept_name as 部门名称 from dept
    -> inner join (select count(*) as 员工数,dept2 from emp group by dept2)         -> as de on dept.dept1=de.dept2
    -> where de.员工数>1;

insert image description here

19. Find the name of Zhang San's department

mysql> select dept_name as 部门名称 from emp
    -> inner join dept on emp.dept2=dept.dept1
    -> where name='张三';

insert image description here

Guess you like

Origin blog.csdn.net/weixin_55822200/article/details/131686651