50道mysql笔试题目及答案_50道SQL练习题及答案(MySQL版)

更新时间:2023-07-14 21:38:18 阅读: 评论:0

50道mysql笔试题⽬及答案_50道SQL练习题及答案(MySQL
版)
--1.查询" 01 "课程⽐" 02 "课程成绩⾼的学⽣的信息及课程分数
SELECT st.*, class1, class2 FROM student st JOIN(SELECT * FROM(SELECT SId s1, score class1 FROM sc WHERE CId =
'01') c1,
(SELECT SId s2, score class2 FROM sc WHERE CId = '02') c2WHERE s1 = s2 AND class1 >class2) cON st.SId =c.s1;--1.1
查询同时存在" 01 "课程和" 02 "课程的情况
SELECT * FROM((SELECT * FROM sc WHERE CId = '01') t1JOIN(SELECT * FROM sc WHERE CId = '02') t2ONt1.Sid=t2.Sid);--1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显⽰为 null )
SELECT * FROM((SELECT * FROM sc WHERE CId = '01') t1left JOIN(SELECT * FROM sc WHERE CId = '02')
t2ONt1.Sid=t2.Sid);--1.3 查询不存在" 01 "课程但存在" 02 "课程的情况
lect * fromscwhere sc.CId = '02' ANDsc.SIdNOT IN(SELECT SId FROM sc WHERE sc.CId = '01');--2.查询平均成绩⼤于等于
60 分的同学的学⽣编号和学⽣姓名和平均成绩
SELECT st.SId, Sname, AVG(score) 'avg' FROM student st JOIN sc s ON st.SId = s.SId GROUP BY st.SId HAVING avg >= 60;--3.查询在 SC 表存在成绩的学⽣信息源毒
SELECT * FROM student WHERE SId IN(SELECT DISTINCT SId FROMsc);--4.查询所有同学的学⽣编号、学⽣姓名、选课总数、所有课程的成绩总和
SELECT st.sid, st.Sname, COUNT(sc.CId) 'nums', SUM(sc.score) 'sum' FROMstudent stJOIN sc on st.SId =sc.SIdGROUP BYst.SId;--4.1显⽰没选课的学⽣(显⽰为NULL)
SELECT st.sid, st.Sname, CASE WHEN COUNT(sc.SId) > 0 THEN COUNT(sc.SId) ELSE NULL END 'nums', SUM(sc.score)
'sum' FROMstudent stleft JOIN sc on st.SId =sc.SIdGROUP BYst.SId;--4.2查有成绩的学⽣信息
热机的效率SELECT * FROM student WHERE SId IN (SELECT DISTINCT SId FROM sc); --适⽤于右表⼩
SELECT * FROM student st WHERE EXISTS (SELECT * FROM sc WHERE sc.SId = st.SId); --适⽤于右表⼤
--5.查询「李」姓⽼师的数量
SELECT COUNT(*) FROM teacher t WHERE t.Tname LIKE '李%';--6.查询学过「张三」⽼师授课的同学的信息
SELECT * FROM student st WHERE st.SId IN(SELECT SId from sc WHERE sc.CId =(SELECT CId FROM cour WHERE cour.TId =(SELECT TId FROM teacher WHERE teacher.Tname = '张三')));--7.查询没有学全所有课程的同学的信息
SELECT * FROM student WHERE SId NOT IN(SELECT SId FROM sc GROUP BY SId HAVING COUNT(*) = (SELECT COUNT(*) FROMcour));--8.查询⾄少有⼀门课与学号为" 01 "的同学所学相同的同学的信息
SELECT * FROM student WHERE SId IN (SELECT SId FROM sc WHERE CId IN (SELECT CId FROM sc WHERE SId = '01'));--9.查询和" 01 "号的同学学习的课程完全相同的其他同学的信息--解法
SELECT * FROM student WHERE SId IN(SELECT SId FROM sc GROUP BY SId HAVING SId <> '01' AND
GROUP_CONCAT(CId ORDER BY CId) =(SELECT GROUP_CONCAT(CId ORDER BY CId) FROM sc WHERE SId = '01'));--解法⼆
SELECT * FROM student WHERE SId IN(SELECT SId FROM sc WHERE CId IN (SELECT CId FROM sc WHERE SId = '01')
AND SId <> '01'
GROUP BYSIdHAVING COUNT(*) = (SELECT COUNT(*) FROM sc WHERE SId = '01'));--10.查询没学过"张三"⽼师讲授的任⼀门课程的学⽣姓名
SELECT student.Sname FROM student WHERE SId NOT IN(SELECT SId FROM sc WHERE CId in(SELECT cour.CId FROM cour WHERE TId =(SELECT teacher.TId FROM teacher WHERE tname = '张三')));--11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
SELECT st.SId, sname, avg(score) FROM student st JOIN sc ON st.sid = sc.SId GROUP BYst.SIdHAVING COUNT(score <=
60 OR NULL) >= 2;--错误解法:
lect student.sid, student.sname, AVG(sc.score) fromstudent,scwherestudent.sid= sc.sid and sc.score< 60防拐骗安全教育ppt
group bysc.sidhaving count(*)>1;--12.检索" 01 "课程分数⼩于 60,按分数降序排列的学⽣信息
SELECT st.*, scoreFROM student st JOINscON st.SId = sc.SId AND sc.CId = '01' AND sc.score < 60
ORDER BY score DESC;--13.按平均成绩从⾼到低显⽰所有学⽣的所有课程的成绩以及平均成绩
SELECT * from sc JOIN(SELECT SId, avg(score) avg FROM sc GROUP BYSId) avgsON sc.SId =avgs.SIdORDER BY avgs.avg desc;--14.查询各科成绩最⾼分、最低分和平均分--以如下形式显⽰:课程 ID,课程 name,最⾼分,最低分,平均分,及格率,中等率,优良率,优秀率--及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90--要求输出课程号和选修⼈数,查询结果按⼈数降序排列,若⼈数相同,按课程号升序排列
SELECTsc.CId'课程 ID',count(*) '选修⼈数',
cour.Cname'课程 name',MAX(score) '最⾼分',MIN(score) '最低分',AVG(score) '平均分',COUNT(score >= 60 OR NULL) / COUNT(*) '及格率',COUNT(score >= 70 AND score < 80 OR NULL) / COUNT(*) '中等率',COUNT(score >= 80 AND score < 90 OR NULL) / COUNT(*) '优良率',COUNT(score >= 90 OR NULL) / COUNT(*) '优秀率'
组织结构图FROM sc join cour ON sc.CId = cour.CId GROUP BYsc.CIdORDER BY COUNT(*) desc, sc.CId;--15.按各科成绩进⾏排序,并显⽰排名, Score 重复时保留名次空缺
SELECT CId, SId, score , (SELECT COUNT(*) FROM sc WHERE CId = o.CId AND score > o.score) + 1rankFROM sc o ORDER BY CId, score DESC;--16.查询学⽣的总成绩,并进⾏排名,总分重复时不保留名次空缺
SET @rank = 0;SELECT q.sid, q.sum, (@rank := @rank + 1) rank FROM(SELECT sid, sum(score) sum FROM sc GROUP BY SId ORDER BY sum) q;--17.统计各科成绩各分数段⼈数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分⽐
SELECTs.CId, c.Cname,
高二历史CONCAT(COUNT(score >= 85 OR NULL), ',',COUNT(score >= 85 OR NULL) / COUNT(*)) '[100-85]',COUNT(score < 85 AND score >= 70 OR NULL) '[85-70]',COUNT(score < 70 AND score >= 60 OR NULL) '[70-60]',COUNT(score < 60 OR NULL)
'[60-0]'
FROM sc s JOIN cour c ON s.CId = c.CId GROUP BYs.CId;--18. 查询各科成绩前三名的记录
SELECT o.* FROM sc o HAVING(SELECT COUNT(*) FROM sc WHERE o.CId = CId AND o.score < score) + 1 <= 3
ORDER BY CId, score DESC, SId;--19.查询每门课程被选修的学⽣数
SELECT CId, COUNT(*) FROM sc GROUP BYCId;--20.查询出只选修两门课程的学⽣学号和姓名
SELECTst.SId, st.SnameFROM student st JOIN sc s ON st.SId = s.SId GROUP BY st.SId HAVING COUNT(*) = 2;--21.查询男⽣、⼥⽣⼈数
lect sx, count(*) fromstudentgroup bysx;--22.查询名字中含有「风」字的学⽣信息
形容瘦的成语SELECT * FROM student WHERE Sname LIKE '%风%';--23.查询同名学⽣名单,并统计同名⼈数
举箸提笔
SELECT st.*, (SELECT COUNT(*) FROM student WHERE Sname = st.Sname) 'same' FROM student st WHERE st.SId
IN(SELECT s1.SId FROM student s1 JOIN student s2 ON s1.SId != s2.SId AND s1.Sname =s2.Sname);--24.查询 1990 年出⽣的学⽣名单
SELECT * FROM student WHERE YEAR(Sage) = 1990;--25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
SELECT CId, avg(score) avg
FROM sc GROUP BY CId ORDER BY avg DESC, CId;--26.查询平均成绩⼤于等于 85 的所有学⽣的学号、姓名和平均成绩
SELECT st.SId, Sname, AVG(score) avg
FROM student st JOIN sc s ON st.SId = s.SId GROUP BY st.SId HAVING avg >= 85;--27.查询课程名称为「数学」,且分数低于60 的学⽣姓名和分数
SELECTst.Sname, s.scoreFROMstudent st, sc s, cour cWHERE st.SId = s.SId AND s.CId = c.CId AND s.score < 60 AND
c.Cname = '数学';--28.查询所有学⽣的课程及分数情况(存在学⽣没成绩,没选课的情况)
SELECTSname, st.SId, scoreFROM student st LEFT JOIN sc s ON st.SId =s.SId;--29.查询任何⼀门课程成绩在 70 分以上的姓名、课程名称和分数--这个我没太理解,理解⼀是任意⼀门成绩均在70分以上
SELECTst.Sname, c.Cname, s.scoreFROM student st, sc s, cour c WHERE st.SId = s.SId AND s.cid = c.cid AND st.SId
三年级数学上册期末考试试卷
IN(SELECT sid FROM sc GROUP BY SId HAVING MIN(score) > 70);--理解⼆是存在⼀门成绩在70分以上即可满⾜条件
SELECTst.Sname, c.Cname, s.scoreFROM student st, sc s, cour c WHERE st.SId = s.SId AND s.cid = c.cid AND st.SId
IN(SELECT sid FROM sc GROUP BY SId HAVING MAX(score) > 70);--理解三就是找出所有⼤于70分的得分。
lect student.sname, courame,sc.score fromstudent,cour,scwhere sc.score>70
and student.sid =sc.sidand sc.cid =cour.cid;SELECT * FROM sc WHERE score > 70
--30.查询存在不及格的课程
SELECT cid FROM sc GROUP BY CId HAVING MIN(score) < 60;lect cid fromscwhere score< 60
group bycid;SELECT DISTINCT cid FROM sc WHERE score < 60;--31.查询课程编号为 01 且课程成绩在 80 分及以上的学⽣的学号和姓名
SELECT sid, sname FROM student WHERE sid IN(SELECT SId FROM sc WHERE CId = '01' AND score >= 80);--32.求每门课程的学⽣⼈数
SELECT cid, count(*) FROM sc GROUP BYcid;--33.成绩不重复,查询选修「张三」⽼师所授课程的学⽣中,成绩最⾼的学⽣信息及其成绩
SELECT st.*, score FROM student st JOIN sc s ON st.SId = s.SId AND s.CId =(SELECT CId FROM cour WHERE TId = (SELECT TId FROM teacher WHERE Tname = '张三')) ORDER BY score DESC LIMIT 1;--34.成绩有重复的情况下,查询选修「张三」⽼师所授课程的学⽣中,成绩最⾼的学⽣信息及其成绩
SELECT st.*, score, CId FROM student st JOIN sc s ON st.SId = s.SId AND s.CId =(SELECT CId FROM cour WHERE TId = (SELECT TId FROM teacher WHERE Tname = '张三'))WHERE score
= (SELECT max(score) FROM sc WHERE CId =s.CId);--35.查询不同课程成绩相同的学⽣的学⽣编号、课程编号、学⽣成绩--这个问题其实⼀开始没太明⽩啥意思,后来理解为某个⼈的⼏科分数是⼀样的,需要把这个⼈找出来
lect a.cid, a.sid, a.score from sc asainner joinscasbon a.sid =b.sidand a.cid !=b.cidand a.score =b.scoregroup bycid, sid;--36.查询每门功成绩最好的前两名
SELECT o.* FROM sc o HAVING (SELECT COUNT(*) FROM sc WHERE CId = o.CId AND score > o.score) + 1 <= 2 ORDER BYo.cid, sid;--37.统计每门课程的学⽣选修⼈数(超过 5 ⼈的课程才统计)
SELECT CId, COUNT(*) sum FROM sc GROUP BY CId HAVING sum > 5;--38.检索⾄少选修两门课程的学⽣学号
SELECT SId FROM sc GROUP BY SId HAVING COUNT(*) >= 2;--39.查询选修了全部课程的学⽣信息
SELECT * FROM student WHERE sid IN(SELECT SId FROM sc GROUP BY SId HAVING COUNT(*) = (SELECT COUNT(*) FROMcour));--40.查询各学⽣的年龄,只按年份来算
SELECT SId, Sname, Sage, YEAR(NOW()) - YEAR(Sage)FROMstudent;--41.按照出⽣⽇期来算,当
前⽉⽇ < 出⽣年⽉的⽉⽇则,年龄减⼀
SELECT SId '学⽣编号', Sname '学⽣姓名', TIMESTAMPDIFF(YEAR, Sage, NOW()) '学⽣年龄'
FROMstudent;--42.查询本周过⽣⽇的学⽣--有点复杂,需要拼接出本周的起⽌⽇期
SELECT * FROMstudentWHERE DATE(CONCAT(YEAR(NOW()),'-',MONTH(Sage), '-', DAY(Sage)))BETWEEN
DATE(DATE_SUB(NOW(),INTERVAL WEEKDAY(NOW()) DAY))AND DATE(DATE_ADD(NOW(),INTERVAL 6 -
WEEKDAY(NOW()) DAY));SELECT * FROMstudent--43. 查询下周过⽣⽇的学⽣--同42
SELECT * FROMstudentWHERE DATE(CONCAT(YEAR(NOW()),'-',MONTH(Sage), '-', DAY(Sage)))BETWEEN
DATE(DATE_SUB(NOW(),INTERVAL WEEKDAY(NOW()) - 7 DAY))AND DATE(DATE_ADD(NOW(),INTERVAL 13 -
WEEKDAY(NOW()) DAY));--44.查询本⽉过⽣⽇的学⽣
SELECT * FROM student WHERE month(sage) = month(NOW())--45.查询下⽉过⽣⽇的学⽣--注意本⽉是12⽉的话,下⼀个⽉份是1即可
SELECT * FROM student WHERE month(Sage) = (CASE WHEN month(NOW()) = 12 THEN 1 ELSE MONTH(NOW()) + 1 END);

本文发布于:2023-07-14 21:38:18,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1096757.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:课程   成绩   查询   同学   选修   存在   姓名   信息
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图