MySQL two table join query practice

topic

1. Find the name of the oldest employee in the sales department
2. Find the name of the employee with the minimum salary in the financial department
3. List the names of the departments whose total income of each department is higher than 9000
4. Find the salary between 7500 and 8500 yuan, The name and department of the oldest person
5. Find out the entry time of the employee with the lowest income in the sales department
6. The name of the employee in the financial department with an income of more than 2,000 yuan
7. List the average income and department name of each department
8. IT technology department entry
9. The total income of the financial department; 10.
First sort by department number, and then sort the employee information table according to the entry time from early to late.
11. Find out which department has no employees yet;
12. List department employees Department number and department name with income greater than 7000;
13. List the total income of employees in each department and department name;
14. List the name of the oldest employee in each department, department name;
15. Ask for Li Si's income and Department name
16. List the name of the employee with the highest income in each department, department name, income, and in descending order of income 17.
List the name of the department with more than 1 employee in the department
19. Find the name of the department where Zhang San is located

practice questions

Preparation

mysql> create database work3;
mysql> use work3;

mysql> create table dept(
    -> dept1 int,
    -> dept_name varchar(11)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> create table emp(
    -> sid int,
    -> name varchar(11),
    -> age int,
    -> worktime_start date,
    -> incoming int,
    -> dept2 int
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> insert into dept values (101,'财务'),(102,'销售'),(103,'IT技术'),(104,'行政');
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> insert into emp values (1789,'张三',35,'1980/1/1',4000,101),(1674,'李四',32,'1983/4/1',3500,101),(1776,'王五',24,'1990/7/1',2000,101),(1568,'赵六',57,'1970/10/11',7500,102),(1564,'荣七',64,'1963/10/11',8500,102),(1879,'牛八',55,'1971/10/20',7300,103);
Query OK, 6 rows affected, 6 warnings (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 6

mysql> select * from dept;
+-------+-----------+
| dept1 | dept_name |
+-------+-----------+
|   101 | 财务      |
|   102 | 销售      |
|   103 | IT技术    |
|   104 | 行政      |
+-------+-----------+
4 rows in set (0.00 sec)

mysql> select * from emp;
+------+--------+------+----------------+----------+-------+
| sid  | name   | age  | worktime_start | incoming | dept2 |
+------+--------+------+----------------+----------+-------+
| 1789 | 张三   |   35 | 1980-01-01     |     4000 |   101 |
| 1674 | 李四   |   32 | 1983-04-01     |     3500 |   101 |
| 1776 | 王五   |   24 | 1990-07-01     |     2000 |   101 |
| 1568 | 赵六   |   57 | 1970-10-11     |     7500 |   102 |
| 1564 | 荣七   |   64 | 1963-10-11     |     8500 |   102 |
| 1879 | 牛八   |   55 | 1971-10-20     |     7300 |   103 |
+------+--------+------+----------------+----------+-------+
6 rows in set (0.00 sec)

Answer

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

mysql> select name as 姓名,age as 年龄,dept_name from dept inner join emp on dept.dept1=emp.dept2 where dept_name = '销售' order by age desc limit 1;
+--------+--------+-----------+
| 姓名   | 年龄   | dept_name |
+--------+--------+-----------+
| 荣七   |     64 | 销售      |
+--------+--------+-----------+
1 row in set (0.00 sec)

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

mysql> select name as 姓名,age as 年龄,dept_name from dept inner join emp on dept.dept1=emp.dept2 where dept_name = '财务' order by age limit 1;
+--------+--------+-----------+
| 姓名   | 年龄   | dept_name |
+--------+--------+-----------+
| 王五   |     24 | 财务      |
+--------+--------+-----------+
1 row in set (0.00 sec)

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 dept inner join emp on dept.dept1=emp.dept2 group by dept_name having `总收入` > 9000;
+--------+-----------+
| 部门   | 总收入    |
+--------+-----------+
| 财务   |      9500 |
| 销售   |     16000 |
+--------+-----------+
2 rows in set (0.00 sec)

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

mysql> select name as `姓名`,age as `年龄`,dept_name as `部门`,incoming as `工资` from dept inner join emp on dept.dept1=emp.dept2 where incoming between 7500 and 8500;
+--------+--------+--------+--------+
| 姓名   | 年龄   | 部门   | 工资   |
+--------+--------+--------+--------+
| 荣七   |     64 | 销售   |   8500 |
| 赵六   |     57 | 销售   |   7500 |
+--------+--------+--------+--------+
2 rows in set (0.00 sec)

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

mysql> select name as `姓名`,worktime_start as `入职时间`,dept_name as `部门`,incoming as `工资` from dept inner join emp on dept.dept1=emp.dept2 where dept_name = '销售' order by worktime_start limit 1;
+--------+--------------+--------+--------+
| 姓名   | 入职时间     | 部门   | 工资   |
+--------+--------------+--------+--------+
| 荣七   | 1963-10-11   | 销售   |   8500 |
+--------+--------------+--------+--------+
1 row in set (0.00 sec)

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

mysql> select name as `姓名`,dept_name as `部门`,incoming as `工资` from dept inner join emp on dept.dept1=emp.dept2 where dept_name = '财务' and incoming > 2000;
+--------+--------+--------+
| 姓名   | 部门   | 工资   |
+--------+--------+--------+
| 张三   | 财务   |   4000 |
| 李四   | 财务   |   3500 |
+--------+--------+--------+
2 rows in set (0.00 sec)

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

mysql> select dept_name as `部门`,avg(incoming) as `平均工资` from dept inner join emp on dept.dept1=emp.dept2 group by dept_name;
+----------+--------------+
| 部门     | 平均工资     |
+----------+--------------+
| 财务     |    3166.6667 |
| 销售     |    8000.0000 |
| IT技术   |    7300.0000 |
+----------+--------------+
3 rows in set (0.00 sec)

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

mysql> select sid as `员工号`,name as `姓名`,dept_name as `部门` from dept inner join emp on dept.dept1=emp.dept2 where dept_name = 'IT技术';
+-----------+--------+----------+
| 员工号    | 姓名   | 部门     |
+-----------+--------+----------+
|      1879 | 牛八   | IT技术   |
+-----------+--------+----------+
1 row in set (0.00 sec)

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

mysql> select dept_name as `部门`,sum(incoming) as `总工资` from dept inner join emp on dept.dept1=emp.dept2 group by dept_name having dept_name = '财务';
+--------+-----------+
| 部门   | 总工资    |
+--------+-----------+
| 财务   |      9500 |
+--------+-----------+
1 row in set (0.00 sec)

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 name,dept_name,worktime_start from emp inner join dept on emp.dept2=dept.dept1 order by dept1 desc, worktime_start desc;
+--------+-----------+----------------+
| name   | dept_name | worktime_start |
+--------+-----------+----------------+
| 牛八   | IT技术    | 1971-10-20     |
| 赵六   | 销售      | 1970-10-11     |
| 荣七   | 销售      | 1963-10-11     |
| 王五   | 财务      | 1990-07-01     |
| 李四   | 财务      | 1983-04-01     |
| 张三   | 财务      | 1980-01-01     |
+--------+-----------+----------------+
6 rows in set (0.00 sec)

11. Find out which department has no employees yet;

mysql> select name,dept_name,worktime_start from emp left join dept on emp.dept2=dept.dept1 where dept_name is null;

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

mysql> select name,incoming,dept_name,dept2 from emp inner join dept on emp.dept2=dept.dept1 where incoming > 7000;
+--------+----------+-----------+-------+
| name   | incoming | dept_name | dept2 |
+--------+----------+-----------+-------+
| 荣七   |     8500 | 销售      |   102 |
| 赵六   |     7500 | 销售      |   102 |
| 牛八   |     7300 | IT技术    |   103 |
+--------+----------+-----------+-------+
3 rows in set (0.00 sec)

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

mysql> select dept_name as `部门`,sum(incoming) as `总工资` from dept inner join emp on dept.dept1=emp.dept2 group by dept_name;
+----------+-----------+
| 部门     | 总工资    |
+----------+-----------+
| 财务     |      9500 |
| 销售     |     16000 |
| IT技术   |      7300 |
+----------+-----------+
3 rows in set (0.00 sec)

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

mysql> select dept.dept_name as 部门,emp.name as 姓名 from dept inner join  (select deptt2,max(age) as max_age from emp group by dept2) as temp on temp.dept2=dept.dept1  inner  join emp on temp.dept2=emp.dept2 and temp.max_age=emp.age;
+----------+--------+
| 部门     | 姓名   |
+----------+--------+
| 财务     | 张三   |
| 销售     | 荣七   |
| IT技术   | 牛八   |
+----------+--------+
3 rows in set (0.00 sec)

15. Find Li Si's income and department name

mysql> select emp.name,dept.dept_name,emp.incoming from emp inner join dept on emp.dept22=dept.dept1 where emp.name = '李四';
+--------+-----------+----------+
| name   | dept_name | incoming |
+--------+-----------+----------+
| 李四   | 财务      |     3500 |
+--------+-----------+----------+
1 row in set (0.00 sec)

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

mysql> select de.name,de.dept_name,temp.max_incoming from (select emp.name,dept.dept_name,emp.incoming from emp inner join dept on emp.dept2=dept.dept1) as de right join (seleect max(incoming) as max_incoming,dept2 from emp group by dept2) as temp on de.incoming==temp.max_incoming;
+--------+-----------+--------------+
| name   | dept_name | max_incoming |
+--------+-----------+--------------+
| 张三   | 财务      |         4000 |
| 荣七   | 销售      |         8500 |
| 牛八   | IT技术    |         7300 |
+--------+-----------+--------------+
3 rows in set (0.00 sec)

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

mysql> select dept_name,count(dept_name) as `员工人数` from dept inner join emp on dept.dept1=emp.dept2 group by dept_name having `员工人数` > 1;
+-----------+--------------+
| dept_name | 员工人数     |
+-----------+--------------+
| 财务      |            3 |
| 销售      |            2 |
+-----------+--------------+
2 rows in set (0.00 sec)

19. Find the name of Zhang San's department

mysql> select name,dept_name from emp inner join dept on dept.dept1=emp.dept2 where name='张三';
+--------+-----------+
| name   | dept_name |
+--------+-----------+
| 张三   | 财务      |
+--------+-----------+
1 row in set (0.00 sec)

Guess you like

Origin blog.csdn.net/bo1029/article/details/131687586