Mysql经典面试题20道
SQL语句在工作与面试时都必不可少,下面我整理了20道题目供大家练习,常见的使用方法和开窗函数都有考察,来测测你的sql技能是否过关。
一、创建表
共有4个表,分别是学生信息表、课程表、老师信息表和成绩表。
1 学生信息表
--建表语句CREATE TABLE Student ( SID VARCHAR (10), Sname VARCHAR (10), Sage datetime, Ssex VARCHAR (10))--插入测试数据INSERT INTO Student VALUES('01' , '赵雷' , '1990-01-01' , '男')INSERT INTO Student VALUES('02' , '钱电' , '1990-12-21' , '男')INSERT INTO Student VALUES('03' , '孙风' , '1990-05-20' , '男')INSERT INTO Student VALUES('04' , '李云' , '1990-08-06' , '男')INSERT INTO Student VALUES('05' , '周梅' , '1991-12-01' , '女')INSERT INTO Student VALUES('06' , '吴兰' , '1992-03-01' , '女')INSERT INTO Student VALUES('07' , '郑竹' , '1989-07-01' , '女')INSERT INTO Student VALUES('08' , '王菊' , '1990-01-20' , '女')
2 课程表
--建表语句CREATE TABLE Course ( CID VARCHAR (10), Cname VARCHAR (10), TID VARCHAR (10))--插入测试数据INSERT INTO Course VALUES('01' , '语文' , '02')INSERT INTO Course VALUES('02' , '数学' , '01')INSERT INTO Course VALUES('03' , '英语' , '03')
3 老师信息表
--建表语句CREATE TABLE Teacher ( TID VARCHAR (10), Tname varchar (10))--插入测试数据INSERT INTO Teacher VALUES('01' , '张三')INSERT INTO Teacher VALUES('02' , '李四')INSERT INTO Teacher VALUES('03' , '王五')
4 成绩表
--建表语句CREATE TABLE SC ( SID VARCHAR (10), CID VARCHAR (10), score int(5))--插入测试数据INSERT INTO SC VALUES('01' , '01' , 80)INSERT INTO SC VALUES('01' , '02' , 90)INSERT INTO SC VALUES('01' , '03' , 99)INSERT INTO SC VALUES('02' , '01' , 70)INSERT INTO SC VALUES('02' , '02' , 60)INSERT INTO SC VALUES('02' , '03' , 80)INSERT INTO SC VALUES('03' , '01' , 80)INSERT INTO SC VALUES('03' , '02' , 80)INSERT INTO SC VALUES('03' , '03' , 80)INSERT INTO SC VALUES('04' , '01' , 50)INSERT INTO SC VALUES('04' , '02' , 30)INSERT INTO SC VALUES('04' , '03' , 20)INSERT INTO SC VALUES('05' , '01' , 76)INSERT INTO SC VALUES('05' , '02' , 87)INSERT INTO SC VALUES('06' , '01' , 31)INSERT INTO SC VALUES('06' , '03' , 34)INSERT INTO SC VALUES('07' , '02' , 89)INSERT INTO SC VALUES('07' , '03' , 98)
二、练习题
Q1:查询「李」姓老师的数量
SELECTcount( * ) 李老师数据 FROMteacher WHERETname LIKE '李%'
Q2:查询存在成绩的学生信息
--方法1:SELECT DISTINCTs.* FROMsc gJOIN student s ON g.sid = s.sid WHEREg.score IS NOT NULL--方法2:SELECT* FROMstudent WHEREsid IN ( SELECT sid FROM sc WHERE sc.score IS NOT NULL )
Q3:查询不存在"01"课程但存在"02"课程的情况
SELECT* FROMsc WHEREcid = '02' AND SID NOT IN ( SELECT SID FROM SC WHERE CID = '01' )
Q4:查询"01"课程比"02"课程成绩高的学生的信息及课程分数
SELECTA.*,B.score FROMStudent AJOIN ( SELECT * FROM SC WHERE CID = '01' ) B ON A.SID = B.SIDJOIN ( SELECT * FROM SC WHERE CID = '02' ) C ON C.SID = B.SID WHEREB.score > C.score
Q5:查询存在"01"课程但可能不存在"02"课程的情况(不存在时显示为null)
SELECT* FROMstudent sJOIN ( SELECT * FROM sc WHERE cid = '01' ) g1 ON s.SID = g1.sidLEFT JOIN ( SELECT * FROM sc WHERE cid = '02' ) g2 ON s.SID = g2.sid
Q6:查询学过「张三」老师授课的同学的信息
SELECTs.*,T.Tname FROMstudent sJOIN SC G ON S.SID = G.SIDJOIN course C ON G.CID = C.CIDJOIN teacher T ON C.TID = T.TID WHERETNAME = '张三'
Q7:查询没学过"张三"老师讲授的任一门课程的学生姓名
SELECTs.Sname FROMstudent s WHEREsid NOT IN (SELECTg.sid FROMsc gJOIN course c ON g.cid = c.cidJOIN teacher t ON t.tid = c.tid WHEREt.tname = '张三' )
Q8:查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
SELECTc.Sname,c.score,d.Cname FROM(SELECTa.sid,a.Sname,b.score,b.cid FROMstudent aJOIN ( SELECT sid, cid, score FROM sc WHERE score > 70 ) b ON a.sid = b.sid ) cJOIN course d ON d.cid = c.cid
Q9:查询各科成绩最高分、最低分和平均分
SELECTcid,max( SCORE ),min( score ),AVG( SCORE ) FROMsc GROUP BYCID
Q10:查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为null)
SELECTs.SID,s.Sname,sum( g.score ),COUNT( g.cid ) FROMstudent sLEFT JOIN sc g ON s.sid = g.sid GROUP BYs.SID
Q11:查询没有学全所有课程的同学的信息
SELECTs.*,count( g.cid ) FROMstudent sJOIN sc g ON s.sid = g.SID GROUP BYs.sid HAVINGcount( g.cid ) < ( SELECT count( * ) FROM course)
Q12:检索"01"课程分数小于 60,按分数降序排列的学生信息
SELECTs.*,g.score FROMstudent sRIGHT JOIN ( SELECT sid, score FROM sc WHERE score < 60 AND cid = '01' ) g ON s.sid = g.SID ORDER BYg.score DESC
Q13:查询出只选修两门课程的学生学号和姓名
SELECTsid,sname FROMstudent WHEREsid IN ( SELECT sid FROM sc GROUP BY sid HAVING count( CID ) = 2 )
Q14:查询平均成绩大于等于85分的所有学生的学号、姓名和平均成绩
SELECTa.*,b.平均成绩 FROMstudent aJOIN ( SELECT sid, avg( score ) 平均成绩 FROM sc GROUP BY sid HAVING avg( score ) >= 85 ) b ON a.sid = b.sid
Q15:查询课程名称为「数学」,且分数低于60的学生姓名和分数
SELECT* FROMstudent aJOIN (SELECTg.sid,c.Cname,g.score FROMsc gJOIN course c ON g.cid = c.cid WHEREc.Cname = '数学' AND g.score < 60 ) b ON a.SID = b.SId
Q16:按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
SELECTSID,avg( score ),MAX( CASE WHEN CID = '01' THEN score ELSE 0 END ) '01',MAX( CASE WHEN CID = '02' THEN score ELSE 0 END ) '02',MAX( CASE WHEN CID = '03' THEN score ELSE 0 END ) '03' FROMsc GROUP BYSID ORDER BYavg( score ) DESC
Q17:展示每位同学的各科成绩,并将各科成绩排名
SELECT*,RANK ( ) over ( PARTITION BY sid ORDER BY score DESC ) 排名 FROMSC
Q18:查询学生的总成绩,并进行排名
SELECTsid,a.总成绩,rank ( ) over ( ORDER BY a.总成绩 DESC ) 总成绩排名 FROM( SELECT SID, sum( score ) 总成绩 FROM sc GROUP BY sid ) a
Q19:查询各科成绩前三名的记录
SELECT* FROM( SELECT CID, score, RANK ( ) OVER ( PARTITION BY CID ORDER BY SCORE DESC ) 排名 FROM SC ) a WHEREa.排名 <4
Q20:查询每门功课成绩最好的前两名
SELECTa.* FROM( SELECT cid, score, rank ( ) over ( PARTITION BY cid ORDER BY score DESC ) 排名 FROM sc ) a WHEREa.排名 <= 2
来源地址:https://blog.csdn.net/Antai_ZHU/article/details/130507320
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341