MySQL必会的28条经典查询

 

MySQL必会的28条经典查询

原创作品。转载请注明出处https://blog.csdn.net/kk123k

表结构及测试数据请看我上一篇文章:学生选修成绩表测试数据

 

Student(Sno,Sname,Ssex) 学生表
Teacher(Tno,Tname) 教师表
Course(Cno,Cname,Tno) 选修课程表 

SC(Sno,Cno,score) 成绩表

问题:

1、查询课程编号“001”课程比“002”课程成绩高的所有学生的学号

select a.sno from (select sno,score from SC where Cno='001') a,(select sno,score from SC where Cno='002') b 
where a.score>b.score and a.sno=b.sno;

2、查询没学过“叶平”老师课的同学的学号、姓名

select sno,sname from student where sno not in
(select distinct sc.sno from sc,course,teacher where sc.cno=course.cno and course.tno=teacher.tno and teacher.tname='叶平')

select sno,sname from student where not exists
(select sc.* from sc,course,teacher where sc.cno=course.cno and course.tno=teacher.tno and teacher.tname='叶平' 
and sc.sno=student.sno)
(注:数据量很大的时候not exists比not in效率高一点点)

 

 

3、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩(不考虑成绩并列情况)

select s.sname,max(sc.score) from student s,sc,course c,teacher t where s.sno=sc.sno and c.cno=sc.cno and c.tno=t.tno 
and t.tname='叶平' 

4、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩(若有并列全部列出)

select s.sname,sc.score from student s,sc,course c,teacher t where s.sno=sc.sno and c.cno=sc.cno and c.tno=t.tno 
and t.tname='叶平' and sc.score=
(select max(sc1.score) from sc sc1,course c1,teacher t1 where c1.cno=sc1.cno and c1.tno=t1.tno and t1.tname='叶平')

5、查询所有同学的学号、姓名、选课数、总成绩

select s.sno,s.sname,count(sc.cno),sum(sc.score) from student s left join sc on s.sno=sc.sno group by s.sno

6、查询学过“叶平”老师所教的所有课的同学的学号、姓名

select student.sno,student.sname from student where student.sno in
(select sc.sno from sc,course,teacher where sc.cno=course.cno and course.tno=teacher.tno and teacher.tname='叶平' 
group by sc.sno having count(sc.cno)=
(select count(course.cno) from course,teacher where course.tno=teacher.tno and tname='叶平'))

7、查询学过编号“001”也学过编号“002”的课程的同学的学号、姓名

select sno,sname from student where sno in 
(select sc1.sno from sc sc1,sc sc2 where sc1.sno=sc2.sno and sc1.cno='001' and sc2.cno='002')
8、查询课程编号“002”课程的成绩比“001”课程低的所有同学的学号、姓名  (比上面第1条多了个姓名)
select s.sno,s.sname from 
student s,(select sno,score from sc where cno='001') a,(select sno,score from sc where cno='002') b 
where a.score>b.score and a.sno=b.sno and s.sno=a.sno

9、查询所有课程成绩小于60分的同学的学号、姓名

select distinct s.sno,s.sname from student s,sc where s.sno=sc.sno and sc.sno not in 
(select ss.sno from student ss, SC where ss.sno=sc.sno and sc.score>=60)

(注意:没有任何选修的人不应该列出来)

10、查询没有学全所有课的同学的学号、姓名

select s.sno,s.sname from student s left join sc 
on s.sno=sc.sno group by s.sno having count(sc.cno)<(select count(cno)from course)

(注意:这里要包括没有任何选修的人)、

11、查询至少有一门课与学号为“1007”的同学所学相同的同学的学号和姓名

select distinct s.sno,s.sname from student s,sc where s.sno=sc.sno and s.sno!='1007' and sc.cno in 
(select cno from sc where sno='1007')
12、查询和学号“1002”的同学学习的课程完全相同的其他同学学号和姓名
select s.sno,s.sname from student s,sc where s.sno=sc.sno and s.sno!='1002' and sc.cno in 
(select cno from sc where sno='1002') group by s.sno having count(sc.cno)=(select count(cno) from sc where sno='1002')

13、按平均成绩从高到低显示所有学生的“高等数学”、“大学英语”、“数据库”三门的课程成绩,按如下形式显示: 学生ID,高等数学,大学英语,数据库,有效课程数,有效平均分

select s.sno as '学生ID',
(select score from sc sc1 where sc.sno=sc1.sno and sc1.cno='001')as '高等数学',
(select score from sc sc2 where sc.sno=sc2.sno and sc2.cno='003')as '大学英语',
(select score from sc sc3 where sc.sno=sc3.sno and sc3.cno='004')as '数据库',
count(cno)as '有效课程数',avg(sc.score)as '有效平均分'
from student s left join (select * from sc where cno in(001,003,004)) sc on s.sno=sc.sno  
group by s.sno order by avg(sc.score) desc
14、求选了课程的学生人数
select count(*) from (select distinct sno from sc) ct

15、查询同名同姓学生名单,并统计同名人数

select sname,count(sname) from student group by Sname having count(sname)>1
16、查询每门课程的平均成绩和被选人数,结果按平均成绩降序排列,平均成绩相同时,按课程号升序排列
select cno,avg(score),count(sno) from SC group by cno order by avg(score) desc,cno asc
17、查询平均成绩大于85的所有学生的学号、姓名和平均成绩
select s.sno,s.sname,avg(sc.score) from student s,sc where s.sno=sc.sno group by s.sno having avg(sc.score)>85
18、查询课程名称为“数据库”且分数低于60的学生姓名和分数
select s.sno,s.sname from student s,sc,course c where s.sno=sc.sno and c.cno=sc.cno and c.cname='数据库' and sc.score<60
19、查询任何一门课程成绩在70分以上的姓名、课程名称和分数
select s.sname,c.cname,sc.score from student s,sc,course c where s.sno=sc.sno and c.cno=sc.cno and sc.score>70

20、查询姓“李”的老师的个数

select count(tno) from teacher where tname like '李%'

21、查询平均成绩大于60分的同学的学号和平均成绩

select sno,avg(score) from sc group by sno having avg(score)>60

22、查询至少选修两门课程的学生学号、姓名、选修数

select s.sno,s.sname,count(sc.cno) from student s,sc where s.sno=sc.sno group by sc.sno having count(sc.cno)>=2

23、查询全部学生都选修的课程的课程号和课程名

select c.cno,c.cname,count(sc.cno) from course c,sc where c.cno=sc.cno 
group by c.cno having count(sc.cno)=(select count(*) from student)
24、查询两门以上不及格课程的同学的学号及其平均成绩
select sno,avg(score) from sc where score<60 group by sno having count(cno)>=2
25、查询各科成绩前三名记录的学号、课程号、分数。(不考虑成绩并列情况)
select sc.sno,sc.cno,sc.score from sc where
(select count(sc1.cno) from sc sc1 where sc1.cno=sc.cno and sc1.score>sc.score)<3
order by sc.cno,score desc
26、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]
select sc.cno as '课程ID', c.cname as '课程名称'
,sum(case when score between 85 and 100 then 1 else 0 end) as '[100 - 85]' 
,sum(case when score between 70 and 85 then 1 else 0 end) as '[85 - 70]' 
,sum(case when score between 60 and 70 then 1 else 0 end) as '[70 - 60]' 
,sum(case when score < 60 then 1 else 0 end) as '[60 -]'
from sc,course c where sc.cno=c.cno group by sc.cno;
27、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
select cno as '课程ID',max(score) as '最高分',min(score) as '最低分' from sc group by cno
28、查询课程号“002”的成绩排名第3-第6(不考虑成绩并列情况)的同学的学号、姓名和分数
select s.sno,s.sname,sc.score from student s,sc where s.sno=sc.sno and sc.cno='002' 
order by sc.score desc limit 2,4

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值