• MySQL 练习题


    创建表

     --1.学生表
    Student(Sno,Sname,Sage,Ssex) --Sno 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别

    create table Student(
    Sno varchar(10),
    Sname varchar(10),
    Sage datetime,
    Ssex nvarchar(10)
    ) ;
    
    --创建测试数据
    insert into Student values('01' , '赵四' , '1994-01-01' , '');
    insert into Student values('02' , '钱贵' , '1996-12-21' , '');
    insert into Student values('03' , '孙三' , '1994-05-20' , '');
    insert into Student values('04' , '李一' , '1993-08-06' , '');
    insert into Student values('05' , '周吴' , '1994-12-01' , '');
    insert into Student values('06' , '吴理' , '1995-03-01' , '');
    insert into Student values('07' , '郑八' , '1996-07-01' , '');
    insert into Student values('08' , '王玖' , '1995-01-20' , '');


    --2.课程表 
    Course(Cno,Cname,Tno) --Cno --课程编号,Cname 课程名称,Tno 教师编号

    create table Course(
    Cno varchar(10),
    Cname varchar(10),
    Tno varchar(10)
    );
    
    --创建测试数据
    insert into Course values('01' , '语文' , '02');
    insert into Course values('02' , '数学' , '03');
    insert into Course values('03' , '英语' , '01');
     

    --3.教师表 
    Teacher(Tno,Tname,Ssex) --T 教师编号,Tname 教师姓名,Tsex 老师性别

    create table Teacher(
    Tno varchar(10),
    Tname varchar(10),
    Tsex varchar(10)
    
    );
    --创建测试数据
    insert into Teacher values('01' , '张老师', '');
    insert into Teacher values('02' , '李老师''');
    insert into Teacher values('03' , '王老师''');


    --4.成绩表 
    SC(Sno,Cno,score) --Sno 学生编号,Cno 课程编号,score 分数

    create table SC(
    Sno varchar(10),
    Cno varchar(10),
    score decimal(18,1)
    );
    
    --创建测试数据
    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);
    insert into SC values('08' , '01' , 88);
    insert into SC values('08' , '02' , 90);
    insert into SC values('08' , '03' , 87);

     题目(点击看答案)

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

    --2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数。 (略)

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

    --4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩。(略)

    --5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩。(答案

    --6、查询"李"姓老师的数量。(答案

    --7、查询学过"张老师"授课的同学的信息。(答案

    --8、查询没学过"张老师"授课的同学的信息。(答案

    --9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息。(答案

    --10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息。(答案

    --11、查询没有学全所有课程的同学的信息。(答案

    --12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息。(答案

    --13、查询和"01"号的同学学习的课程完全相同的其他同学的信息。(答案

    --14、查询没学过"张老师"讲授的任一门课程的学生姓名。(答案

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

    --16、检索"01"课程分数小于60,按分数降序排列的学生信息。(答案

    --17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩。(答案

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

    --19、按各科成绩进行排序,并显示排名。(答案

    --20、查询学生的总成绩并进行排名。(答案

    --21、查询不同老师所教不同课程平均分从高到低显示。(答案

    --22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩。(答案

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

    --24、查询学生平均成绩及其名次。(答案

    --25、查询各科成绩前三名的记录。(答案

    --26、查询每门课程被选修的学生数。(答案

    --27、查询出只有两门课程的全部学生的学号和姓名。(答案

    --28、查询男生、女生人数。(略)

    --29、查询名字中含有"风"字的学生信息。(略)

    --30、查询同名同性学生名单,并统计同名人数。(答案

    --31、查询1994年出生的学生名单(注:Student表中Sage列的类型是datetime)。(答案

    --32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号。(答案

    --33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩。(答案

    --34、查询课程名称为"数学",且分数低于60的学生姓名和分数。(答案

    --35、查询所有学生的课程及分数情况。(答案

    --36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数。(答案

    --37、查询不及格的课程。(略)

    --38、查询课程编号为02且课程成绩在80分以上的学生的学号和姓名。(答案

    --39、求每门课程的学生人数。(略)

    --40、查询选修"张老师"所授课程的学生中,成绩最高的学生信息及其成绩。(答案

    --41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩。(答案

    --42、查询每门功成绩最好的前两名。(略)

    --43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列。(答案

    --44、检索至少选修两门课程的学生学号。(略)

    --45、查询选修了全部课程的学生信息。(略)

    --46、查询各学生的年龄。(答案

    --47、查询本周过生日的学生。(答案

    --48、查询下周过生日的学生。(略)

    --49、查询本月过生日的学生。(答案

    --50、查询下月过生日的学生。(答案

    答案:

    1、

    SELECT stu.*
    ,sc01.score AS '01分数'
    ,sc02.score AS '02分数'
    FROM student stu
    INNER JOIN sc sc01
    ON stu.Sno=sc01.Sno AND sc01.Cno='01'
    INNER JOIN sc sc02
    ON stu.Sno=sc02.Sno AND sc02.Cno='02'
    WHERE sc02.score > sc01.score ;

    3、

    SELECT stu.Sno
    ,stu.sname
    ,AVG(sc.score) AS '平均分'
    FROM student stu
    INNER JOIN sc
    ON stu.Sno=sc.Sno
    GROUP BY stu.Sno
    HAVING AVG(sc.score)>=60;

    5、

    SELECT stu.Sno
    ,stu.sname
    ,COUNT(sc.Cno) AS '选课总数'
    ,SUM(sc.score) AS '总成绩'
    FROM student  stu
    INNER JOIN sc
    ON stu.Sno=sc.Sno
    GROUP BY stu.Sno

    6、

    SELECT COUNT(*) FROM teacher WHERE Tname LIKE '李%' ;

    7、

    SELECT stu.*
    FROM student stu
    INNER JOIN sc
    ON stu.Sno=sc.Sno
    INNER JOIN course c
    ON sc.Cno=c.Cno
    INNER JOIN teacher t
    ON c.Tno=t.Tno
    WHERE t.Tname ='张老师' 

    8、

    SELECT stu.*
    FROM student stu
    LEFT JOIN sc
    ON stu.Sno=sc.Sno
    WHERE NOT EXISTS(
                  SELECT *
                  FROM course c
                  INNER JOIN teacher t
                  ON c.Tno=t.Tno
                  INNER JOIN sc
                  ON sc.Cno=c.Cno
                  WHERE t.Tname='张老师'
                  AND sc.Sno=stu.Sno
           )
    GROUP BY Sno;



    9、

    SELECT stu.*
    FROM student stu
    INNER JOIN sc sc01
    ON stu.Sno=sc01.Sno AND sc01.Cno='01'
    INNER JOIN sc sc02
    ON stu.Sno=sc02.Sno AND sc02.Cno='02'

    10、

    select stu.*
    from student stu
    LEFT JOIN sc sc01
    on stu.Sno=sc01.Sno and sc01.Cno='01'
    LEFT JOIN sc sc02
    on stu.Sno=sc02.Sno and sc02.Cno='02'
    where sc01.Cno='01' and sc02.Cno='02' is NULL ;

    11、 

    SELECT s.*
    FROM student s
    LEFT JOIN sc
    ON s.Sno=sc.Sno
    LEFT JOIN (
    SELECT COUNT(1) anum FROM course
    ) c
    ON 1=1
    GROUP BY 1,2,3,4
    HAVING MAX(c.anum)>COUNT(sc.Cno) ;

    12、

    SELECT s.*
    FROM student s
    INNER JOIN sc b
    ON s.Sno=b.Sno
    WHERE EXISTS(
        SELECT 1 FROM sc WHERE Sno='01' AND Cno=b.Cno
    )
    GROUP BY Sno;

    13、

    SELECT a.*
    ,COUNT(b.Cno) AS anum
    ,SUM(CASE WHEN EXISTS(SELECT 1 FROM sc WHERE Sno='01' AND Cno=b.Cno)THEN 1 ELSE 0 END) AS snum
    ,MAX(c.num1) AS num1
    FROM student a
    INNER JOIN sc b
    ON a.Sno=b.Sno
    INNER JOIN (SELECT COUNT(1) AS num1 FROM sc WHERE Sno='01') c
    ON 1=1
    GROUP BY 1
    HAVING anum=snum AND anum=num1 ;

    14、

    SELECT a.*
    FROM student a
    LEFT JOIN(
           SELECT a.s
           FROM student a
           LEFT JOIN sc b
           ON a.Sno=b.Sno
           LEFT JOIN course c
           ON b.Cno=c.Cno
           LEFT JOIN teacher d
           ON c.Tno=d.Tno AND d.Tname='张老师'
           WHERE d.Tno IS NOT NULL
           )b
    ON a.s=b.s
    WHERE b.s IS NULL ;

    15、

    SELECT a.Sno
    ,a.sname
    ,AVG(b.score)
    FROM student a
    INNER JOIN sc b
    ON a.Sno=b.Sno
    GROUP BY 1,2
    HAVING SUM(CASE WHEN b.score>=60 THEN 0 ELSE 1 END)>=2

    16、

    SELECT a.*
    ,b.score
    FROM student a
    INNER JOIN sc b
    ON a.Sno=b.Sno
    WHERE b.Cno='01' AND b.score < 60
    ORDER BY b.score DESC ;

    17、

    SELECT a.*
    ,SUM(CASE WHEN b.Cno='01' THEN b.score ELSE 0 END) AS s01
    ,SUM(CASE WHEN b.Cno='02' THEN b.score ELSE 0 END) AS s02
    ,SUM(CASE WHEN b.Cno='03' THEN b.score ELSE 0 END) AS s03
    ,AVG(CASE WHEN b.score IS NULL THEN 0 ELSE b.score END) avs
    FROM student a
    LEFT JOIN sc b
    ON a.Sno=b.Sno
    GROUP BY 1
    ORDER BY avs DESC ;

    18、

    SELECT a.Cno
    ,a.Cname
    ,MAX(b.score)
    ,MIN(b.score)
    ,AVG(b.score)
    ,SUM(CASE WHEN b.score>=60 THEN 1 ELSE 0 END)/COUNT(1)
    ,SUM(CASE WHEN b.score>=70 AND b.score<80 THEN 1 ELSE 0 END)/COUNT(1)
    ,SUM(CASE WHEN b.score>=80 AND b.score<90 THEN 1 ELSE 0 END)/COUNT(1)
    ,SUM(CASE WHEN b.score>=90 THEN 1 ELSE 0 END)/COUNT(1)
    FROM course a
    INNER JOIN sc b
    ON a.Cno=b.Cno
    GROUP BY 1,2 ;

    19、

    SET @r:=0;
    SELECT a.Cname,a.score,@r:=@r+1
    FROM(
    SELECT c.*,sc.score
    FROM course c
    INNER JOIN sc
    ON c.Cno=sc.Cno
    WHERE c.Cno='01'
    ORDER BY sc.Cno DESC
    )a

    20、

    SET @r:=0;
    SELECT a.*, @r:=@r+1 as '总成绩排名'
    FROM (
    SELECT sc.Sno, SUM(sc.score) as '总成绩'
    FROM sc
    GROUP BY sc.Sno 
    ORDER BY SUM(sc.score) DESC
    )a

    21、

    SELECT a.*
    ,b.Cname
    ,AVG(c.score) as core
    FROM teacher a
    INNER JOIN course b
    ON a.Tno=b.Tno
    INNER JOIN sc c
    ON b.Cno=c.Cno
    GROUP BY 1
    ORDER BY core DESC ;

    22、

    SELECT a.*,b.Cname,b.score
    FROM student a
    INNER JOIN (
         SELECT a.*,
         c.Cname,
         COUNT(b.Cno)+1 AS tp 
         FROM sc a 
         LEFT JOIN sc b 
         ON a.Cno=b.Cno AND a.score<b.score 
         LEFT JOIN course c 
         ON a.Cno=c.Cno 
         GROUP BY 1 
         HAVING COUNT(b.Cno)+1 IN(2,3) 
         
    ) b
    ON a.Sno=b.Sno

    23、

    SELECT a.Cno
    ,a.Cname
    ,SUM(CASE WHEN b.score<=100 AND b.score>85 THEN 1 ELSE 0 END) AS '[100-85]'
    ,SUM(CASE WHEN b.score<=85  AND b.score>70 THEN 1 ELSE 0 END) AS '[85-70]'
    ,SUM(CASE WHEN b.score<=70  AND b.score>60 THEN 1 ELSE 0 END) AS '[70-60]'
    ,SUM(CASE WHEN b.score<=60  AND b.score>0   THEN 1 ELSE 0 END) AS '[60-0]'
    ,SUM(CASE WHEN b.score<=100 AND b.score>85 THEN 1 ELSE 0 END)/COUNT(1) AS '[100-85]%'
    ,SUM(CASE WHEN b.score<=85  AND b.score>70 THEN 1 ELSE 0 END)/COUNT(1) AS '[85-70]%'
    ,SUM(CASE WHEN b.score<=70  AND b.score>60 THEN 1 ELSE 0 END)/COUNT(1) AS '[70-60]%'
    ,SUM(CASE WHEN b.score<=60  AND b.score>0   THEN 1 ELSE 0 END)/COUNT(1) AS '[60-0]%'
    FROM course a
    INNER JOIN sc b
    ON a.Cno=b.Cno
    GROUP BY 1,2 

    24、

    SELECT a.*,COUNT(b.Sno)+1
    FROM (
      SELECT a.*,AVG(CASE WHEN b.score IS NULL THEN 0 ELSE b.score END) AS ascore 
      FROM student a 
      LEFT JOIN sc b ON a.Sno=b.Sno 
      GROUP BY 1
    )a
    LEFT JOIN(
      SELECT a.*,AVG(CASE WHEN b.score IS NULL THEN 0 ELSE b.score END) AS ascore 
      FROM student a LEFT JOIN sc b ON a.Sno=b.Sno 
      GROUP BY 1
    )b 
    ON a.ascore<b.ascore 
    GROUP BY 5 DESC

    25、

    SELECT a.*,COUNT(b.Cno)+1 AS ascore
    FROM sc a
    LEFT JOIN sc b
    ON a.Cno=b.Cno AND a.score<b.score
    GROUP BY 1,2,3
    HAVING ascore<=3
    ORDER BY a.Cno,ascore

    26、

    SELECT a.*,COUNT(b.Sno)
    FROM course a
    LEFT JOIN sc b
    ON a.Cno=b.Cno
    GROUP BY 1,2,3 

    27、

    SELECT a.Sno,a.Sname
    FROM student a
    LEFT JOIN sc b
    ON a.Sno=b.Sno
    GROUP BY 1
    HAVING COUNT(b.Cno)=2

    30、

    SELECT Sname
    ,Ssex
    ,COUNT(1)
    FROM student
    GROUP BY 1,2
    HAVING COUNT(1) > 1 

    31、

    SELECT * FROM student WHERE YEAR(sage)=1994

    32、

    SELECT a.*
    ,AVG(b.score) AS ascore
    FROM course a
    LEFT JOIN sc b
    ON a.Cno=b.Cno
    GROUP BY 1,2,3
    ORDER BY ascore DESC ,a.Cno 

    33、

    SELECT a.*
    ,AVG(b.score) AS ascore
    FROM student a
    LEFT JOIN sc b
    ON a.Sno=b.Sno
    GROUP BY 1
    HAVING ascore>=85

    34、

    SELECT c.*
    FROM course a
    LEFT JOIN sc b
    ON a.Cno=b.Cno
    LEFT JOIN student c
    ON b.Sno=c.Sno
    WHERE a.Cname='数学'
    AND b.score < 60 

    35、

    SELECT *
    FROM sc a
    INNER JOIN student b
    ON a.Sno=b.Sno
    INNER JOIN course c
    ON a.Cno=c.Cno

    36、

    SELECT b.Sname
    ,c.Cname
    ,a.score
    FROM sc a
    INNER JOIN student b
    ON a.Sno=b.Sno
    INNER JOIN course c
    ON a.Cno=c.Cno
    WHERE a.score > 70

    38、

    SELECT a.Sno,a.Sname
    FROM student a
    INNER JOIN sc b
    ON a.Sno=b.Sno
    WHERE b.Cno='02' AND b.score > 80

    40、

    SELECT a.*,b.score
    FROM student a
    INNER JOIN sc b
    ON a.Sno=b.Sno
    INNER JOIN(
      SELECT c.Cno,MAX(c.score) AS maxscore
      FROM teacher a
      INNER JOIN course b
      ON a.Tno=b.Tno
      INNER JOIN sc c
      ON b.Cno=c.Cno
      WHERE a.Tname='张老师'
      GROUP BY Cno
    )c
    ON b.Cno=c.Cno AND b.score=c.maxscore

    41、

    SELECT a.Sno,a.Cno,a.score
    FROM sc a
    INNER JOIN (
    SELECT a.score,b.Sno,COUNT(1)
    FROM sc a
    INNER JOIN student b
    ON a.Sno=b.Sno
    GROUP BY a.score,b.Sno
    HAVING COUNT(1)>1
    )b
    ON a.Sno=b.Sno AND a.score=b.score

    43、

    SELECT a.Cno
    ,COUNT(1) AS pnum
    FROM sc a
    GROUP BY 1
    HAVING pnum > 5
    ORDER BY pnum DESC,a.Cno 

    46、

    SELECT a.Sname,YEAR(CURDATE())-YEAR(a.Sage) AS '年龄'
    FROM student a

    47、

    SELECT a.*
    FROM student a
    WHERE CONCAT(MONTH(a.Sage),DAY(a.Sage))>=CONCAT(MONTH(SUBDATE(CURDATE(),WEEKDAY(CURDATE()))),DAY(SUBDATE(CURDATE(),WEEKDAY(CURDATE()))))
    AND CONCAT(MONTH(a.Sage),DAY(a.Sage))<=CONCAT(MONTH(SUBDATE(CURDATE(),WEEKDAY(CURDATE())-6)),DAY(SUBDATE(CURDATE(),WEEKDAY(CURDATE())-6)))

    49、

    SELECT a.* FROM student a WHERE MONTH(a.Sage)=MONTH(CURDATE())

    50、

    SELECT a.* FROM student a WHERE MONTH(a.Sage)=MONTH(CURDATE())+1
  • 相关阅读:
    bzoj1221
    hdu3377
    bzoj3930
    bzoj3976
    bzoj4237
    fzu1977
    hdu1693
    ural1519
    bzoj1264
    回答自己的提问
  • 原文地址:https://www.cnblogs.com/lucky1024/p/11106133.html
Copyright © 2020-2023  润新知