搞定SQL面试
Posted linxiaochi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搞定SQL面试相关的知识,希望对你有一定的参考价值。
- 38、查询各个课程及相应的选修人数;
select sc.cid,c.cname,count(distinct sid) as ‘stuCount‘ from sc sc,course c
where sc.cid=c.cid
group by sc.cid,c.cname;
- 39、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;
select distinct sc1.sid,sc1.cid,sc1.score from sc sc1,sc sc2
where sc1.cid!=sc2.cid and sc1.score=sc2.score
order by sc1.score asc;
- 40、查询每门课程成绩最好的前两名;
select sc.cid,c.cname,sc.sid,s.sname,sc.score from student s,sc sc,course c
where s.sid=sc.sid and sc.cid=c.cid and sc.score in
(
select sc2.score from sc sc2
where sc2.cid=sc.cid
order by sc2.score desc limit 2
)
order by sc.cid;
- 41、统计每门课程的学生选修人数(超过10人的课程才统计)。
要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select sc.Cid,COUNT(distinct Sid) as ‘StuCount‘ from SC sc
group by sc.Cid
having COUNT(distinct Sid)>=10
order by StuCount desc,sc.Cid asc
- 42、检索至少选修两门课程的学生学号;
select distinct sc.sid from sc sc
group by sc.sid
having count(sc.cid)>=2;
- 43、查询全部学生都选修的课程的课程号和课程名;
select sc.cid,c.cname from sc sc,course c
where sc.cid=c.cid
group by sc.cid,c.cname
having count(sc.sid)=(select count(distinct s.sid) from student s)
- 44、查询没学过“叶平”老师讲授的任一门课程的学生姓名;
select s.sname from student s where s.sid not in
(
select sc.sid from sc sc,course c,teacher t
where sc.cid=c.cid and c.tid=t.tid and t.tname=‘叶平‘
)
- 45、查询两门以上不及格课程的同学的学号及其平均成绩
select sc.sid,avg(ifnull(sc.score,0)) as ‘AvgScore‘ from sc sc
where sc.sid in
(select sc2.sid from sc sc2
where sc2.score<60
group by sc2.sid
having count(sc2.cid)>2
)
group by sc.sid;
- 46、检索“004”课程分数小于60,按分数降序排列的同学学号;(很简单的一题)
select sc.Sid from SC sc
where sc.Cid=‘004‘ and sc.Score<60
order by sc.Score desc
文章转载自 https://www.cnblogs.com/edisonchou/p/3878135.html
欢迎加入数据分析交流群(加群备注博客园)
以上是关于搞定SQL面试的主要内容,如果未能解决你的问题,请参考以下文章