一.创建表并插入数据
mysql> create table worker(
-> department_id int(11) not null comment '部门号',
-> worker_id int(11) primary key not null comment '职工号',
-> worker_date date not null comment '工作时间',
-> wages float(8,2) not null comment '工资',
-> politics varchar(10) not null default '群众' comment '政治面貌',
-> name varchar(20) not null comment '姓名',
-> borth_date date not null comment '出生日期'
-> )engine=InnoDB default charset=utf8 row_format=dynamic;
Query OK, 0 rows affected, 4 warnings (0.02 sec)
mysql> insert into worker (department_id,worker_id,worker_date,wages,politics,name,borth_date) values (101,1001,'2015-5-4',3500.00,'群众','张三','1990-7-1');
Query OK, 1 row affected (0.01 sec)
mysql> insert into worker (department_id,worker_id,worker_date,wages,politics,name,borth_date) values (101,1001,'2017-2-6',3200.00,'团员','李四','1997-2-8');
Query OK, 1 row affected (0.00 sec)
mysql> insert into worker (department_id,worker_id,worker_date,wages,politics,name,borth_date) values (102,1003,'2011-1-4',8500.00,'党员','王亮','1983-6-8');
Query OK, 1 row affected (0.00 sec)
mysql> insert into worker (department_id,worker_id,worker_date,wages,politics,name,borth_date) values (102,1004,'2016-10-10',5500.00,'群众','赵六','1994-9-5');
Query OK, 1 row affected (0.00 sec)
mysql> insert into worker (department_id,worker_id,worker_date,wages,politics,name,borth_date) values (102,1005,'2014-4-1',4800.00,'党员','钱七','1992-12-30');
Query OK, 1 row affected (0.01 sec)
mysql> insert into worker (department_id,worker_id,worker_date,wages,politics,name,borth_date) values (102,1006,'2017-5-5',4500.00,'党员','孙八','1996-9-2');
Query OK, 1 row affected (0.00 sec
1、显示所有职工的基本信息
select *from worker;
+---------------+-----------+-------------+---------+----------+--------+------------+
| department_id | worker_id | worker_date | wages | politics | name | borth_date |
+---------------+-----------+-------------+---------+----------+--------+------------+
| 101 | 1001 | 2015-05-04 | 3500.00 | 群众 | 张三 | 1990-07-01 |
| 101 | 1001 | 2015-05-04 | 3500.00 | 群众 | 张三 | 1990-07-01 |
| 101 | 1001 | 2017-02-06 | 3200.00 | 团员 | 李四 | 1997-02-08 |
| 102 | 1003 | 2011-01-04 | 8500.00 | 党员 | 王亮 | 1983-06-08 |
| 102 | 1004 | 2016-10-10 | 5500.00 | 群众 | 赵六 | 1994-09-05 |
| 102 | 1005 | 2014-04-01 | 4800.00 | 党员 | 钱七 | 1992-12-30 |
| 102 | 1006 | 2017-05-05 | 4500.00 | 党员 | 孙八 | 1996-09-02 |
+---------------+-----------+-------------+---------+----------+--------+------------+
7 rows in set (0.00 sec)
2、查询所有职工所属部门的部门号,不显示重复的部门号
mysql> select distinct department_id from worker;
+---------------+
| department_id |
+---------------+
| 101 |
| 102 |
+---------------+
2 rows in set (0.02 sec)
3、求出所有职工的人数。
mysql> select count(name) from worker;
+-------------+
| count(name) |
+-------------+
| 7 |
+-------------+
1 row in set (0.04 sec)
4、列出最高工和最低工资。
mysql> select max(wages) as '最高工资',min(wages) as '最低工资' from worker;
+--------------+--------------+
| 最高工资 | 最低工资 |
+--------------+--------------+
| 8500.00 | 3200.00 |
+--------------+--------------+
1 row in set (0.01 sec)
5、列出职工的平均工资和总工资。
mysql> select avg(wages) as '平均工资',sum(wages) as '总工资' from worker;
+--------------+-----------+
| 平均工资 | 总工资 |
+--------------+-----------+
| 4785.714286 | 33500.00 |
+--------------+-----------+
1 row in set (0.01 sec)
6、创建一个只有职工号、姓名和参加工作的新表,名为工作日期表。
mysql> create table workdate select worker_id,name from worker;
Query OK, 7 rows affected (0.02 sec)
Records: 7 Duplicates: 0 Warnings: 0
7、列出1960年以前出生的职工的姓名、参加工作日期。
mysql> select name,worker_date from worker where borth_date < '1960-1-1';
Empty set (0.02 sec)
8、列出工资在1000-2000之间的所有职工姓名。
mysql> select name from worker where wages > 1000 and wages < 2000;
Empty set (0.00 sec)
9、列出所有陈姓和李姓的职工姓名。
mysql> select name from worker where name like '陈%' or name like '李%';
+--------+
| name |
+--------+
| 李四 |
+--------+
1 row in set (0.01 sec)
10、列出所有部门号为2和3的职工号、姓名、党员否。
mysql> select worker_id,name,politics from worker where politics!='党员' and department_id<=>102 or department_id<=>103;
+-----------+--------+----------+
| worker_id | name | politics |
+-----------+--------+----------+
| 1004 | 赵六 | 群众 |
+-----------+--------+----------+
1 row in set (0.00 sec)
11、将职工表worker中的职工按出生的先后顺序排序。
mysql> select *from worker order by borth_date;
+---------------+-----------+-------------+---------+----------+--------+------------+
| department_id | worker_id | worker_date | wages | politics | name | borth_date |
+---------------+-----------+-------------+---------+----------+--------+------------+
| 102 | 1003 | 2011-01-04 | 8500.00 | 党员 | 王亮 | 1983-06-08 |
| 101 | 1001 | 2015-05-04 | 3500.00 | 群众 | 张三 | 1990-07-01 |
| 101 | 1001 | 2015-05-04 | 3500.00 | 群众 | 张三 | 1990-07-01 |
| 102 | 1005 | 2014-04-01 | 4800.00 | 党员 | 钱七 | 1992-12-30 |
| 102 | 1004 | 2016-10-10 | 5500.00 | 群众 | 赵六 | 1994-09-05 |
| 102 | 1006 | 2017-05-05 | 4500.00 | 党员 | 孙八 | 1996-09-02 |
| 101 | 1001 | 2017-02-06 | 3200.00 | 团员 | 李四 | 1997-02-08 |
+---------------+-----------+-------------+---------+----------+--------+------------+
7 rows in set (0.00 sec)
12、显示工资最高的前3名职工的职工号和姓名。
mysql> select worker_id,name from worker order by wages desc limit 0,3;
+-----------+--------+
| worker_id | name |
+-----------+--------+
| 1003 | 王亮 |
| 1004 | 赵六 |
| 1005 | 钱七 |
+-----------+--------+
3 rows in set (0.00 sec)
13、求出各部门党员的人数。
mysql> select department_id,count(name) from worker where politics='党员'group by department_id;
+---------------+-------------+
| department_id | count(name) |
+---------------+-------------+
| 102 | 3 |
+---------------+-------------+
1 row in set (0.00 sec)
14、统计各部门的工资和平均工资
mysql> select department_id,sum(wages),avg(wages) from worker group by department_id;
+---------------+------------+-------------+
| department_id | sum(wages) | avg(wages) |
+---------------+------------+-------------+
| 101 | 10200.00 | 3400.000000 |
| 102 | 23300.00 | 5825.000000 |
+---------------+------------+-------------+
2 rows in set (0.00 sec
15、列出总人数大于4的部门号和总人数
select department_id,count(department_id) from worker group by(department_id) having count(department_id) > 4;
Empty set (0.00 sec)