单表查询
素材: 表名:worker-- 表中字段均为中文,比如 部门号 工资 职工号 参加工作 等
CREATE TABLE `worker` (
`部门号` int(11) NOT NULL,
`职工号` int(11) NOT NULL,
`工作时间` date NOT NULL,
`工资` float(8,2) NOT NULL,
`政治面貌` varchar(10) NOT NULL DEFAULT '群众',
`姓名` varchar(20) NOT NULL,
`出生日期` date NOT NULL,`年龄` date NOT NULL,
PRIMARY KEY (`职工号`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生
日期`) VALUES (101, 1001, '2015-5-4', 3500.00, '群众', '张三', '1990-7-1');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生
日期`) VALUES (101, 1002, '2017-2-6', 3200.00, '团员', '李四', '1997-2-8');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生
日期`) VALUES (102, 1003, '2011-1-4', 8500.00, '党员', '王亮', '1983-6-8');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生
日期`) VALUES (102, 1004, '2016-10-10', 5500.00, '群众', '赵六', '1994-9-5');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生
日期`) VALUES (102, 1005, '2014-4-1', 4800.00, '党员', '钱七', '1992-12-30');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生
日期`) VALUES (102, 1006, '2017-5-5', 4500.00, '党员', '孙八', '1996-9-2');

1、显示所有职工的基本信息。
mysql> select * from worker;
2、查询所有职工所属部门的部门号,不显示重复的部门号。
select distinct `部门号` from worker;

3、求出所有职工的人数。select count(*) from worker;

4、列出最高工和最低工资。
select max(`工资`) as '最高工资',min(`工资`) as '最低工资' from worker;

5、列出职工的平均工资和总工资。
select avg(`工资`) as '平均工资',sum(`工资`) as '总工资' from worker;

6、创建一个只有职工号、姓名和参加工作的新表,名为工作日期表。
create table `工作日期表` ( `职工号` int,`姓名` varchar (10),`参加工作`date);

8、列出所有姓赵的职工的职工号、姓名和出生日期。
select `职工号`,`姓名`,`出生日期` from worker where `姓名` like '赵%';

9、列出1990年以前出生的职工的姓名、参加工作日期。
select `姓名`,`工作时间` from worker where `出生日期` < '1990-1-1';

10、列出工资在3000-5000之间的所有职工姓名。
select `姓名` from worker where `工资` >= 3000 and `工资` <= 5000;

11、列出所有王姓和李姓的职工姓名。
select `姓名` from worker where `姓名` like '王%' or `姓名` like '李%';

12、列出所有部门号为102的职工号、姓名、党员否。
select `职工号`,`姓名`,`政治面貌` from worker where `部门号` = 102;

13、将职工表worker中的职工按出生的先后顺序排序。
select * from worker order by `出生日期` asc;

14、显示工资最高的前3名职工的职工号和姓名。
select `职工号`,`姓名` from worker where `工资` order by `工资` desc limit 3;
15、求出各部门党员的人数。
select count(*) as '党员的人数' from worker where `政治面貌` = '党员';

16、统计各部门的工资和平均工资
select sum(工资) as '101总工资' from worker where `部门号` = '101';

select avg(工资) as '101平均工资' from worker where `部门号` = '101';
select sum(工资) as '102总工资' from worker where `部门号` = '102';
select avg(工资) as '102平均工资' from worker where `部门号` = '102';
17、列出总人数大于3的部门号和总人数。
select `部门号`,count(*) `总人数` from worker group by `部门号` having count(部门号) > 3;
