一、
1、显示所有职工的基本信息。
select * from worker;
2、查询所有职工所属部门的部门号,不显示重复的部门号。
select distinct 部门号 from worker; 3、求出所有职工的人数。
select count(姓名) from worker;
4、列出最高工和最低工资。
select max(工资),min(工资) from worker;
5、列出职工的平均工资和总工资。
select avg(工资), sum(工资) from worker;
6、创建一个只有职工号、姓名和参加工作的新表,名为工作日期表。
create table 工作日期表 as select 职工号, 姓名, 工作时间 from worker;
7、显示所有女职工的年龄。
select 姓名, datediff(year, 出生日期, getdate()) as 年龄 from worker where 性别 = '女';
8、列出所有姓刘的职工的职工号、姓名和出生日期。
select 职工号, 姓名, 出生日期 from worker where 姓名 like '刘%';
9、列出1960年以前出生的职工的姓名、参加工作日期。
select 姓名, 工作时间 from worker where 出生日期 < '1960-01-01';
10、列出工资在1000-2000之间的所有职工姓名。
select 姓名 from worker where 工资 between 1000 and 2000;
11、列出所有陈姓和李姓的职工姓名。
select 姓名 from worker where 姓名 like '陈%' or 姓名 like '李%';
12、列出所有部门号为2和3的职工号、姓名、党员否。
select 职工号, 姓名, 政治面貌 from worker where 部门号 in (102,103);
13、将职工表worker中的职工按出生的先后顺序排序。
select * from worker order by 出生日期;
14、显示工资最高的前3名职工的职工号和姓名。
select 职工号, 姓名 from worker order by 工资 desc limit 3;
15、求出各部门党员的人数。
select 部门号, count(*) as 党员人数 from worker where 政治面貌 = '党员' group by 部门号;
16、统计各部门的工资和平均工资
select 部门号, sum(工资) as 总工资, avg(工资) as 平均工资 from worker groupby 部门号;
17、列出总人数大于4的部门号和总人数。
select 部门号, count(*) as 总人数 from worker group by 部门号 having 总人数 > 4;
二、
3.查询student表的所有记录
select * from student;
4.查询student表的第2条到4条记录
select * from student limit 1,3;
5.从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息
select id,name,department from student;
6.从student表中查询计算机系和英语系的学生的信息
select * from student where department='计算机系' or department='英语系';
7.从student表中查询年龄18~22岁的学生信息
select * from student where 18<(2022-birth) and (2022-birth)<20;
8.从student表中查询每个院系有多少人
select department,count(department) from student group by department;
9.从score表中查询每个科目的最高分
select c_name,max(grade) from score group by c_name;
10.查询李四的考试科目(c_name)和考试成绩(grade)
select c_name,grade from student stu inner join score s on s.stu_id=stu.id where name='李四';
11.用连接的方式查询所有学生的信息和考试信息
select * from student stu left join score s on s.stu_id=stu.id;
12.计算每个学生的总成绩
select name,sum(grade) from score inner join student on score.stu_id=student.id group by stu_id;
13.计算每个考试科目的平均成绩
select c_name,avg(grade) from score group by c_name;
14.查询计算机成绩低于95的学生信息
select * from student where id in (select stu_id from score where c_name='计算机' and grade<95);
15.查询同时参加计算机和英语考试的学生的信息
select * from student where id in (select stu_id from score where c_name ='计算机') and id in (select stu_id from score where c_name='英语');
16.将计算机考试成绩按从高到低进行排序
select * from score where c_name='计算机' order by grade desc;
17.从student表和score表中查询出学生的学号,然后合并查询结果
select id from student union select stu_id from score;
18.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩
select name,department,c_name,grade from student stu inner join score s on stu.id=s.stu_id where name like '王%' or name like '张%';
19.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩
select name,2022-birth age,department,c_name,grade from student stu inner join score s on stu.id=s.stu_id where address like '%湖南%';
三、面试题
(1)、select s_id, s_name, count(distinct c_id) as course_count, sum(s_score) as total_score from student left join score on student.s_id = score.s_id group by s_id, s_name;
(2)、
select distinct student.s_id, student.s_name from student join score on student.s_id = score.s_id join course on score.c_id = course.c_id join teacher on course.t_id = teacher.t_id where teacher.t_name = '张三';
(3)、
select distinct student.s_id, student.s_name from student join score on student.s_id = score.s_id where score.c_id in ( select c_id from score where s_id = 2 );
(4)、
(5)、
select course.c_id, course.c_name, count(case when score.s_score between 100 and 85 then 1 end) as '[100-85]', count(case when score.s_score between 85 and 70 then 1 end) as '[85-70]', count(case when score.s_score between 70 and 60 then 1 end) as '[70-60]', count(case when score.s_score < 60 then 1 end) as '[<60]' from course left join score on course.c_id = score.c_id group by course.c_id, course.c_name;