测试表和数据
建表
学生表
CREATE TABLE `Student`(
`s_id` VARCHAR(20),
`s_name` VARCHAR(20) NOT NULL DEFAULT '',
`s_birth` VARCHAR(20) NOT NULL DEFAULT '',
`s_sex` VARCHAR(10) NOT NULL DEFAULT '',
PRIMARY KEY(`s_id`)
);
课程表
CREATE TABLE `Course`(
`c_id` VARCHAR(20),
`c_name` VARCHAR(20) NOT NULL DEFAULT '',
`t_id` VARCHAR(20) NOT NULL,
PRIMARY KEY(`c_id`)
);
教师表
CREATE TABLE `Teacher`(
`t_id` VARCHAR(20),
`t_name` VARCHAR(20) NOT NULL DEFAULT '',
PRIMARY KEY(`t_id`)
);
成绩表
CREATE TABLE `Score`(
`s_id` VARCHAR(20),
`c_id` VARCHAR(20),
`s_score` INT(3),
PRIMARY KEY(`s_id`,`c_id`)
);
插入学生表测试数据
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');
课程表测试数据
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
教师表测试数据
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
成绩表测试数据
insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);
测试表与数据如下图:
练习题
1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
select s1.*,s2.s_score as '语文',s3.s_score as '数学' from student s1
left join score s2 on s2.s_id = s1.s_id and s2.c_id = '01'
left join score s3 on s3.s_id = s1.s_id and s3.c_id = '02'
where s2.s_score > s3.s_score;
2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
select s1.*,s2.s_score as '语文',s3.s_score as '数学' from student s1
left join score s2 on s2.s_id = s1.s_id and s2.c_id = '01'
left join score s3 on s3.s_id = s1.s_id and s3.c_id = '02'
where s2.s_score < s3.s_score;
3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
方法一:
select a.s_id,a.s_name,b.f as '平均成绩' from student as a,(select s_id,avg(s_score) as f from score group by s_id having avg(s_score) >= 60) as b
where b.s_id = a.s_id;
方法二:
select b.s_id,b.s_name,ROUND(AVG(a.s_score),2) as avg_score from
student b
join score a on b.s_id = a.s_id
GROUP BY b.s_id,b.s_name HAVING avg_score >=60;
4、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
方法一:
select a.s_id,a.s_name,b.count,b.sum from student as a left join (select s_id,count(c_id) as count,sum(s_score) as sum from score group by s_id) as b
on a.s_id = b.s_id;
方法二:
select a.s_id,a.s_name,count(b.c_id) as sum_course,sum(b.s_score) as sum_score from
student a left join score b on a.s_id=b.s_id GROUP BY a.s_id,a.s_name;
5、查询"李"姓老师的数量
select count(*) from teacher where t_name like '李%';
6、查询学过"张三"老师授课的同学的信息
方法一:
select * from student a join score b on a.s_id = b.s_id join course c on b.c_id = c.c_id join teacher d on c.t_id = d.t_id and d.t_name = '张三';
方法二:
select a.* from student a join score b on a.s_id=b.s_id where b.c_id in
(select c_id from course where t_id =(select t_id from teacher where t_name = '张三'));
7、查询没学过"张三"老师授课的同学的信息
select * from student c where c.s_id not in
(select a.s_id from student a join score b on a.s_id=b.s_id where b.c_id in
(select a.c_id from course a join teacher b on a.t_id = b.t_id where t_name ='张三'));
8、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
方法一:
select s.* from student as s join (select a.s_id from score a join (select * from score where c_id = 01) b on a.s_id = b.s_id and a.c_id = 02)
as d where s.s_id = d.s_id;
方法二:
select a.* from student a,score b,score c where a.s_id = b.s_id and a.s_id = c.s_id and b.c_id='01' and c.c_id='02';
9、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
select * from student where s_id in (select s_id from score where c_id = 01)
and s_id not in (select s_id from score where c_id = 02);
10、查询没有学全所有课程的同学的信息
select a.* from student a left join score b on a.s_id = b.s_id group by a.s_id having count(b.c_id)<(select count(*) from course);
11、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
方法一:
select distinct st.* from student st left join score sc on sc.s_id=st.s_id
where sc.c_id in (select c_id from score where s_id = 01);
方法二:
select * from student where s_id in
(select distinct a.s_id from score a where a.c_id in(select a.c_id from score a where a.s_id='01'));
12、查询没学过"张三"老师讲授的任一门课程的学生姓名
select s_name from student where s_id not in
(select s_id from score where c_id in
(select c_id from course where t_id = (select t_id from teacher where t_name = '张三')));
13、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select a.s_id,a.s_name ,b.x as '平均成绩' from student a,
(select s_id,avg(s_score) as x from score where s_score < 60
group by s_id having count(c_id) >= 2) b where a.s_id = b.s_id;
14、检索"01"课程分数小于60,按分数降序排列的学生信息
方法一:
select a.*,b.c_id,b.s_score from student a left join score b on a.s_id = b.s_id where b.c_id = 01 and b.s_score < 60
order by b.s_score desc;
方法二:
select a.*,b.c_id,b.s_score from student a,score b
where a.s_id = b.s_id and b.c_id='01' and b.s_score<60 ORDER BY b.s_score DESC;
15、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select a.s_id,(select s_score from score where s_id=a.s_id and c_id='01') as 语文,
(select s_score from score where s_id=a.s_id and c_id='02') as 数学,
(select s_score from score where s_id=a.s_id and c_id='03') as 英语,
avg(s_score) from score a group by a.s_id order by avg(s_score) desc;
16、查询不同老师所教不同课程平均分从高到低显示
方法一:
select c.t_name,b.c_name,avg(s_score) from score a join course b on a.c_id = b.c_id join teacher c on b.t_id = c.t_id
group by a.c_id order by avg(s_score) desc;
方法二:
select a.t_id,c.t_name,a.c_id,ROUND(avg(s_score),2) as avg_score from course a
left join score b on a.c_id=b.c_id
left join teacher c on a.t_id=c.t_id
GROUP BY a.c_id,a.t_id,c.t_name ORDER BY avg_score DESC;
17、查询各科成绩前三名的记录
-- 1.选出b表比a表成绩大的所有组
-- 2.选出比当前id成绩大的 小于三个的
select a.s_id,a.c_id,a.s_score from score a
left join score b on a.c_id = b.c_id and a.s_score<b.s_score
group by a.s_id,a.c_id,a.s_score HAVING COUNT(b.s_id)<3
ORDER BY a.c_id,a.s_score DESC;
18、查询每门课程被选修的学生数
select c_id,count(s_id) from score group by c_id;
18、查询出只有两门课程的全部学生的学号和姓名
方法一:
select a.s_id,a.s_name from student a join score b on a.s_id = b.s_id group by b.s_id having count(c_id) = 2;
方法二:
select s_id,s_name from student where s_id in (select s_id from score GROUP BY s_id HAVING COUNT(c_id)=2);
19、查询男生、女生人数
select s_sex,count(s_sex) from student group by s_sex;
20、查询名字中含有"风"字的学生信息
select * from student where s_name like '%风%';
21、查询同名同性学生名单,并统计同名人数
select a.s_name,a.s_sex,count(*) from student a JOIN
student b on a.s_id !=b.s_id and a.s_name = b.s_name and a.s_sex = b.s_sex
GROUP BY a.s_name,a.s_sex;
22、查询1990年出生的学生名单
select s_name from student where s_birth like '1990%';
23、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select c_id,avg(s_score) from score group by c_id order by avg(s_score) desc,c_id;
24、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
select a.s_id,b.s_name,ROUND(avg(a.s_score),2) as avg_score from score a
left join student b on a.s_id=b.s_id GROUP BY s_id HAVING avg_score>=85;
25、查询课程名称为"数学",且分数低于60的学生姓名和分数
select a.s_name,b.s_score from score b join student a on a.s_id=b.s_id where b.c_id=(
select c_id from course where c_name ='数学') and b.s_score<60;
26、查询所有学生的课程及分数情况;
select a.s_id,a.s_name,
SUM(case c.c_name when '语文' then b.s_score else 0 end) as '语文',
SUM(case c.c_name when '数学' then b.s_score else 0 end) as '数学',
SUM(case c.c_name when '英语' then b.s_score else 0 end) as '英语',
SUM(b.s_score) as '总分'
from student a left join score b on a.s_id = b.s_id
left join course c on b.c_id = c.c_id
GROUP BY a.s_id,a.s_name;
27、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
select a.s_name,b.c_name,c.s_score from course b left join score c on b.c_id = c.c_id
left join student a on a.s_id=c.s_id where c.s_score>=70;
28、查询不及格的课程
select a.s_id,a.c_id,b.c_name,a.s_score from score a left join course b on a.c_id = b.c_id
where a.s_score<60;
29、查询课程编号为01且课程成绩在80分及以上的学生的学号和姓名;
方法一
select s_id,s_name from student where s_id in (select s_id from score where c_id = '01' and s_score >= 80);
方法二
select a.s_id,b.s_name from score a LEFT JOIN student b on a.s_id = b.s_id
where a.c_id = '01' and a.s_score>=80;
30、求每门课程的学生人数
select c_id,count(s_id) from score group by c_id;
31、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
方法一:
select a.*,b.s_score,b.c_id,c.c_name from student a left join score b on a.s_id = b.s_id left join course c on b.c_id = c.c_id left join teacher d on c.t_id = d.t_id
where d.t_name = '张三' order by b.s_score desc limit 0,1;
方法二:
select a.*,b.s_score,b.c_id,c.c_name from student a
LEFT JOIN score b on a.s_id = b.s_id
LEFT JOIN course c on b.c_id=c.c_id
where b.c_id =(select c_id from course c,teacher d where c.t_id=d.t_id and d.t_name='张三')
and b.s_score in (select MAX(s_score) from score where c_id='02');
32、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
select DISTINCT b.s_id,b.c_id,b.s_score from score a,score b where a.c_id != b.c_id and a.s_score = b.s_score;
33、查询每门功成绩最好的前两名
select a.s_id,a.c_id,a.s_score from score a
where (select COUNT(1) from score b where b.c_id=a.c_id and b.s_score>=a.s_score)<=2 ORDER BY a.c_id;
34、统计每门课程的学生选修人数(超过5人的课程才统计)。
要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select c_id,count(s_id) from score group by c_id having count(s_id) > 5 order by count(s_id) desc,c_id;
35、检索至少选修两门课程的学生学号
select s_id from score group by s_id having count(c_id) >=2;
36、查询选修了全部课程的学生信息
方法一:
select a.* from student a join score b on a.s_id = b.s_id group by b.s_id having count(b.c_id) = (select count(*) from course);
方法二:
select * from student where s_id in (select s_id from score GROUP BY s_id HAVING count(*)=(select count(*) from course));
37、查询本周过生日的学生
select * from student where WEEK(DATE_FORMAT(NOW(),'%Y%m%d')) =WEEK(s_birth);
38、查询下周过生日的学生
select * from student where WEEK(DATE_FORMAT(NOW(),'%Y%m%d'))+1 =WEEK(s_birth);
39、查询本月过生日的学生
select * from student where month(date_format(now(),'%Y%m%d')) =month(s_birth);
40、查询下月过生日的学生
select * from student where month(date_format(now(),'%Y%m%d'))+1 =month(s_birth);