MySQL二表连接查询练习

题目

1.找出销售部门中年纪最大的员工的姓名
2.求财务部门最低工资的员工姓名
3.列出每个部门收入总和高于9000的部门名称
4.求工资在7500到8500元之间,年龄最大的人的姓名及部门
5.找出销售部门收入最低的员工入职时间
6.财务部门收入超过2000元的员工姓名
7.列出每个部门的平均收入及部门名称
8.IT技术部入职员工的员工号
9.财务部门的收入总和;
10.先按部门号大小排序,再依据入职时间由早到晚排序员工信息表
11.找出哪个部门还没有员工入职;
12.列出部门员工收入大于7000的部门编号,部门名称;
13.列出每一个部门的员工总收入及部门名称;
14.列出每一个部门中年纪最大的员工姓名,部门名称;
15.求李四的收入及部门名称
16.列出每个部门中收入最高的员工姓名,部门名称,收入,并按照收入降序
17.列出部门员工数大于1个的部门名称
19.查找张三所在的部门名称

练习题

前期准备

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)

答案

1.找出销售部门中年纪最大的员工的姓名

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.求财务部门最低工资的员工姓名

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.列出每个部门收入总和高于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.求工资在7500到8500元之间,年龄最大的人的姓名及部门

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.找出销售部门收入最低的员工入职时间

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.财务部门收入超过2000元的员工姓名

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.列出每个部门的平均收入及部门名称

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.IT技术部入职员工的员工号

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.财务部门的收入总和;

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.先按部门号大小排序,再依据入职时间由早到晚排序员工信息表

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.找出哪个部门还没有员工入职;

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

12.列出部门员工收入大于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.列出每一个部门的员工总收入及部门名称;

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.列出每一个部门中年纪最大的员工姓名,部门名称;

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.求李四的收入及部门名称

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.列出每个部门中收入最高的员工姓名,部门名称,收入,并按照收入降序

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.列出部门员工数大于1个的部门名称

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.查找张三所在的部门名称

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)

你可能感兴趣的:(数据库,mysql,数据库)