MYSQL(经典50题)

-- 1 查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数

-- 思路:1)某学生同时学了 01 和 02 课程2)某学生只学习了01课程,02 成绩为null

SELECT

*

FROM student a

INNER JOIN score b

ON a.sid=b.sid

INNER JOIN score c ON

a.sid=c.sid and b.Cid='01'and c.cid='02'

WHERE b.score>c.score;

-- 1.1查询同时选修01和02课程的情况

SELECT *

FROM score a

inner JOIN score b

on a.sid=b.sid

WHERE a.cid='01' and b.cid='02'

-- 1.2查询存在" 01 "课程但可能不存在" 02 "课程的情况

SELECT * FROM

(SELECT * FROM score where Cid='01') a

LEFT JOIN

(SELECT * FROM score where Cid='02') b

on a.Sid=b.Sid

-- 或者

SELECT * FROM

score a

LEFT JOIN

score b

on a.Sid=b.Sid and b.Cid='02'

WHERE a.Cid='01'

-- 1.3查询不存在" 01 "课程但存在" 02 "课程的情况

SELECT *FROM score WHERE Sid not in (SELECT Sid where cid='01') and Cid='02'

-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩

SELECT s.sid,avg_score,Sname FROM student s

INNER JOIN

(select Sid,avg(score) AS avg_score

FROM score GROUP BY Sid

having avg(score)>=60 )b

on s.sid=b.sid

-- 3 查询SC表存在成绩的学生信息

select b.* from (SELECT Sid from score GROUP BY sid) a

left join student b

ON a.Sid =b.Sid

-- 4查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null

SELECT a.sid, a.Sname, b.c, b.su FROM student a

left JOIN

(SELECT Sid,count(cid) as c ,sum(score) as su

FROM score GROUP BY Sid ORDER BY Sid) b

on a.Sid=b.Sid

-- 4.1.查没有成绩的学生信息

SELECT Sid FROM score GROUP BY sid;

SELECT *FROM student where sid NOT in (SELECT Sid FROM score GROUP BY sid)

-- 5.查询「李」姓老师的数量

SELECT count(Tid)from teacher WHERE Tname LIKE '李%'

-- 6查询学过「张三」老师授课的同学的信息

-- student course

SELECT a.,tname FROM student aINNER JOIN(SELECT a.,tname from score a

INNER JOIN

(select a.*,Tname from course a

INNER JOIN teacher b

on a.Tid=b.Tid) b

on a.cid= b.Cid) b

ON a.Sid= b.Sid

where Tname='张三'

-- 7.查询没有学全所有课程的同学的信息

-- student score course SELECT a.*,ct from student a INNER JOIN (SELECT Sid ,count(cid) ct from score GROUP BY Sid HAVING ct< (SELECT count(cid) from course))b ON a.Sid= b.Sid

-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息

-- student score

SELECT DISTINCT a.* from student a

INNER join

(SELECT * from score where cid in(

SELECT Cid from score where sid='01'))b

on a.Sid= b.Sid

-- 9.查询和" 01 "号的同学学习的课程完全相同的其他同学的信息

-- -- student score

SELECT

b.*

from

(SELECT Sid from score WHERE Sid not in

(SELECT sid from score where cid not in (select cid from score WHERE sid= '01'))and sid !='01'

GROUP BY Sid

HAVING COUNT(cid)= (SELECT count(cid)from score WHERE Sid= '01'))a

INNER JOIN student b

ON a.Sid =b.Sid

-- 10. 查询没学过"张三"老师讲授的任一门课程的学生姓名

(SELECT a.* FROM course a

INNER JOIN

teacher b

on a.tid=b.Tid

WHERE Tname= '张三')-- tid

(select a.* from score a

inner join

(SELECT a.* FROM course a

INNER JOIN

teacher b

on a.tid=b.Tid

WHERE Tname= '张三')b

ON a.Cid = b.Cid )

SELECT

Sname

from student a

where Sid not in

(select Sid from score a

left join course b

on a.cid=b.cid

INNER JOIN teacher c

on b.tid=c.tid

WHERE Tname= '张三'

)

-- 11 查询有两门课程及其以上不及格同学的学号,姓名及其平均成绩

-- score student

select s.sid, s.sname, avg(score) FROM score sc,student s

where score

GROUP BY s.Sid,Sname

HAVING count(cid)>=2

-- 12. 检索" 01 "课程分数小于 60,按分数降序排列的学生信息

-- student score

SELECT * FROM student a

inner join

(SELECT Sid, score FROM score WHERE cid='01' and score

on a.Sid=b.Sid

ORDER BY b.score DESC

-- 13. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩?

-- score student

select a.sid, a.cid, a.score,avg FROM

(select a.sid,b.cid,b.score from student a

left join score b

ON a.sid=b.sid) a

left join

(select sid,avg(score) avg

from score GROUP BY sid)b

on a.sid=b.sid

GROUP BY sid

ORDER BY avg DESC

-- 14. 查询各科成绩最高分、最低分和平均分:

-- 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 (及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90)

-- 要求输出课程号和选修数,查询结果按人数降序排列,若人数相同,按课程号升序排列

select c.cid, Cname,max(score),MIN(score),avg(score),count(*) 选修人数,

(sum(if(score>60,1,0))/COUNT(Sid)) 及格率,

(sum(if(score>=70 AND score(sum(if(score>=70 AND score

(sum(if(score>90,1,0))/COUNT(Sid)) 优秀率

FROM score sc, course c

WHERE sc.cid=c.cid

GROUP BY sc.cid,c.Cname

ORDER BY 选修人数 desc,c.Cid asc

-- 15.1按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺?

--

SELECT sc.Sid sc.Cid, sc.score,

(SELECT count(DISTINCT a.score)from score a where sc.cid=a.Cid and sc.score

FROM score sc

ORDER BY sc.Cid ,sc.score DESC

-- 16 查询学生的总成绩,并进行排名,总分重复时保留名次空缺

SELECT *,

rank() over(ORDER BY sum(score) desc) as 排名

from

(select Sid,

sum(score)

from score group by Sid);

-- 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺

#窗口函数

select s.Sid,

sum(s.score) as sum_sco,

dense_rank() over(order by sum(s.score) desc) as 'rank'

from score s group by s.Sid;

-- 17统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比

SELECT a.,b.Cname FROM(SELECTCid,concat(sum(case when score>=0 and score)100,'%')as '[60-0]',concat(sum(case when score>=60 and score)100,'%')as '[70-60]',concat(sum(case when score>=70 and score)100,'%')as '[85-70]',concat(sum(case when score>85 and score)*100,'%')as '[100-85]'

FROM score GROUP BY Cid)a

LEFT JOIN

course b

on a.Cid =b.Cid;

-- 18 查询各科成绩前三名的记录

select a.*

from(select *, dense_rank() over(partition by Cid order by score desc)score_rank from score) a where score_rank

-- 查询每门课程被选修的学生数

-- cours score

SELECT cid,

count(*) as 学生数

from score GROUP BY cid

-- 查询出只选修两门课程的学生学号和姓名 -- student score SELECT b.sid,b.Sname from (select sid,count(*) as ct FROM score GROUP BY sid HAVING ct=2)a LEFT JOIN student b on a.Sid=b.sid

-- 查询男生、女生人数

SELECT Ssex, count(1)as num FROM student

GROUP BY Ssex

-- 22. 查询名字中含有「风」字的学生信息

select * from Student where Sname like '%风%';

-- 23.查询同名同性学生名单,并统计同名人数

SELECT a.*

from student a

INNER JOIN student b

on a.Sname =b.Sname and a.Ssex=b.Ssex and a.Sid!=b.Sid

-- 24. 查询 1990 年出生的学生名单

select * from Student where Sage like '1990%';

-- 25. 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列

select cid, avg(score) 平均成绩 FROM score

GROUP BY Cid

ORDER BY 平均成绩 desc ,Cid ASC

-- 26. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩

SELECT a.sid, a.Sname,avg_score FROM

(select sid, avg(score) avg_score FROM score

GROUP BY Sid

HAVING avg(score)>=85) b

LEFT JOIN student a

on a.sid=b.Sid

-- 27. 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 (三表联结)

select s.Sname,sc.score from Course c,score sc ,Student s

where Cname = '数学' and c.Cid=sc.Cid and sc.score

-- 28. 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)

select s.Sid,s.Sname,score from Student s left join score sc on s.Sid=sc.Sid;

-- 29. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数

select s.Sid,s.Sname,c.Cname,score from Student s ,score sc,Course c

where s.Sid=sc.Sid and sc.Cid = c.Cid and

s.Sid not in (select Sid from score where score

-- 30. 查询不及格的课程

select DISTINCT cid from score where score

-- 31. 查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名

select a.*,b.Sname FROM

(select Sid from score where cid='01' and score>=80 )a

left join

student b

ON a.sid=b.sid

-- 32. 求每门课程的学生人数

select Cid,count(*) from score group by Cid ;

-- 33. 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

-- 四表

select b.*,score,tname from score a left join student b ON a.Sid=b.Sid left join course c on a.Cid= c.Cid left join teacher t ON c.tid=t.Tid where tname= '张三' ORDER BY score desc LIMIT 1 -- 34. 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 SELECT a.* FROM (select b.*,a.score,t.tname, DENSE_RANK() over(ORDER BY score desc)as score_rank from score a left join student b ON a.Sid=b.Sid left join course c on a.Cid= c.Cid left join teacher t ON c.tid=t.Tid where tname= '张三')a WHERE score_rank=1; -- 35. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 (同一个学生的 ?) select DISTINCT b.* FROM score a inner join score b ON a.sid=b.sid AND a.cid!=b.cid and a.score=b.score -- 36. 查询每门功成绩最好的前两名 select *from (select *, DENSE_RANK() over (PARTITION by cid ORDER BY score desc )as score_rank from score)s WHERE score_rank <=2 -- 37. 统计每门课程的学生选修人数(超过 5 人的课程才统计)。

-- 38. 检索至少选修两门课程的学生学号

select Sid from score group by Sid

having count(distinct Cid) >=2;

-- 39. 查询选修了全部课程的学生信息(可以参考之前)

-- 40. 查询各学生的年龄,只按年份来算

select Sid,Sage, year(CURDATE())-YEAR(Sage)FROM student

-- 42. 查询本周过生日的学生?

select * from Student where week(Sage) = week(now());

-- 43. 查询下周过生日的学生?

select * from Student where week(Sage) = week(now())+1;

select * from Student where week(Sage) = week(date_add(now(),interval 1 week))

-- 44. 查询本月过生日的学生

select * from Student where month(Sage) = month(now());

-- 45. 查询下月过生日的学生

select * from Student where Month(Sage) = month(now())+1;

select * from Student where Month(Sage) =month(date_add(now(), interval 1 Month));

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值