MySQL经典练习题

本文参考CSDN博主「csdnluolei」的MySQL经典练习50题,链接如下:

https://blog.csdn.net/csdnluolei/article/details/83507312?spm=1001.2014.3001.5506
练习题涉及到四个表:学生信息表student、成绩表score、课程信息表course、教师信息表teacher。四个表的详细字段如下文所示,表格的建表语句和插入数据语句请前往 CSDN博主「csdnluolei」的“MySQL经典练习50题”文章复制粘贴。

学生信息表student:
学生信息表student
成绩表score
成绩表score
课程信息表course
课程信息表course
教师信息表teacher
教师信息表teacher

练习题目及答案

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

#方法一:先使用自连接查找出课程1分数比课程2高的学生s_id和课程分数,然后再和学生信息表做关联查询
SELECT 
  s.*,
  sc.c1_score,
  sc.c2_score 
FROM
  student s 
  INNER JOIN 
    (SELECT 
    --使用自连接查找出课程1分数比课程2高的学生s_id和课程分数
      s1.`s_id` AS s_id,
      s1.`s_score` AS c1_score,
      s2.`s_score` AS c2_score 
    FROM
      score s1 
      INNER JOIN score s2 
        ON s1.`s_id` = s2.`s_id` 
        AND s1.`c_id` = 01 
        AND s2.`c_id` = 02 
        AND s1.`s_score` > s2.`s_score`) sc 
    ON s.s_id = sc.s_id 
    
#方法二:使用三表连接
SELECT 
  s.*,
  s1.`s_score` AS c1_score,
  s2.`s_score` AS c2_score 
FROM
  student s 
  LEFT JOIN score s1 
    ON s.`s_id` = s1.`s_id` 
    AND s1.c_id = '01' 
  LEFT JOIN score s2 
    ON s.`s_id` = s2.`s_id` 
    AND s2.c_id = '02' 
WHERE s1.`s_score` > s2.`s_score` ;

2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
参考题目1,只需把条件筛选中的>变成<即可

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

#方法一:先在成绩表筛选出平均分大于60的学生编号,然后再和学生信息表连接
SELECT 
  s.`s_id`,
  s.`s_name`,
  sc.avg_s 
FROM
  student s 
  INNER JOIN 
    (SELECT 
      s_id,
      AVG(s_score) AS avg_s 
    FROM
      score 
    GROUP BY s_id 
    HAVING avg_s > 60) sc 
    ON s.`s_id` = sc.s_id;
#方法二:先把成绩表和学生信息表连接,再分组统计学生的平均分,最后筛选
SELECT 
  s.`s_id`,
  s.`s_name`,
  AVG(sc.s_score) AS avg_s 
FROM
  student s 
  INNER JOIN score sc 
    ON s.`s_id` = sc.`s_id` 
GROUP BY s.`s_id`,
  s.`s_name` 
HAVING avg_s > 60 

– 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩 (包括有成绩的和无成绩的)

--方法一:使用联合查询,第一个查询语句查询有成绩的同学信息,第二个查询语句查询没有成绩的同学信息
SELECT 
  s.`s_id`,
  s.`s_name`,
  AVG(sc.s_score) AS avg_s 
FROM
  student s 
  INNER JOIN score sc 
    ON s.`s_id` = sc.`s_id` 
GROUP BY s.`s_id`,
  s.`s_name` 
HAVING avg_s <60 
UNION 
SELECT
  s.`s_id`,
  s.`s_name`,
  NULL AS avg_s 
FROM
  student s
WHERE 
 s.`s_id` NOT IN (SELECT DISTINCT s_id FROM score);
 --方法二:使用左连接
 SELECT 
  s.`s_id`,
  s.`s_name`,
  AVG(sc.`s_score`) AS avg_s 
FROM
  student s 
  LEFT JOIN score sc 
    ON s.`s_id` = sc.`s_id` 
GROUP BY s.`s_id` 
HAVING avg_s < 60 
  OR s.`s_id` NOT IN 
  (SELECT DISTINCT 
    s_id 
  FROM
    score) ;

– 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩,并从高到低排序

SELECT 
  st.`s_id`,
  st.`s_name`,
  COUNT(*) AS 选课总数,
  SUM(sc.`s_score`) AS 总成绩 
FROM
  student st 
  LEFT JOIN score sc 
    ON st.`s_id` = sc.s_id 
GROUP BY st.`s_id` 
ORDER BY 总成绩 DESC;

– 6、查询"李"姓老师的数量

SELECT COUNT(*) AS 数量 FROM teacher WHERE `t_name` LIKE '李%';

– 7、查询学过"张三"老师授课的同学的信息

#方法一:使用子查询
SELECT 
  st.`s_id`,st.`s_name`,st.`s_birth`,st.`s_sex` 
FROM
  student st 
WHERE s_id IN 
  (SELECT 
   --查询学过张三老师开设课程的学生的编号id
    s_id 
  FROM
    score 
  WHERE c_id IN 
    (SELECT 
    --查询张三老师开设课程的编号
      c_id 
    FROM
      course 
    WHERE t_id IN 
      (SELECT
      --查询张三老师的教师编号id
        t_id 
      FROM
        teacher 
      WHERE t_name = '张三'))) ;
#方法二:使用连接查询+子查询
SELECT 
  st.* 
FROM
  student st 
  INNER JOIN score sc 
    ON st.`s_id` = sc.`s_id` 
WHERE sc.c_id IN 
  (SELECT 
    c_id 
  FROM
    course 
  WHERE t_id IN 
    (SELECT 
      t_id 
    FROM
      teacher 
    WHERE t_name = '张三')) ;

– 8、查询没学过"张三"老师授课的同学的信息

#使用子查询
SELECT 
  st.`s_id`,st.`s_name`,st.`s_birth`,st.`s_sex` 
FROM
  student st 
WHERE s_id NOT IN 
  (SELECT 
  --查询学过张三老师开设课程的学生的编号id
    s_id 
  FROM
    score 
  WHERE c_id IN 
    (SELECT 
     --查询张三老师开设课程的编号
      c_id 
    FROM
      course 
    WHERE t_id IN 
      (SELECT 
      --查询张三老师的教师编号id
        t_id 
      FROM
        teacher 
      WHERE t_name = '张三'))) ;
  • -9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息,及两门课程成绩
#三表连接
SELECT 
  st.*,
  sc1.s_score AS c1_s,
  sc2.s_score AS c2_s 
FROM
  student st 
  INNER JOIN score sc1 
    ON st.`s_id` = sc1.`s_id` 
    AND sc1.`c_id` = '01' 
  INNER JOIN score sc2 
    ON st.`s_id` = sc2.`s_id` 
    AND sc2.`c_id` = '02' 

– 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息

#方法一:使用连接查询
SELECT 
  st.* 
FROM
  student st 
  INNER JOIN score sc1 
    ON st.`s_id` = sc1.`s_id` 
    AND sc1.`c_id` = '01' 
    AND st.s_id NOT IN 
    (SELECT 
    --查询学过课程编号为02的学生编号id
      s_id 
    FROM
      score 
    WHERE `c_id` = '02') ;
#方法二:使用子查询
SELECT 
  * 
FROM
  student st 
WHERE st.`s_id` IN 
  (SELECT 
    s_id 
  FROM
    score 
  WHERE c_id = '01') 
AND st.`s_id` NOT IN 
  (SELECT 
    s_id 
  FROM
    score 
  WHERE c_id = '02')

– 11、查询没有学全所有课程的同学的信息

#方法一:
SELECT 
  * 
FROM
  student 
WHERE s_id IN 
  (SELECT 
  --提取出符合条件的学生编号id
    c.`s_id` 
  FROM
    (SELECT
    --查询每个学生学的课程个数,并筛选出没有学完全部课程的同学
      st.`s_id`,
      COUNT(sc.s_score) AS num 
    FROM
      student st 
      LEFT JOIN score sc 
        ON st.`s_id` = sc.s_id 
    GROUP BY st.`s_id` 
    HAVING num <> 
      (SELECT 
        COUNT(DISTINCT c_id) 
      FROM
        score)) c);
#方法二:使用子查询
SELECT 
  * 
FROM
  student 
WHERE s_id NOT IN 
  (SELECT
  --查询学完全部课程的学生的编号 
    s_id 
  FROM
    score 
  GROUP BY s_id 
  HAVING COUNT(s_score) = 3) 

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

#方法一:使用子查询
SELECT * FROM student WHERE s_id IN
--查询至少有一门课程与学号为01的同学所学相同的同学的学号
(SELECT DISTINCT s_id FROM score WHERE c_id IN 
--查询学号为01的同学所学的课程
(SELECT c_id FROM score WHERE s_id='01'));
#方法二:使用连接查询
SELECT 
st.`s_id`,st.`s_name`,st.`s_birth`,st.`s_sex`
FROM student st
INNER JOIN score sc
ON st.`s_id`=sc.`s_id`
WHERE sc.`c_id` IN(SELECT c_id FROM score WHERE s_id='01')
AND st.`s_id`<>'01'
--group by有去重的作用
GROUP BY st.`s_id`,st.`s_name`,st.`s_birth`,st.`s_sex`;

– 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息

#方法一:
SELECT 
  * 
FROM
  student st 
WHERE st.`s_id` IN 
  (SELECT 
    s1.s_id 
  FROM
    score s1 
    JOIN score s2 
      ON s1.s_id = s2.s_id 
      AND s2.c_id = '02' 
    JOIN score s3 
      ON s1.s_id = s3.s_id 
      AND s3.c_id = '03' 
  WHERE s1.c_id = '01') ;
  #方法二:
  SELECT 
  * 
FROM
  student st 
WHERE st.`s_id` IN 
  (SELECT s_id FROM score WHERE c_id IN
	(SELECT c_id FROM score WHERE s_id='01') GROUP BY s_id
   HAVING COUNT(1)=(SELECT COUNT(1) FROM score WHERE s_id='01')
   ) ;

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

SELECT 
  st.`s_id`,
  st.`s_name`,
  COUNT(1) 课程数,
  AVG(sc.s_score) AS avg_s 
FROM
  score sc 
  LEFT JOIN student st ON sc.s_id = st.`s_id` 
WHERE sc.s_score < 60 
GROUP BY st.`s_id` 
HAVING COUNT(1) >= 2 ;

– 16、检索"01"课程分数小于60,按分数降序排列的学生信息及01分数

SELECT 
  st.*,
  sc.s_score 
FROM
  student st 
  RIGHT JOIN score sc 
    ON st.`s_id` = sc.s_id 
WHERE sc.c_id = '01' 
  AND sc.s_score < 60 
ORDER BY sc.s_score DESC 

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

#方法一:
SELECT s_id,
(SELECT s_score FROM score WHERE s_id=a.s_id AND c_id='01') AS c1_score,
(SELECT s_score FROM score WHERE s_id=a.s_id AND c_id='02') AS c2_score,
(SELECT s_score FROM score WHERE s_id=a.s_id AND c_id='03') AS c3_score,
AVG(s_score) AS avg_s 
FROM score a GROUP BY s_id ORDER BY avg_s DESC;
#方法二:多表连接,但表的个数很多是效率较低
SELECT
sc.`s_id`, s1.s_score,s2.s_score,s3.s_score,AVG(sc.s_score) AS avg_s
FROM score sc
LEFT JOIN score s1 ON sc.`s_id`=s1.s_id AND s1.c_id='01'
LEFT JOIN score s2 ON sc.`s_id`=s2.s_id AND s2.c_id='02'
LEFT JOIN score s3 ON sc.`s_id`=s3.s_id AND s3.c_id='03'
GROUP BY sc.`s_id`
ORDER BY avg_s DESC;

– 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90

SELECT sc.c_id,c_name,
MAX(s_score) AS max_s,
MIN(s_score) AS min_s,
AVG(s_score) AS avg_s,
SUM(CASE WHEN s_score>60 THEN 1 ELSE 0 END)/COUNT(s_score) AS 及格率,
SUM(CASE WHEN s_score>=70 AND s_score<80 THEN 1 ELSE 0 END)/COUNT(s_score) AS 中等率,
SUM(CASE WHEN s_score>=80 AND s_score<90 THEN 1 ELSE 0 END)/COUNT(s_score) AS 优良率,
SUM(CASE WHEN s_score>=90 THEN 1 ELSE 0 END)/COUNT(s_score) AS 优秀率
FROM score sc
INNER JOIN course c ON sc.c_id=c.c_id
 GROUP BY c_id;

– 19、按各科成绩进行排序,并显示排名

#使用窗口函数,row_number(),MySQL是8.0版本以上才支持窗口函数
SELECT 
  c_id,
  s_score,
  row_number () over (
    PARTITION BY c_id 
ORDER BY s_score DESC
) AS rank1 
FROM
  score ;

– 20、查询学生的总成绩并进行排名

SELECT 
  a.s_id,
  a.sum_s,
  row_number () over (ORDER BY sum_s DESC) AS rank1 
FROM
  (SELECT 
    s_id,
    SUM(s_score) AS sum_s 
  FROM
    score 
  GROUP BY s_id) a ;

– 21、查询不同老师所教不同课程平均分从高到低显示

SELECT 
  t.t_id,
  t.t_name,
  c.c_id,
  c.c_name,
  sc.avg_s 
FROM
  course c 
INNER JOIN teacher t ON c.t_id = t.t_id
LEFT JOIN(
SELECT c_id,AVG(s_score) AS avg_s FROM score GROUP BY c_id
) sc
ON c.c_id=sc.c_id
ORDER BY sc.avg_s DESC;

– 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩

SELECT 
  st.*,
  c_id,
  a.s_score,
  rank1 
FROM
student st
JOIN
  (SELECT 
    s_id,
    c_id,
    s_score,
    row_number () over (PARTITION BY c_id ORDER BY s_score) AS rank1 
  FROM
    score) a 
 ON st.s_id=a.s_id
WHERE a.rank1 IN (2, 3);

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

SELECT c_id,
SUM(CASE WHEN s_score >=85 THEN 1 ELSE 0 END) AS "100-85_num",
SUM(CASE WHEN s_score >=85 THEN 1 ELSE 0 END)/COUNT(s_score) AS "100-85_num",
SUM(CASE WHEN s_score>=70 AND s_score<85 THEN 1 ELSE 0 END) AS "85-70_num",
SUM(CASE WHEN s_score>=70 AND s_score<85 THEN 1 ELSE 0 END)/COUNT(s_score) AS "85-70_num",
SUM(CASE WHEN s_score>=60 AND s_score<70 THEN 1 ELSE 0 END) AS "70-60_num",
SUM(CASE WHEN s_score>=60 AND s_score<70 THEN 1 ELSE 0 END)/COUNT(s_score) AS "70-60_num",
SUM(CASE WHEN s_score<=60 THEN 1 ELSE 0 END) AS "0-60_num",
SUM(CASE WHEN s_score<=60 THEN 1 ELSE 0 END)/COUNT(s_score) AS "0-60_num"
FROM score
GROUP BY c_id;

– 24、查询学生平均成绩及其名次

#和第20题相似,只需把聚合函数sum改成avg即可
SELECT 
  a.s_id,
  a.avg_s,
  row_number () over (ORDER BY avg_s DESC) AS rank1 
FROM
  (SELECT 
    s_id,
    AVG(s_score) AS avg_s 
  FROM
    score 
  GROUP BY s_id) a ;
  • 24.1查询学生平均成绩及其名次,添加名次rank,(相同分数的相同名次,并列排名)
#注意rank()/row_number()/dense_rank()之间的差别
SELECT 
  a.s_id,
  a.avg_s,
  rank () over (ORDER BY avg_s DESC) AS rank1 
FROM
  (SELECT 
    s_id,
    AVG(s_score) AS avg_s 
  FROM
    score 
  GROUP BY s_id) a ;

– 25、查询各科成绩前三名的记录

SELECT 
  a.c_id,
  a.s_score 
FROM
  (SELECT 
    c_id,
    s_score,
    row_number () over (
      PARTITION BY c_id 
  ORDER BY s_score DESC
  ) AS rank1 
  FROM
    score) a 
WHERE rank1 <= 3 ;

– 26、查询每门课程被选修的学生数

SELECT c_id,COUNT(s_score) AS num FROM score GROUP BY c_id;

– 27、查询出只有两门课程的全部学生的学号和姓名

SELECT 
  st.s_id,
  st.s_name 
FROM
  student st 
  JOIN 
    (SELECT 
      s_id,
      COUNT(s_score) AS num 
    FROM
      score 
    GROUP BY s_id 
    HAVING num = 2) a 
    ON st.s_id = a.s_id ;

28/29题

-- 28、查询男生、女生人数
SELECT s_sex,COUNT(*) FROM student GROUP BY s_sex;
-- 29、查询名字中含有"风"字的学生信息
SELECT * FROM student WHERE s_name LIKE '%风%';

– 30、查询同名同性学生名单,并统计同名人数

SELECT 
  st.`s_name`,
  st.`s_sex`,
  COUNT(*) AS num 
FROM
  student1 st 
GROUP BY st.`s_name`,
  st.`s_sex`
HAVING num>1 ;

31-35

-- 31、查询1990年出生的学生名单
SELECT * FROM student WHERE s_birth LIKE '1990%';-- s_bith的数据类型是varchar
-- 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
SELECT c_id,AVG(s_score) AS avg_s 
FROM
  score 
GROUP BY c_id 
ORDER BY avg_s DESC,c_id ;
-- 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
SELECT 
  st.s_id,
  st.s_name,
  sc.avg_s 
FROM
  student st 
  INNER JOIN 
    (SELECT 
      s_id,
      AVG(s_score) AS avg_s 
    FROM
      score 
    GROUP BY s_id 
    HAVING avg_s > 85) sc 
    ON st.s_id = sc.s_id ;
-- 34、查询课程名称为"数学",且分数低于60的学生姓名和分数
SELECT 
  st.s_name,
  sc.s_score 
FROM
  student st 
  INNER JOIN score sc 
    ON st.s_id = sc.s_id 
    AND s_score < 60 
    AND c_id = 
    (SELECT 
      c_id 
    FROM
      course 
    WHERE c_name = '数学') ;
    
 -- 35、查询所有学生的课程及分数情况;
 SELECT 
 st.s_id,st.s_name,
SUM(CASE c.c_name WHEN '语文' THEN sc.s_score ELSE 0 END) AS 语文,
SUM(CASE c.c_name WHEN '数学' THEN sc.s_score ELSE 0 END) AS 数学,
SUM(CASE c.c_name WHEN '英语' THEN sc.s_score ELSE 0 END) AS 英语
 FROM student st  
 LEFT JOIN score sc ON st.s_id=sc.s_id
 LEFT JOIN course c ON sc.c_id=c.c_id
GROUP BY st.s_id,st.s_name;

题目:36-50

-- 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
SELECT 
  st.s_name,
  c.c_name,
  sc.s_score 
FROM
  student st 
  LEFT JOIN score sc ON st.s_id = sc.s_id 
  LEFT JOIN course c ON sc.c_id = c.c_id 
WHERE sc.s_score >70 ;

-- 37、查询不及格的学生id,姓名,及其课程名称,分数
SELECT 
  st.s_id,
  st.s_name,
  c.c_name,
  sc.s_score 
FROM
  student st 
  LEFT JOIN score sc 
    ON st.s_id = sc.s_id 
  LEFT JOIN course c 
    ON sc.c_id = c.c_id 
WHERE sc.s_score < 60 ;

-- 38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名; 
SELECT 
  st.s_id,
  st.s_name,
  sc.c_id,
  sc.s_score 
FROM
  student st 
  LEFT JOIN score sc 
    ON st.s_id = sc.s_id 
WHERE sc.c_id = '01' 
  AND sc.s_score >= 80;
  
-- 39、求每门课程的学生人数
SELECT 
  c.c_name,
  COUNT(1) AS 学生人数 
FROM
  score s 
  INNER JOIN course c 
    ON s.c_id = c.c_id 
GROUP BY c.c_id ;

-- 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
SELECT 
  st.*,
  sc.c_id,
  sc.s_score 
FROM
  student st 
  LEFT JOIN score sc 
    ON st.s_id = sc.s_id 
WHERE c_id IN 
  (SELECT c_id FROM course WHERE t_id IN 
    (SELECT t_id FROM teacher WHERE t_name = '张三')
   ) 
GROUP BY sc.c_id 
ORDER BY sc.s_score DESC;

-- 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 
SELECT 
  s1.s_id,
  s1.c_id,
  s1.s_score 
FROM
  score s1 
  INNER JOIN score s2 
ON s1.s_score = s2.s_score AND s1.c_id <> s2.c_id
GROUP BY
  s1.s_id,
  s1.c_id,
  s1.s_score ;

-- 42、查询每门功课成绩最好的前两名
SELECT 
  a.c_id,
  a.s_score,
  a.rank1 
FROM
  (SELECT 
    s.c_id,
    s.s_score,
    row_number () over (PARTITION BY c_id ORDER BY s_score) AS rank1 
   FROM
    score s) a 
WHERE a.rank1 < 3 ;

-- 43、统计每门课程的学生选修人数(超过5人的课程才统计)。
-- 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 
SELECT 
  sc.c_id,
  COUNT(sc.s_score) AS num 
FROM
  score sc 
GROUP BY sc.c_id 
HAVING num > 5 
ORDER BY num DESC,c_id ;

-- 44、检索至少选修两门课程的学生学号
SELECT 
  s_id 
FROM
  score 
GROUP BY s_id HAVING COUNT(1) >= 2 ;

-- 45、查询选修了全部课程的学生信息
SELECT 
  st.* 
FROM
  student st 
  INNER JOIN score sc ON st.s_id = sc.s_id 
GROUP BY st.s_id 
HAVING COUNT(sc.s_score) = 
  (SELECT DISTINCT COUNT(c_id) FROM course) ;

-- 46、查询各学生的年龄
SELECT st.*,YEAR(NOW())-YEAR(st.s_birth)AS age FROM student st;

-- 47、查询本周过生日的学生
SELECT * FROM student WHERE WEEK(CURDATE())=WEEK(s_birth);

-- 48、查询下周过生日的学生
SELECT * FROM student WHERE WEEK(CURDATE())=WEEK(s_birth)-1;

-- 49、查询本月过生日的学生
SELECT * FROM student WHERE MONTH(CURDATE())=MONTH(s_birth);

-- 50、查询下月过生日的学生
SELECT * FROM student WHERE MONTH(CURDATE())=MONTH(s_birth)-1;

你可能感兴趣的:(mysql)