搞定SQL面試(2)
阿新 • • 發佈:2018-09-15
min 全部 pre null sse case order 和平 iso
- 16、按平均成績從低到高顯示所有學生的“語文”、“數學”、“英語”三門的課程成績,
按如下形式顯示: 學生ID,語文,數學,英語,有效課程數,有效平均分;
select t.sid as ‘學生ID‘, (select score from sc where sid=t.sid and cid=‘002‘) as ‘語文‘, (select score from sc where sid=t.sid and cid=‘003‘) as ‘數學‘, (select score from sc where sid=t.sid and cid=‘004‘) as ‘英語‘, count(t.cid) as ‘有效課程數‘, avg(t.score) as ‘有效平均分‘ from sc t group by t.sid order by avg(t.score);
- 17、查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分;
select cid as ‘課程ID‘,max(score) as ‘最高分‘,min(score) as ‘最低分‘
from sc group by cid order by cid;
- 18、按各科平均成績從低到高和及格率的百分數從高到低順序;
select sc.cid,c.cname,round(IFNULL(avg(sc.score),0),2) as ‘平均成績‘, round(100*sum(case when ifnull(sc.score,0)>=60 then 1 else 0 end)/count(*),2) as ‘及格率‘ from sc sc,course c where sc.cid=c.cid group by 1,2 order by ‘及格率‘ desc;
- 19、查詢如下課程平均成績和及格率的百分數(備註:需要在1行內顯示):
企業管理(002),OO&UML (003),數據庫(004)
select sum(case when cid=‘002‘ then score else 0 end)/sum(case when cid=‘002‘ then 1 else 0 end) as ‘企業管理平均分‘, sum(case when cid=‘002‘ and score>=60 then 1 else 0 end)/sum(case when cid=‘002‘ then 1 else 0 end) as ‘企業管理及格率‘, sum(case when cid=‘003‘ then score else 0 end)/sum(case when cid=‘003‘ then 1 else 0 end) as ‘OO&UML平均分‘, sum(case when cid=‘003‘ and score>=60 then 1 else 0 end)/sum(case when cid=‘003‘ then 1 else 0 end) as ‘OO&UML及格率‘, sum(case when cid=‘004‘ then score else 0 end)/sum(case when cid=‘004‘ then 1 else 0 end) as ‘數據庫平均分‘, sum(case when cid=‘004‘ and score>=60 then 1 else 0 end)/sum(case when cid=‘004‘ then 1 else 0 end) as ‘數據庫及格率‘ from sc;
- 20、查詢不同老師所教不同課程平均分從高到低顯示;
select c.cid,max(c.cname) as ‘cname‘,max(t.tid) as ‘tid‘,max(t.tname) as ‘tname‘,
avg(score) as ‘avgscore‘
from sc sc,course c,teacher t
where sc.cid=c.cid and c.tid=t.tid
group by c.cid
order by avgscore desc;
- 21、統計列印各科成績,各分數段人數:課程ID,課程名稱,[100-85],[85-70],[70-60],[ <60]
select sc.cid,max(c.cname) as ‘課程名稱‘,
sum(case when sc.score between 85 and 100 then 1 else 0 end) as ‘[100-85]‘,
sum(case when sc.score between 70 and 85 then 1 else 0 end) as ‘[85-70]‘,
sum(case when sc.score between 60 and 70 then 1 else 0 end) as ‘[70-60]‘,
sum(case when sc.score between 0 and 100 then 1 else 0 end) as ‘[ <60]‘
from sc sc,course c
where sc.cid=c.cid
group by sc.cid
- 22、查詢學生平均成績及其名次;
select s.Sid,s.Sname,T2.AvgScore,
(select COUNT(AvgScore) from
(select Sid,AVG(Score) as ‘AvgScore‘ from SC group by Sid) as T1
where T2.AvgScore<T1.AvgScore)+1 as ‘Rank‘
from
(select Sid,AVG(Score) as ‘AvgScore‘ from SC
group by Sid) as T2,
Student s
where s.Sid=T2.Sid
order by AvgScore desc
- 23、查詢各科成績前三名的記錄:(不考慮成績並列情況)
select sc.cid,c.cname,sc.sid,s.sname,sc.score
from student s,sc sc,course c
where sc.cid=c.cid and sc.sid=s.sid and sc.score in
(
select score from sc sc2
where sc.cid=sc2.cid
order by score desc
)
order by sc.cid,sc.Score desc
- 24、查詢每門課程被選修的學生數;
select sc.cid,max(c.cname) as ‘CNAME‘,count(distinct sc.sid) as ‘studentCount‘
from sc sc,course c
where sc.cid=c.cid
group by sc.cid;
- 25、查詢出只選修了一門課程的全部學生的學號和姓名;
select s.sid,s.sname
from student s
where s.sid in
(select sc.sid from sc sc
group by sc.sid
having count(distinct sc.cid)=3)
- 26、查詢男生、女生的人數
select COUNT(sid) as ‘BoysCount‘ from Student s where s.Ssex=‘男‘;
select COUNT(sid) as ‘GirlsCount‘ from Student s where s.Ssex=‘女‘
- 27、查詢姓“張”的學生名單;
select s.sid,s.Sname
from Student s
where s.Sname like ‘張%‘
- 28、查詢同名同姓學生名單,並統計同名人數;
select s.Sname,COUNT(Sname) as ‘SameCount‘
from Student s
group by s.Sname
having COUNT(Sname)>1
- 29、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列
select sc.cid,AVG(sc.Score) as ‘AvgScore‘
from SC sc
group by sc.cid
order by AvgScore asc,cid desc
- 30、查詢平均成績大於85的所有學生的學號、姓名和平均成績
select sc.sid,s.Sname,AVG(sc.Score) as ‘AvgScore‘
from Student s,SC sc
where s.sid=sc.sid
group by sc.sid,s.Sname
having AVG(sc.Score)>85
- 31、查詢課程名稱為“數學”,且分數低於60的學生姓名和分數
select s.Sname,sc.Score from Student s,SC sc,Course c
where s.Sid=sc.Sid and sc.Cid=c.Cid and c.Cname=‘數學‘ and sc.Score<60
- 32、查詢所有學生的選課情況;
select s.Sid,s.Sname,c.Cid,c.Cname from Student s,SC sc,Course c
where s.Sid=sc.Sid and c.Cid=sc.Cid
order by s.Sid
- 33、查詢任何一門課程成績在70分以上的姓名、課程名稱和分數;
select distinct s.sid,s.sname,c.cname,sc.score
from student s,sc sc,course c
where s.sid=sc.sid and sc.cid=c.cid and sc.score>=70;
- 34、查詢不及格的課程,並按課程號從大到小排列;
select distinct sc.cid,c.cname from sc sc,course c
where sc.cid=c.cid and sc.score<60
order by sc.cid desc;
- 35、查詢課程編號為003且課程成績在80分以上的學生的學號和姓名
select sc.sid,s.sname,sc.score
from student s,sc sc
where s.sid=sc.sid and sc.cid=‘003‘ and sc.score>=80;
- 36、求選了課程的學生人數(超簡單的一題)
select count(distinct sid) as ‘studentCount‘ from sc;
- 37、查詢選修“楊艷”老師所授課程的學生中,成績最高的學生姓名及其成績;
select s.sid,s.sname,sc.score
from student s,sc sc,course c,teacher t
where s.sid=sc.sid and sc.cid=c.cid and c.tid=t.tid and t.tname=‘楊艷‘
and sc.score=
(
select max(sc2.score) from sc sc2 where sc.cid=sc2.cid
)
文章轉載自https://www.cnblogs.com/edisonchou/p/3878135.html
歡迎加入數據分析交流群(加群備註博客園)
搞定SQL面試(2)