1. 程式人生 > >MySQL必會的28條經典查詢

MySQL必會的28條經典查詢

.cn -s 創作 desc 結果 若有 mit ade 原創

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

MySQL必會的28條經典查詢