目录
6、创建一个只有职工号、姓名和参加工作的新表,名为工作日期表
3.从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息
8.查询李四的考试科目(c_name)和考试成绩(grade)
15.从student表和score表中查询出学生的学号,然后合并查询结果
单表查询
创建worker表并插入信息
1、显示所有职工的基本信息
select * from worker;
![](https://i-blog.csdnimg.cn/blog_migrate/a773c9b8627cdba7803e84f5271a4313.png)
2、查询所有职工所属部门的部门号,不显示重复的部门号
select distinct 部门号 from worker;
3、求出所有职工的人数
select count(*) 员工号 from worker;
![](https://i-blog.csdnimg.cn/blog_migrate/ba4e5638eec78053cffd34809cd3e260.png)
4、列出最高工资和最低工资
select max(工资) 最高工资,min(工资) 最低工资 from worker;
5、列出职工的平均工资和总工资
select avg(工资) 平均工资,sum(工资) 总工资 from worker;
![](https://i-blog.csdnimg.cn/blog_migrate/055fdd739a24f2b564fbd6db85d993f8.png)
6、创建一个只有职工号、姓名和参加工作的新表,名为工作日期表
CREATE TABLE `工作日期` (
`职工号` INT(11) NOT NULL,
`姓名` VARCHAR(20) NOT NULL,
`工作时间` DATE NOT NULL,
PRIMARY KEY (`职工号`)
) ENGINE=INNODB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
7、显示所有职工的年龄
SELECT 姓名,2023-year(出生日期) from worker;
![](https://i-blog.csdnimg.cn/blog_migrate/6e5ec5a16ddc003ef19b826c0789f9d4.png)
8、列出所有姓刘的职工的职工号、姓名和出生日期
select 职工号,姓名,出生日期 from worker where 姓名 like '刘%';
![](https://i-blog.csdnimg.cn/blog_migrate/1b867ac3da8bf913e3f077430efb743e.png)
9、列出1960年以前出生的职工的姓名、参加工作日期
select 姓名,工作时间 参加工作日期 from worker where 出生日期 like 出生日期>'1960%';
![](https://i-blog.csdnimg.cn/blog_migrate/7132a942bed4cac49741868f56cc893e.png)
10、列出工资在1000-2000之间的所有职工姓名
select 姓名 from worker where 工资 between 1000 and 2000;
![](https://i-blog.csdnimg.cn/blog_migrate/feadf371ea12d0c2551b888617f03aab.png)
11、列出所有陈姓和李姓的职工姓名
select 姓名 from worker where 姓名 like '陈%' or 姓名 like '李%';
12、列出所有部门号为2和3的职工号、姓名、党员否
select 职工号,姓名,政治面貌 from worker where 部门号=102 or 部门号=103;
![](https://i-blog.csdnimg.cn/blog_migrate/276276a8892b63483bfa6dfb808c5922.png)
13、将职工表worker中的职工按出生的先后顺序排序
select * from worker order by 出生日期;
14、显示工资最高的前3名职工的职工号和姓名
select 职工号,姓名 from worker order by 工资 desc limit 3;
![](https://i-blog.csdnimg.cn/blog_migrate/7251a10083dc72157732dfe041fdff05.png)
15、求出各部门党员的人数
select 部门号,count(*) 党员总数 from worker where 政治面貌='党员' group by 部门号;
16、统计各部门的工资和平均工资
select `部门号`,sum(`工资`),avg(`工资`) from worker group by `部门号`;
![](https://i-blog.csdnimg.cn/blog_migrate/86e487ae2d0880c064978e414336eeca.png)
17、列出总人数大于4的部门号和总人数
select 部门号,count(部门号) 总人数 from worker group by 部门号 having count(部门号)>=4;
多表查询
创建student和score表并插入信息
1.查询student表的所有记录
select * from student;
![](https://i-blog.csdnimg.cn/blog_migrate/50b01bbd0fdf27e26b42143f1dd5c3ba.png)
2.查询student表的第2条到4条记录
select * from student limit 3,3;
3.从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息
select id,name,department from student;
![](https://i-blog.csdnimg.cn/blog_migrate/3afe867439f5455fa29f146da81038c4.png)
4.从student表中查询计算机系和英语系的学生的信息
select * from student where department='计算机系' or department='英语系';
![](https://i-blog.csdnimg.cn/blog_migrate/7f98fe0079252fe613eeeb7f2bea353c.png)
5.从student表中查询年龄18~22岁的学生信息
SELECT * FROM student where birth>2001 and birth<2005;
![](https://i-blog.csdnimg.cn/blog_migrate/f0dadd2ec1aca130d50d9af12e2f1de2.png)
6.从student表中查询每个院系有多少人
select department,count(department) 总人数 from student group by department;
![](https://i-blog.csdnimg.cn/blog_migrate/72c0bda737b1a0d276dd5aaecf00eef4.png)
7.从score表中查询每个科目的最高分
select c_name,max(grade) 最高分 from score group by c_name;
![](https://i-blog.csdnimg.cn/blog_migrate/845ab9bfa2b7956d4099bc00a07ee62d.png)
8.查询李四的考试科目(c_name)和考试成绩(grade)
select score.c_name,score.grade from score inner join student s on score.stu_id=s.id where s.name='李四';
![](https://i-blog.csdnimg.cn/blog_migrate/bc37bf40d995fefd6059f3628ac04362.png)
9.用连接的方式查询所有学生的信息和考试信息
select * from score c inner join student s on s.id=c.stu_id;
10.计算每个学生的总成绩
select name,sum(grade) 总成绩 from student s inner join score c on s.id=c.stu_id group by c.stu_id;
![](https://i-blog.csdnimg.cn/blog_migrate/fbcc3050d5cfa4495f06ce33318a4754.png)
11.计算每个考试科目的平均成绩
select c_name,avg(grade) 平均成绩 from score group by c_name;
![](https://i-blog.csdnimg.cn/blog_migrate/c46ca47baa44393e4b848cb068250810.png)
12.查询计算机成绩低于95的学生信息
select * from student s inner join score c on c.stu_id=s.id where c_name='计算机' and grade<95;
13.查询同时参加计算机和英语考试的学生的信息
select * from student s inner join score c on c.stu_id=s.id where c_name='计算机' and c_name='计算机';
![](https://i-blog.csdnimg.cn/blog_migrate/949ba393a14f205fa290e8b8f5d01cc6.png)
14.将计算机考试成绩按从高到低进行排序
select name,grade from score c inner join student s on s.id=c.stu_id where c_name='计算机' order by grade desc;
15.从student表和score表中查询出学生的学号,然后合并查询结果
select name,stu_id from student s inner join score c on s.id=c.stu_id;
![](https://i-blog.csdnimg.cn/blog_migrate/0ad7b31d0facb003427f851aab9d09e0.png)
16.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩
select name,department,c_name,grade from student,score where student.id=score.stu_id;
![](https://i-blog.csdnimg.cn/blog_migrate/ab13d3e7c17a052236d1c747b0a42b15.png)
17.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩
select name,department,c_name,grade from student s inner join score c on s.id=c.stu_id where address like '湖南%';