SQL:经典面试50题

SQL经典面试50题

非标准答案,SQL语句欢迎留言讨论

 40题按年份计算年龄,42题跨年查询暂未解决,有写出来的朋友还请赐教!!

 

表结构

学生表
Student(SId,Sname,Sage,Ssex)
-- SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
课程表 Course(CId,Cname,TId) -- CId 课程编号,Cname 课程名称,TId 教师编号

教师表 Teacher(TId,Tname) -- TId 教师编号,Tname 教师姓名
成绩表 SC(SId,CId,score) -- SId 学生编号,CId 课程编号,score 分数

 

SQL经典面试50题

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

    1.1 查询同时存在" 01 "课程和" 02 "课程的情况

    1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )

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

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

  2. 查询在 SC 表存在成绩的学生信息

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

    4.1 查有成绩的学生信息

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

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

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

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

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

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

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

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

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

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

    以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率

    及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90

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

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

        15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次

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

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

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

  2. 查询各科成绩前三名的记录

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

  4. 查询出只选修两门课程的学生学号和姓名

  5. 查询男生、女生人数

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

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

  8. 查询 1990 年出生的学生名单

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

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

  11. 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数

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

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

  14. 查询不及格的课程

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

  16. 求每门课程的学生人数

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

  18. 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

  19. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩

  20. 查询每门功成绩最好的前两名

  21. 统计每门课程的学生选修人数(超过 5 人的课程才统计)。

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

  23. 查询选修了全部课程的学生信息

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

  25. 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一

  26. 查询本周过生日的学生

  27. 查询下周过生日的学生

  28. 查询本月过生日的学生

  29. 查询下月过生日的学生

创建表&插入数据

#create
CREATE TABLE student(sid VARCHAR(10),sname VARCHAR(10),sage datetime,ssex VARCHAR(10));#学生表
CREATE TABLE course(cid VARCHAR(10),cname VARCHAR(10),tid VARCHAR(10));#科目表
CREATE TABLE teacher(tid VARCHAR(10),tname VARCHAR(10));#教师表
CREATE TABLE score(sid VARCHAR(10),cid VARCHAR(10),score DECIMAL(5,2));#成绩表
#DECIMAL(5,2)规定了存储的值将不会超过5位数字,开且小数点后面有2位数字。

注:未避免占用篇幅,测试数据插入放在文章最后

 

练习题(采用分步法)

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

 思路:①构建 学生编号+课程01成绩+课程02成绩 的表 ②根据查询到的" 01 "课程比" 02 "课程成绩高的学生ID查询对应学生信息

-- ①构建 学生编号+课程01成绩+课程02成绩 的表
SELECT a.sid FROM
(SELECT sid,cid,score FROM score WHERE cid=01) AS a
INNER JOIN
(SELECT sid,cid,score FROM score WHERE cid=02) AS b
ON a.sid = b.sid
WHERE a.score > b.score;

-- ②查询对应学生姓名及成绩
SELECT a.sid,c.sname,c.ssex,c.sage,a.score as ascore,b.score as bscore FROM
(SELECT sid,cid,score FROM score WHERE cid=01) AS a
INNER JOIN
(SELECT sid,cid,score FROM score WHERE cid=02) AS b
ON a.sid = b.sid
INNER JOIN student AS c ON c.sid = a.sid
WHERE a.score > b.score;

查询结果:

SQL:经典面试50题

 

#查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
SELECT * FROM student RIGHT JOIN (
SELECT t1.sid,class1,class2 FROM
(SELECT sid,score as class1 FROM Score WHERE Score.cid = 01)as t1,
(SELECT sid,score as class2 FROM Score WHERE Score.cid = 02)as t2
WHERE t1.sid = t2.sid AND t1.class1 > t2.class2)r
ON student.sid = r.sid;

SELECT c.sid,c.sname,c.sage,c.ssex,s.score01,s.score02 FROM (
SELECT a.sid,a.score as score01,b.score as score02 FROM
(SELECT sid,score FROM Score WHERE Score.cid = 01)as a,
(SELECT sid,score FROM Score WHERE Score.cid = 02)as b
WHERE a.sid = b.sid AND a.score > b.score) AS s
LEFT JOIN student AS c
ON c.sid = s.sid;

 

1.1 查询同时存在" 01 "课程和" 02 "课程的情况

#查询同时存在" 01 "课程和" 02 "课程的情况(字段去重及字段重命名只需将*替换为字段)
SELECT * FROM 
(SELECT * FROM score  WHERE cid = 01) AS a
INNER JOIN
(SELECT * FROM score  WHERE cid = 02) AS b
ON
a.sid = b.sid;

 

1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )

#查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null ),201921只有课程1无课程2
SELECT * FROM 
(SELECT * FROM score  WHERE cid = 01) AS a
LEFT JOIN
(SELECT * FROM score  WHERE cid = 02) AS b
ON
a.sid = b.sid;-- LEFT JOIN左连接查询结果显示左表中所有数据

 

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

SELECT * FROM score sc
WHERE sc.sid NOT IN (SELECT sid FROM score s2  WHERE s2.cid = 01) AND sc.cid = 02;-- 正确(5条记录)

错误语句:

SQL:经典面试50题

 

 

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

#①查询平均分大于等于60
SELECT AVG(sc.score),sc.sid FROM score AS sc GROUP BY sc.sid HAVING AVG(sc.score)>=60;
#②查询学生信息
SELECT * FROM student s,(SELECT AVG(sc.score),sc.sid FROM score AS sc GROUP BY sc.sid HAVING AVG(sc.score)>=60) scs WHERE s.sid = scs.sid;
#②查询学生信息(去重字段)
SELECT 
S.sid,S.sname,S.sage,S.ssex,scs.avgscore 
FROM 
student s,
(SELECT AVG(sc.score) AS avgscore,sc.sid FROM score AS sc GROUP BY sc.sid HAVING AVG(sc.score)>=60) scs 
WHERE s.sid = scs.sid;

 

3.查询在 Score 表存在成绩的学生信息

#201922无成绩信息
SELECT * FROM student s WHERE s.sid IN (SELECT sc.sid FROM score sc);
SELECT * from student S,Score SC where S.sid = SC.sid;

#SELECT DISTINCT 列名称 FROM 表名称  返回唯一不同的值(去重)
SELECT DISTINCT S.* from student S,Score SC where S.sid = SC.sid;

#查询有成绩的学生信息
#IN()适合B表比A表数据小的情况;EXISTS()适合B表比A表数据大的情况
SELECT * from student S WHERE S.sid in (SELECT SC.sid from score SC);
SELECT * from student S where exists (SELECT SC.sid from score SC WHERE S.sid = SC.sid);

 

4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的成绩总和

SELECT 
    s.sid,s.sname,scs.countcourse,scs.sumscore 
FROM 
    student s,
    (SELECT sc.sid,COUNT(sc.cid) as countcourse,SUM(sc.score) as sumscore FROM score sc GROUP BY sc.sid) scs WHERE s.sid = scs.sid;-- select中的列要与group by的分组列对应

 

#没选课的学生信息也显示,并按选课总数降序排序
SELECT s.sid,s.sname,scs.countcourse,scs.sumscore 
FROM student s 
LEFT JOIN 
(SELECT sc.sid,COUNT(sc.cid) as countcourse,SUM(sc.score) as sumscore FROM score sc GROUP BY sc.sid) scs
ON s.sid = scs.sid
ORDER BY scs.countcourse DESC;

 

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

SELECT count(t.tid) FROM teacher t WHERE t.tname LIKE "李%";

 

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

#①查询张三老师的课程
select DISTINCT(c.cid),c.cname from course C,teacher T where C.tid = T.tid and T.tname = "张三";
#②查询对应学生信息
SELECT DISTINCT S.* from student S,score SC,course C,teacher T WHERE
T.tid = C.tid and C.cid = SC.cid and SC.sid = S.sid and T.tname = "张三";

 

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

SELECT * from student S where S.sid not in
(SELECT SC.sid from score SC GROUP BY SC.sid 
having count(SC.cid) = (select count(cid) from course));

 

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

 SELECT * FROM student s,score sc WHERE s.sid = sc.sid AND sc.cid 
 IN (SELECT sc.cid FROM score sc WHERE sc.sid = "201921");-- 201921 只学了01课程

 

 9.查询和" 201923 "号(201924、201927)的同学学习的课程完全相同的其他同学的信息!!!!!!

 #方法1--查询错误:25、28--201925、201928在学生表中为重复值(删除重复值后查询无误)
# 1. 先找出201923的所有课程
# 2. 找出学过201923没有学过的课程的学生,排除
# 3. group by统计每个学生的所选课程数
# 4. group by后的筛选需要用having语句 筛选出课程数相同的
SELECT
 S.*,COUNT( * ) AS course_count 
FROM
 student S,score SC 
WHERE
 S.sid = SC.sid 
 AND S.sid NOT IN (
 SELECT DISTINCT S.sid FROM
  student S,score SC 
 WHERE
  S.sid = SC.sid 
  AND SC.cid NOT IN (SELECT SC.cid FROM score SC WHERE SC.sid = "201923") 
 ) 
GROUP BY
 s.sid 
HAVING
 course_count = (SELECT count( * ) FROM score sc WHERE sc.SId = 201923);
 #方法2:将课程ID拼接
# 1. 针对每个学生拼接出他的课程列表,作为临时表temp
# 2. 找出01号学生的课程列表字符串
# 3. 找出临时表temp中课程列表与01号相同且不为01号的sid
# 4. 从学生表中根据sid找出数据
#GROUP_CONCAT将group by产生的同一个分组中的值连接起来,返回一个字符串结果.语法:group_concat( [distinct] 要连接的字段 [order by 排序字段 asc/desc ] [separator ‘分隔符‘] )
SELECT 
DISTINCT * FROM student WHERE sid IN (   SELECT sid FROM   ( SELECT sc.sid, GROUP_CONCAT( sc.cid ORDER BY sc.cid ) courselist FROM score sc GROUP BY sc.sid ) temp   WHERE   courselist = ( SELECT group_concat( sc.cid ORDER BY sc.cid ) FROM score sc WHERE sc.SId = 201923 )   AND temp.sid != 201923   );

 

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

SELECT DISTINCT s.sname FROM student s,score sc WHERE s.sid = sc.sid AND sc.sid
NOT IN 
(SELECT sc.sid FROM course c,teacher t,score sc WHERE c.tid = t.tid AND t.tname = "张三" AND sc.cid = c.cid);

 

 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩(201926、201927)

#执行顺序:from-where-group by-having-select-order by
#从成绩表中选取score小于60的,并group by sid,having count 大于1
SELECT 
SC.sid,S.sname,avg(SC.score) 
FROM 
score SC,student S 
WHERE SC.sid = S.sid and SC.score < 60 
GROUP BY SC.sid 
HAVING count(SC.cid)>1;-- 28也在查询结果中:201925、201928在学生表中为重复值

 

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

SELECT DISTINCT s.*,sc.score FROM student s,score sc 
WHERE sc.score < 60 AND sc.cid = "02" AND s.sid = sc.sid 
ORDER BY sc.score DESC;

-- 查询最后可以设置排序方式,语法为ORDER BY ***** DESC\ASC(order by默认采用升序(asc),如果存在 where 子句,那么 order by 必须放到where 语句后面);DISTINCT作用于单列

 

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

SELECT sc.*,a.avgscore FROM score sc LEFT JOIN
(SELECT sc.sid,avg(sc.score) as avgscore FROM score sc GROUP BY sc.sid) a
ON sc.sid =a.sid
ORDER BY a.avgscore DESC;


#① sum,avg,count等等都属于统计函数,是需要经过分组之后才能统计出来的;
#② 平均数与原始数据需要同时显示:使用group by除统计函数之外,查询字段与分组字段必须一一对应
#select field1,field2,
count(*) from a group by field1,field2,若group by 后面的字段不全,则数据也不全 SELECT SC.sid,SC.cid,SC.score,avg(SC.score) as avgscore from score SC GROUP BY SC.sid,SC.cid,SC.score ORDER BY avgscore desc;-- 该语句计算的是单门课程的均值

 

14.查询各科成绩最高分、最低分和平均分,以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率.及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90.要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

SELECT
    SC.cid,
    max( SC.score ) AS 最高分,
    min( SC.score ) AS 最低分,
    avg( SC.score ) AS 平均分,
    count( * ) AS 选修人数,
    sum( CASE WHEN SC.score >= 60 THEN 1 ELSE 0 END ) / count( * ) AS 及格率,
    sum( CASE WHEN SC.score >= 70 AND SC.score < 80 THEN 1 ELSE 0 END ) / count( * ) AS 中等率,
    sum( CASE WHEN SC.score >= 80 AND SC.score < 90 THEN 1 ELSE 0 END ) / count( * ) AS 优良率,
    sum( CASE WHEN SC.score >= 90 THEN 1 ELSE 0 END ) / count( * ) AS 优秀率 
FROM
    score SC 
GROUP BY
    SC.cid 
ORDER BY
    count( * ) DESC,
    SC.cid ASC;

SQL:经典面试50题

 

 

15.按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺!!!!!!

#①使用窗口函数:row_number(),rank(),dense_rank()
#语法:rank() over(PARTITION BY 分组字段 ORDER BY 排序字段)
SELECT *,rank() over(PARTITION BY cid ORDER BY score DESC) as ran FROM score;

#②左连接方法
SELECT a.cid,a.sid,a.score,count(b.score)+1 as ran FROM score as a
LEFT JOIN score as b
ON a.score < b.score AND a.cid = b.cid
GROUP BY a.cid,a.sid,a.score
ORDER BY a.cid,ran ASC;-- 思路:比当前分数更高的有几个

SQL:经典面试50题

 

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

#方法一:使用窗口函数
SELECT 
    *,
    sum(score) as sscore,
    dense_rank() over(ORDER BY sum(score) DESC) as ran 
FROM score  
GROUP BY sid;

#方法二:使用自定义变量
#自定义变量: 申明变量:SET @crank =0;  对变量进行赋值:@crank := @crank +1   ,赋值操作符   =或:=   使用:查找,比较  运算等  ,      作用域:针对于当前会话(连接)有效,作用域同于会话变量
SET @crank=0;
SELECT q.sid,total,@crank:=@crank +1 as ran FROM
(SELECT sc.sid,sum(sc.score) as total FROM score sc
GROUP BY sc.sid
ORDER BY total DESC) as q;-- rank不可作为变量名

 

 SQL:经典面试50题

 

 

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

SELECT sc.cid,c.cname,count(*) as sump,
sum(CASE WHEN sc.score <= 100 and sc.score > 85 THEN 1 ELSE 0 END) as "[100-85]",
sum(CASE WHEN sc.score <= 85 and sc.score > 70 THEN 1 ELSE 0 END) as "[85-70]",
sum(CASE WHEN sc.score <= 70 and sc.score > 60 THEN 1 ELSE 0 END) as "[70-60]",
sum(CASE WHEN sc.score <= 60 THEN 1 ELSE 0 END) as "[60-0]" ,
sum(CASE WHEN sc.score <= 100 and sc.score > 85 THEN 1 ELSE 0 END)/count(*) as p1,
sum(CASE WHEN sc.score <= 85 and sc.score > 70 THEN 1 ELSE 0 END)/count(*) as p2,
sum(CASE WHEN sc.score <= 70 and sc.score > 60 THEN 1 ELSE 0 END)/count(*) as p3,
sum(CASE WHEN sc.score <= 60 THEN 1 ELSE 0 END)/count(*) as p4
FROM score sc 
LEFT JOIN course c
ON sc.cid = c.cid
GROUP BY sc.cid;

SQL:经典面试50题

 

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

SELECT * FROM 
(SELECT sc.*,row_number() over(PARTITION BY cid ORDER BY score DESC) as ran FROM score sc) a
WHERE a.ran in(1,2,3);#非并列排名

 

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

SELECT c.cname,count(sc.sid) FROM course c,score sc WHERE sc.cid = c.cid GROUP BY sc.cid;

 

20.查询出只选修两门课程的学生学号和姓名

SELECT s.sname,s.sid FROM student s WHERE s.sid IN (SELECT sc.sid FROM score sc GROUP BY sc.sid HAVING count(sc.cid) = 2);-- 嵌套查询
SELECT s.sname,s.sid FROM student s,score sc WHERE s.sid = sc.sid GROUP BY sc.sid HAVING count(*) =2;-- 联合查询

 

21.查询男生、女生人数

SELECT ssex,count(*) as sn FROM student GROUP BY ssex;

 

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

SELECT * FROM student WHERE sname LIKE "%%";

 

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

SELECT sname,count(*) "同名人数" FROM student GROUP BY sname HAVING count(*) >1;

 

24.查询 2001 年出生的学生名单

SELECT * FROM student WHERE YEAR(sage) = 2001;

 

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

SELECT 
sc.cid,
avg(sc.score) as avgscore,
c.cname 
FROM score sc,course c 
WHERE sc.cid = c.cid 
GROUP BY sc.cid 
ORDER BY avg(sc.score) DESC,sc.cid ASC;

 

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

SELECT 
sc.sid,
avg(sc.score) as avgscore,
s.sname 
FROM score sc,student s 
WHERE sc.sid = s.sid 
GROUP BY sc.sid 
HAVING avgscore >= 85;-- having也可以用来截取结果表

 

27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数

SELECT s.sname,sc.score 
FROM student s,score sc,course c 
WHERE s.sid=sc.sid 
AND c.cid = sc.cid 
AND c.cname = "数学" 
AND sc.score <60;

 

28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况:李四、张三)

SELECT s.sname,sc.cid,sc.score FROM student s LEFT JOIN score sc ON s.sid = sc.sid;

 

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

SELECT s.sname,c.cname,sc.score FROM student s,course c,score sc WHERE s.sid=sc.sid AND c.cid=sc.cid AND sc.score>70;


30.查询存在不及格的课程

-- 可以用group by 来取唯一,也可以用distinct
SELECT cid FROM score WHERE score<60 GROUP BY cid;
SELECT DISTINCT cid FROM score WHERE score<60;


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

SELECT s.sname,sc.sid,sc.score FROM student s,score sc WHERE sc.cid = "01" AND sc.score > 80 AND s.sid =sc.sid;


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

SELECT c.cname,sc.cid,count(sc.sid) as snum FROM course c,score sc WHERE c.cid = sc.cid GROUP BY sc.cid;


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

SELECT s.sname,sc.sid,sc.score 
FROM student s,score sc,course c,teacher t 
WHERE s.sid=sc.sid AND sc.cid=c.cid AND t.tid=c.tid AND t.tname="张三" 
ORDER BY sc.score DESC 
limit 0,1; 
-- order之后限制查询结果数;limit 1 与limit 0,1效果相同,若为limit 0,2则表示从第一行开始取两行数据


34.成绩有重复的情况下,查询选修「王五」老师所授课程的学生中,成绩最高的学生信息及其成绩!!!!!!

SELECT s.sname,sc.sid,sc.score FROM student s,score sc,course c,teacher t WHERE s.sid=sc.sid AND sc.cid=c.cid AND t.tid=c.tid AND t.tname="王五" ORDER BY sc.score DESC;-- 原数据:最高分sid为201903,cid为02,score为100
UPDATE score SET score = 100 WHERE sid = "201915" AND cid = "02"; -- 修改原数据后:201903、201915并列第一(100分)

-- 查询到最高分数,限制条件为 分数 = 最高分
SELECT s.sname,s.sage,s.ssex,sc.sid,sc.score 
FROM student s,score sc,course c,teacher t 
WHERE s.sid=sc.sid AND sc.cid=c.cid AND t.tid=c.tid AND t.tname="王五" 
AND sc.score =(
        SELECT max(sc.score) FROM 
        student s,score sc,course c,teacher t 
        WHERE s.sid=sc.sid AND sc.cid=c.cid AND t.tid=c.tid AND t.tname="王五");

 

35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩!!!!!!

#插入一学生数据(202001 3门课程成绩相同) 注:实际业务中使用外键限制无学生信息的学生成绩插入表中,此处仅作练习
INSERT INTO Score(sid,cid,score) 
VALUE
(202001,01,99),
(202001,02,99),
(202001,03,99);

 

 

 

#INNER JOIN(用group by取唯一)
SELECT sc.sid,sc.cid,sc.score 
FROM score sc 
INNER JOIN score sc2
ON sc.sid = sc2.sid AND sc.cid != sc2.cid AND sc.score = sc2.score 
GROUP BY cid,sid 
ORDER BY sid;-- 使用inner join;201903(01 03相同 但02 不同) 201912(02 03相同 但 02 不同)

 

 36.查询每门功成绩最好的前两名(同18题)--分组排序问题

SELECT * 
FROM
(SELECT sc.*,row_number() over (PARTITION BY sc.cid ORDER BY sc.score DESC) as pm FROM score sc)a 
WHERE a.pm IN (1,2);

 

37.统计每门课程的学生选修人数(超过 5 人的课程才统计)

SELECT cid,count(sid) AS 选修人数 FROM score GROUP BY cid HAVING 选修人数>5;

 

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

SELECT sid,count(cid) as cs FROM score GROUP BY sid HAVING cs >=2;

 

39.查询选修了全部课程的学生信息

SELECT s.* FROM score sc,student s WHERE sc.sid=s.sid GROUP BY sc.sid HAVING count(sc.cid) = (SELECT DISTINCT count(cid) FROM course);

 

40.查询各学生的年龄,只按年份来算(若未到生日则虚增一岁)!!!!!!

备注:该题未解决

SELECT s.sname,FLOOR((DATEDIFF(CURRENT_DATE(),s.sage)/365.2422)) as age,s.sage FROM student s;-- 该方法未到生日的年龄未虚增
select student.*,year(curdate())-year(sage)+c.a as 年龄 
from student,
    (select sid,
                    (case when month(curdate())<month(sage) and day(curdate())<day(sage) then -1 else 0 end) as a
    from student) c
where student.sid = c.sid;-- 该方法未到生日的年龄未虚增

#年龄计算参考:https://www.jianshu.com/p/64b53c26bea1

 

41.按照出生日期来算,当前月日 < 出生年月的月日则年龄减一!!!!!!

SELECT 
  s.sname,
  TIMESTAMPDIFF(year,s.sage,CURRENT_DATE()) AS age,
  s.sage
FROM student s;-- 当前日期为2020-4-21,鲁韦昌马的出生日期为1994-07-22,俞任袁柳的出生日期为1994-01-16,按照此方法计算,前者为25岁(未满),后者为26岁(已满) SELECT
  s.sname,
  ROUND((DATEDIFF(now(),s.sage)/365.2422)) as age,
  s.sage
FROM student s;-- 当前日期为2020-4-21,孔曹严华的出生日期为1994-05-23,戚谢邹喻的出生日期为1994-11-30,按照此方法计算,前者(未满但快满)快满岁则年龄为26(虚增1岁),后者还远未满,年龄为25,该方法最接近按照出生日期精确计算的年龄

 

42.查询本周过生日的学生!!!!!!

备注:跨年查询未解决

select *from Student
where WEEKOFYEAR(CONCAT(YEAR(CURRENT_DATE),-,MONTH(Sage),-,DAY(Sage))) = WEEKOFYEAR(CURRENT_DATE);

 

43.查询下周过生日的学生!!!!!!

select *from Student
where WEEKOFYEAR(CONCAT(YEAR(CURRENT_DATE),-,MONTH(Sage),-,DAY(Sage))) = WEEKOFYEAR(CURRENT_DATE)+1;

 

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

SELECT * FROM student WHERE MONTH(student.sage) = MONTH(CURRENT_DATE());

 

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

SELECT * FROM student WHERE MONTH(student.sage) = MONTH(CURRENT_DATE())+1;
#以上若出现跨年查询则应该使用

SELECT * FROM student WHERE MONTH(DATE_SUB(sage,INTERVAL 1 month)) = MONTH(CURRENT_DATE);-- 出生日期减1月,若为1994-12,则值变为1993-12

select sid from Student
where (case when month(CURDATE())=12 then 1 else month(CURDATE())+1 end) = month(sage);

 

 

插入数据

#populate
#1.更新成绩表
#插入单条数据(注:若values值按顺序排列,字段名可省略,最好加上字段名)
INSERT into student(sid,sname,sage,ssex) values(201901,赵钱孙李,1993/9/8,);

#插入多行数据
INSERT into student(sid,sname,sage,ssex) 
values
(201902,冯陈褚卫,1993-04-19,),
(201903,朱秦尤许,1991-10-21,),
(201904,孔曹严华,1994-05-23,),
(201905,戚谢邹喻,1994-11-30,),
(201906,云苏潘葛,1991-08-29,),
(201907,鲁韦昌马,1994-07-22,),
(201908,俞任袁柳,1994-01-16,),
(201909,费廉岑薛,1993-03-24,),
(201910,滕殷罗毕,1993-07-04,),
(201911,周吴郑王,1994-11-03,),
(201912,蒋沈韩杨,1992-09-05,),
(201913,何吕施张,1992-09-02,),
(201914,金魏陶姜,1994-03-18,),
(201915,柏水窦章,1994-12-05,),
(201916,奚范彭郎,1994-01-22,),
(201917,苗凤花方,1993-03-09,),
(201918,酆鲍史唐,1992-09-27,),
(201919,雷贺倪汤,1994-08-22,),
(201920,郝邬安常,1993-01-18,);

INSERT into student(sid,sname,sage,ssex) 
values
(201921,张三,1993-04-20,);

INSERT into student(sid,sname,sage,ssex) 
values
(201922,李四,1995-01-20,);

INSERT into student(sid,sname,sage,ssex) 
values
(201923,王二,1995-01-20,),
(201924,赵四,1995-03-20,);

INSERT into student(sid,sname,sage,ssex) 
values
(201925,王五,1995-01-20,);

INSERT into student(sid,sname,sage,ssex) 
values
(201926,孙钱,1995-01-20,),
(201927,三一,1995-03-20,);

INSERT into student(sid,sname,sage,ssex) 
values
(201928,妙妙,1995-01-20,);


#2.更新课程表
insert into course values
(01,数学,01),
(02,语文,03),
(03,英语,01);

#3.更新教师表
INSERT INTO teacher VALUES(01,张三),(02,李四),(03,王五);

#4.更新成绩表
INSERT INTO Score 
VALUE
(201901,01,68),
(201902,01,92),
(201903,01,94),
(201904,01,79),
(201905,01,64),
(201906,01,80),
(201907,01,91),
(201908,01,86),
(201909,01,95),
(201910,01,68),
(201911,01,96),
(201912,01,73),
(201913,01,62),
(201914,01,81),
(201915,01,82),
(201916,01,65),
(201917,01,99),
(201918,01,84),
(201919,01,90),
(201920,01,71),
(201901,02,93),
(201902,02,93),
(201903,02,100),
(201904,02,76),
(201905,02,61),
(201906,02,70),
(201907,02,62),
(201908,02,94),
(201909,02,77),
(201910,02,87),
(201911,02,91),
(201912,02,63),
(201913,02,73),
(201914,02,96),
(201915,02,97),
(201916,02,93),
(201917,02,60),
(201918,02,70),
(201919,02,61),
(201920,02,81),
(201901,03,86),
(201902,03,99),
(201903,03,94),
(201904,03,81),
(201905,03,100),
(201906,03,85),
(201907,03,86),
(201908,03,66),
(201909,03,68),
(201910,03,88),
(201911,03,79),
(201912,03,63),
(201913,03,74),
(201914,03,67),
(201915,03,91),
(201916,03,77),
(201917,03,80),
(201918,03,96),
(201919,03,68),
(201920,03,83);

INSERT INTO Score 
VALUE
(201921,01,80);

INSERT INTO Score 
VALUE
(201922,01,90);

INSERT INTO Score 
VALUE
(201923,02,90),(201923,03,89),
(201924,02,70),(201924,03,80);

INSERT INTO Score 
VALUE
(201925,02,90);

INSERT INTO Score 
VALUE
(201926,01,50),(201926,02,49),(201926,03,20),
(201927,02,50),(201927,03,58);

INSERT INTO Score 
VALUE
(201928,02,50);

 

 

持续更新补充

有任何问题欢迎批评指正,邮箱 fanyu1601@163.com

SQL:经典面试50题

上一篇:mysql语法


下一篇:10、mongodb