MySQL经典练习50道

 

MySQL练习

50道经典SQL练习题

数据表介绍

  • 1.学⽣表 Student(SId,Sname,Sage,Ssex)

    • SId 学⽣编号

    • Sname 学⽣姓名

    • Sage 出⽣年⽉

    • Ssex 学⽣性别

  • 2.课程表 Course(CId,Cname,TId)

    • CId 课程编号

    • Cname 课程名称

    • TId 教师编号

  • 3.教师表 Teacher(TId,Tname)

    • TId 教师编号

    • Tname 教师姓名

  • 4.成绩表 SC(SId,CId,score)

  • SId 学⽣编号

  • CId 课程编号

  • score 分数

建表语句

  • 学⽣表 Student

    create table Student(
        SId varchar(10),
        Sname varchar(10),
        Sage datetime,
        Ssex varchar(10)
    ); 
  • 课程表 Course

    create table Course(
        CId varchar(10),
        Cname nvarchar(10),
        TId varchar(10)
    );
  • 教师表 Teacher

    create table Teacher(
        TId varchar(10),
        Tname varchar(10)
    );
  • 成绩表 SC

    create table SC(
        SId varchar(10),
        CId varchar(10),
        score decimal(18,1)
    );

插入数据

  • 学⽣表 Student

    -- 学生表 Student
    insert into Student values('01' , '赵雷' , '1990-01-01' , '男'); 
    insert into Student values('02' , '钱电' , '1990-12-21' , '男'); 
    insert into Student values('03' , '孙风' , '1990-12-20' , '男'); 
    insert into Student values('04' , '李云' , '1990-12-06' , '男'); 
    insert into Student values('05' , '周梅' , '1991-12-01' , '女'); 
    insert into Student values('06' , '吴兰' , '1992-01-01' , '女'); 
    insert into Student values('07' , '郑竹' , '1989-01-01' , '女'); 
    insert into Student values('09' , '张三' , '2017-12-20' , '女'); 
    insert into Student values('10' , '李四' , '2017-12-25' , '女'); 
    insert into Student values('11' , '李四' , '2012-06-06' , '女'); 
    insert into Student values('12' , '赵六' , '2013-06-13' , '女'); 
    insert into Student values('13' , '孙七' , '2014-06-01' , '女'); 
  • 课程表 Course

    -- 科⽬表 Course 
    insert into Course values('01' , '语文' , '02'); 
    insert into Course values('02' , '数学' , '01'); 
    insert into Course values('03' , '英语' , '03');
  • 教师表 Teacher

    -- 教师表 Teacher 
    insert into Teacher values('01' , '张三'); 
    insert into Teacher values('02' , '李四'); 
    insert into Teacher values('03' , '王五'); 
  • 成绩表 SC

    -- 成绩表 SC 
    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);

练习题目含答案

1.查询" 01 “课程⽐” 02 "课程成绩⾼的学⽣的信息及课程分数
SELECT  tt1.SId
        ,tt2.Sname
        ,tt3.CId
        ,tt3.score
from (
    select  t1.SId
    from(
        select  SId
                ,CId
                ,score 
        from SC 
        where CId = '01'
    ) t1
    left join(
        select  SId
                ,CId
                ,score 
        from SC 
        where CId = '02'
    ) t2
    on t1.Sid = t2.Sid
    where t1.Score > t2.Score
) tt1 join Student tt2 on tt1.Sid = tt2.Sid
join SC tt3 on tt1.Sid = tt3.Sid;
2.查询同时存在" 01 “课程和” 02 "课程的情况
SELECT  t1.SId
from(
    select  SId
    from SC 
    where CId = '01'
) t1 join (
    select SId
    from SC 
    where CId = '02'
) t2 on t1.SId = t2.SId;
3.查询存在" 01 “课程但可能不存在” 02 "课程的情况(不存在时显示为 null )
SELECT  t1.SId
        ,t1.CId
        ,t1.score
        ,t2.CId as t2CId
        ,t2.score as t2Score
from(
    select  SId
            ,CId
            ,score
    from SC 
    where CId = '01'
) t1 left join (
    select  SId
            ,CId
            ,score
    from SC 
    where CId = '02'
) t2 on t1.SId = t2.SId;
4.查询不存在" 01 “课程但存在” 02 "课程的情况
SELECT  t1.SId
        ,t1.CId
        ,t1.score
        ,t2.CId as t2CId
        ,t2.score as t2Score
from(
    select  SId
            ,CId
            ,score
    from SC 
    where CId = '02'
) t1 left join (
    select  SId
            ,CId
            ,score
    from SC 
    where CId = '01'
) t2 on t1.SId = t2.SId;
5.查询平均成绩⼤于等于 60 分的同学的学⽣编号和学⽣姓名和平均成绩
SELECT  t1.SId
        ,t2.SName
        ,t1.avg_score
from(
    SELECT  SId
            ,round(AVG(score),2) as avg_score
    FROM SC
    GROUP BY SId
    HAVING avg_score>=60
) t1 join Student t2
on t1.SId = t2.SId

PS:round(m,n)函数将字段m的查询结果保留小数点n位

6.查询在 SC 表存在成绩的学⽣信息
SELECT  t1.SId
        ,t2.SName
from (
    SELECT distinct SId
    from SC
) t1 join Student t2
on t1.SId = t2.SId

PS:DISTINCT 将后面的字段的查询结果进行去重。

7.查询所有同学的学⽣编号、学⽣姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
select  t2.SId
        ,t2.SName
        ,t1.cnt
        ,t1.sum_score
from(
    select  SId
            ,count(CId) as cnt
            ,sum(score) as sum_score
    from SC 
    group by SId
) t1 right join
Student t2 on t1.SId = t2.SId 
8.查询「李」姓⽼师的数量
SELECT COUNT(Tname)
FROM Teacher
WHERE Tname LIKE '李%';
9.查询学过「张三」⽼师授课的同学的信息
select  t2.SId
        ,t2.SName
from (
    select  SId
    from SC
    where CId =(
        select  CId
        from Course
        where TId = (
            select  TId
            from Teacher
            where Tname = '张三'
        )
    )
) t1 join Student t2 on t1.SId = t2.SId;
10.查询没有学全所有课程的同学的信息
select  tt1.SId
        ,tt1.SName
        ,count(tt1.CId) as cnt
from (
    select  t1.SId
            ,t1.CId
            ,t2.SName
    from SC t1 join Student t2
    on t1.SId = t2.SID
) tt1 group by tt1.SId,tt1.SName
Having cnt < (select  count(*) from Course)
11.查询⾄少有⼀⻔课与学号为" 01 "的同学所学相同的同学的信息
SELECT  DISTINCT t1.SId
                ,t2.Sname
from SC t1 join Student t2 on t1.SId = t2.SId
where t1.SId!='01' and t1.CId in (SELECT cid FROM SC WHERE sid = '01')
12.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息
SELECT  ttt1.SId
        ,ttt2.SName
from(
    SELECT  tt1.SId
            ,count(tt1.t1CId) as t1Cnt
            ,count(tt1.t2CId) as t2Cnt
    from(
        SELECT  t1.CId as t1CId
                ,t2.SId
                ,t2.CId as t2CId
        FROM (
            SELECT  SId
                    ,CId 
            FROM SC WHERE SId = '01'
        ) t1 right join(
            SELECT  SId
                    ,CId 
            FROM SC WHERE SId != '01'
        ) t2 on t1.CId = t2.CId
        order by t1.SId,t2.SId
    ) tt1 group by tt1.SId
    HAVING t1Cnt = t2Cnt and t2Cnt = (SELECT  count(*) from SC where Sid = '01')
) ttt1 join Student ttt2 on ttt1.SId = ttt2.SId;
13.查询没学过"张三"⽼师讲授的任⼀⻔课程的学⽣姓名
select  SId
        ,SName
from Student
where SId not in (
    select  SId
    from SC
    where CId in (
        select  CId
        from Course
        where TId = (
            select  TId
            from Teacher
            where Tname = '张三'
        )
    )
);
14.查询两⻔及其以上不及格课程的同学的学号,姓名及其平均成绩
select  tt2.SId
        ,tt2.SName
        ,tt1.avg_score
from (
    select  t1.SId
            ,count(t1.CId) as cnt
            ,avg(t1.score) as avg_score
    from(
        select  SId
                ,CId
                ,score
        from SC
        where score < 60
    ) t1 group by SId having cnt >=2
) tt1 join Student tt2 on tt1.SId = tt2.SId;
15.检索" 01 "课程分数⼩于 60,按分数降序排列的学⽣信息
select  t2.SId
        ,t2.Sname
        ,t1.score
from SC t1
join Student t2
on t1.SId = t2.SId
and t1.CId = '01' and t1.score < 60
order by t1.score desc;
16.按平均成绩从⾼到低显示所有学⽣的所有课程的成绩以及平均成绩
select  tt1.SId
        ,tt1.avg_score
        ,tt2.score as 'Chinese'
        ,tt3.score as 'Math'
        ,tt4.score as 'English'
from (
    select  t1.SId
            ,avg(t1.score) as avg_score
    from SC t1
    group by t1.SId
) tt1 
left join (select SId,score from SC where CId = '01') tt2 on tt1.SId = tt2.SId
left join (select SId,score from SC where CId = '02') tt3 on tt1.SId = tt3.SId
left join (select SId,score from SC where CId = '03') tt4 on tt1.SId = tt4.SId
order by tt1.avg_score desc;
17.查询各科成绩最⾼分、最低分和平均分: 以如下形式显示:课程 ID,课程 name,最⾼分,最低分,平均分,及格率,中等率,优良率,优秀率 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90要求输出课程号和选修⼈数,查询结果按⼈数降序排列,若⼈数相同,按课程号升序排列
select  t1.CId
        ,t2.Cname
        ,max(score) as max_score
        ,min(score) as min_score
        ,avg(score) as avg_score
        ,concat(round(sum(if(score>=60,1,0))*100/count(score),2),"%") as 及格率
        ,concat(round(sum(if(score>=70 and score<80,1,0))*100/count(score),2),"%") as 中等率
        ,concat(round(sum(if(score>=80 and score<90,1,0))*100/count(score),2),"%") as 优良率
        ,concat(round(sum(if(score>=90,1,0))*100/count(score),2),"%") as 优秀率
        ,count(score) as num
from SC t1 join Course t2 on t1.CId = t2.CId
group by t1.CId,t2.Cname
order by num desc,CId asc;

concat()用作字符串拼接

concat(‘1’ , ’+’ , ’1’ , ‘=’ , ‘2’) ==> 1+1=2

case when 条件判断

CASE WHEN sc.score>=60 THEN 1 ELSE 0 END

意思是sc.score>=60这个条件成立时,返回1,不成立时返回0

18.按各科平均成绩进⾏排序,并显示排名,Score 重复时保留名次空缺
SET @i := 0; -- 定义一个变量
SELECT  t1.CId
        ,t1.avg_score
        ,@i := @i + 1 as rank
from (
    select  CId
            ,avg(score) as avg_score
    from SC 
    group by CId
    order by avg_score desc
) t1;
19.按各科平均成绩进⾏排序,并显示排名, Score 重复时不保留名次空缺
SET @i := 0;
SELECT  s.cid
        ,s.avg_sc AS '平均分'
        ,@i := @i + 1 AS '排名' 
FROM (
    SELECT  SC.`CId`
            ,AVG(SC.`score`) AS avg_sc 
    FROM SC 
    GROUP BY SC.`CId` 
    ORDER BY avg_sc DESC
)s;
20.查询学⽣的总成绩,并进⾏排名,总分重复时保留名次空缺

1 2 2 4 5 6

SET @i := 0;
SET @j := 0;
SET @p := 0;
SET @q := 0;
select  t1.SId
        ,t1.sum_score
        ,@j := @j + 1
        ,@p := t1.sum_score
        ,if(@p=@q,@i,@i := @j) as rank
        ,@q :=@p
from (
    select  SId
            ,sum(score) as sum_score
    from SC
    group by SId
    order by sum_score desc
) t1;
21.查询学⽣的总成绩,并进⾏排名,总分重复时不保留名次空缺

1 2 2 2 3 4 5

SET @i := 0;
SET @p := 0;
SET @q := 0;
SELECT  t1.SId
        ,t1.sum_score
        ,@p := t1.sum_score
        ,if(@p=@q,@i,@i := @i+1) as dense_rank
        ,@q :=@p
​
from (
    select  SId
            ,sum(score) as sum_score
    from SC
    group by SId
    order by sum_score desc
) t1;
22.统计各科成绩各分数段⼈数:课程编号,课程名称,[100-85),[85-70),[70-60),[60-0)及所占百分⽐
select  t1.CId
        ,t2.Cname
        ,concat(round(sum(if(score<=100 and score>85,1,0))*100/count(score),2),"%") as '[100-85)'
        ,concat(round(sum(if(score<=85 and score>70,1,0))*100/count(score),2),"%") as '[85-70)'
        ,concat(round(sum(if(score<=70 and score>60,1,0))*100/count(score),2),"%") as '[70-60)'
        ,concat(round(sum(if(score<=60 and score>0,1,0))*100/count(score),2),"%") as '[60-0)'
from SC t1 join Course t2 on t1.CId = t2.CId
group by t1.CId,t2.Cname;
23.查询各科成绩前三名的记录
SET @i := 0;
SET @p := 0;
SET @q := 0;
SELECT  tt1.SId
        ,tt2.SName
        ,tt1.CId
        ,tt1.score
        ,tt1.rn
from (
    select  t1.SId
            ,t1.CId
            ,t1.score
            ,@p := t1.CId
            ,if(@p=@q,@i := @i + 1,@i :=1) as rn
            ,@q := @p
    from (
        select  SId
                ,CId
                ,score
        from SC
        order by CId,score DESC
    ) t1
) tt1 join Student tt2 on tt1.rn<=3 and tt1.SId = tt2.SId
order by tt1.CId,rn;

PS:UNION用的比较多union all是直接连接,取到得是所有值,记录可能有重复 union 是取唯一值,记录没有重复

1、UNION 的语法如下:

[SQL 语句 1]

UNION

[SQL 语句 2]

2、UNION ALL 的语法如下:

[SQL 语句 1]

UNION ALL

[SQL 语句 2]

24.查询每⻔课程被选修的学⽣数
SELECT  SC.`CId`
        ,COUNT(SC.`CId`) as cnt
FROM SC 
GROUP BY SC.`CId`;
25.查询出只选修两⻔课程的学⽣学号和姓名
select  t1.SId
        ,t2.SName
from (
    SELECT  SId
            ,COUNT(CId) as cnt
    FROM SC
    GROUP BY SId
    having cnt = 2
) t1 join Student t2 on t1.SId = t2.SId;
26.查询男⽣、⼥⽣⼈数
SELECT  st.ssex
        ,COUNT(1) 
FROM Student st 
GROUP BY st.ssex;
27.查询名字中含有「⻛」字的学⽣信息
SELECT * FROM Student WHERE Student.`Sname` LIKE '%风%'; 
28.查询同名同性学⽣名单,并统计同名⼈数
SELECT  SName
        ,SSex
        ,count(1) as cnt
from Student
group by SName,SSex
having cnt > 1;
29.查询 1990 年出⽣的学⽣名单
SELECT  s.* 
FROM Student s 
WHERE s.`Sage` BETWEEN '1990-1-1' AND '1990-12-31';
30.查询每⻔课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
SELECT  SC.CId
        ,c.Cname
        ,AVG(SC.score) AS avg_score
FROM SC 
JOIN Course c ON SC.CId = c.CId 
GROUP BY SC.CId,c.Cname
ORDER BY avg_score DESC,SC.CId;
31.查询平均成绩⼤于等于 85 的所有学⽣的学号、姓名和平均成绩
select  t1.SId
        ,t2.SName
        ,t1.avg_score
from (
    select  SId
            ,round(avg(score),2) as avg_score
    from SC 
    group by SId 
    having avg_score >= 85
) t1 join Student t2
on t1.SId = t2.SId;
32.查询课程名称为「数学」,且分数低于 60 的学⽣姓名和分数
select  t2.SName
        ,t1.score
from (
    SELECT  SId
            ,score
    from SC
    where CId = (
        select  CId
        from Course 
        where CName = '数学'
    ) and score<60
) t1 join Student t2
on t1.SId = t2.SId;
33.查询所有学⽣的课程及分数情况(存在学⽣没成绩,没选课的情况)
SELECT  t1.SName
        ,t2.语文成绩
        ,t2.数学成绩
        ,t2.英语成绩
from Student t1
left join (
    SELECT  SId
            ,sum(case CId when '01' then score else 0 end) as '语文成绩'
            ,sum(case CId when '02' then score else 0 end) as '数学成绩'
            ,sum(case CId when '03' then score else 0 end) as '英语成绩'
    from SC
    group by SId
) t2 on t1.SId = t2.SId;
34.查询课程成绩在 70 分以上的姓名、课程名称和分数
select  t2.SName
        ,t3.CName
        ,t1.score
from (
    SELECT  SId
            ,CId
            ,score
    from SC
    where score>70
) t1 join Student t2
on t1.SId = t2.SId
join Course t3
on t1.CId = t3.CId;
35.查询不及格的课程
select  distinct 
        t3.CName
from (
    SELECT  SId
            ,CId
            ,score
    from SC
    where score<60
) t1 join Student t2
on t1.SId = t2.SId
join Course t3
on t1.CId = t3.CId;
36.查询课程编号为 01 且课程成绩在 80 分及以上的学⽣的学号和姓名
select  t1.SId
        ,t2.SName
from (
    SELECT  SId
            ,score
    from SC
    where CId = '01' and score >= 80
) t1 join Student t2
on t1.SId = t2.SId;
37.求每⻔课程的学⽣⼈数
SELECT  SC.`CId`
        ,c.cname
        ,COUNT(SC.`SId`)
FROM SC
JOIN Course c
ON SC.`CId` = c.`CId`
GROUP BY SC.`CId`,c.`Cname`;
38.成绩不重复,查询选修「张三」⽼师所授课程的学⽣中,成绩最⾼的学⽣信息及其成绩
select t1.SId
        ,t2.SName
        ,t1.CId
        ,t1.score
from (
    select  SId
            ,CId
            ,score
    from SC
    where CId in (
        SELECT  CId
        from Course
        where TId = (
            select  TId
            from Teacher
            where TName = '张三'
        )
    )
    order by score desc
    limit 1
) t1 join Student t2
on t1.SId = t2.SId;
39.成绩有重复的情况下,查询选修「张三」⽼师所授课程的学⽣中,成绩最⾼的学⽣信息及其成绩
SET @i := 0;
SET @p := 0;
SET @q := 0;
select  tt1.SId
        ,tt1.CId
        ,tt1.score
        ,tt2.SName
from (
    SELECT  t1.SId
            ,t1.CId
            ,t1.score
            ,@p := t1.score
            ,if(@p=@q,@i,@i := @i+1) as dense_rank
            ,@q :=@p
    from (
        select  SId
                ,CId
                ,score
        from SC
        where CId in (
            SELECT  CId
            from Course
            where TId = (
                select  TId
                from Teacher
                where TName = '张三'
            )
        )
        order by score desc
    ) t1
) tt1 join Student tt2 
on tt1.dense_rank = 1 
and tt1.SId = tt2.SId
40.查询不同课程成绩相同的学⽣的学⽣编号、课程编号、学⽣成绩
select  distinct
                t3.SName
                ,t1.CId
                ,t1.score
from SC t1
join SC t2
on t1.score = t2.score and t1.CId != t2.CId
join Student t3 on t1.SId = t3.SId;
41.查询每⻔课程成绩最好的前两名
SET @i := 0;
SET @p := 0;
SET @q := 0;
SELECT  tt1.SId
        ,tt2.SName
        ,tt1.CId
        ,tt1.score
        ,tt1.rn
from (
    select  t1.SId
            ,t1.CId
            ,t1.score
            ,@p := t1.CId
            ,if(@p=@q,@i := @i + 1,@i :=1) as rn
            ,@q := @p
    from (
        select  SId
                ,CId
                ,score
        from SC
        order by CId,score DESC
    ) t1
) tt1 join Student tt2 on tt1.rn<=2 and tt1.SId = tt2.SId
order by tt1.CId,rn
42.统计每⻔课程的学⽣选修⼈数(超过 5 ⼈的课程才统计)。
SELECT  SC.`CId`
        ,COUNT(SC.`SId`) AS '选课人数'
FROM SC
GROUP BY SC.`CId`
HAVING 选课人数 > 5;
43.检索⾄少选修两⻔课程的学⽣学号
SELECT  SC.`SId`
        ,COUNT(SC.`CId`) AS '选课数量'
FROM SC
GROUP BY SC.`SId`
HAVING 选课数量 >=2;
44.查询选修了全部课程的学⽣信息
SELECT  s.`SId`
        ,s.`Sname`
        ,COUNT(SC.`CId`) AS 选课数量
FROM Student s
JOIN SC ON SC.`SId` = s.`SId`
GROUP BY s.`SId`,s.`Sname`
HAVING COUNT(SC.`CId`) = (SELECT COUNT(c.`CId`) FROM Course c);
45.查询各学⽣的年龄,只按年份来算

常见的日期操作:

select DATE_FORMAT(STR_TO_DATE('2022数01加14牛','%Y数%m加%d牛'),'%Y');

select FROM_UNIXTIME( UNIX_TIMESTAMP(STR_TO_DATE('2022数01加14牛','%Y数%m加%d牛')) + 4243600,'%Y/%m/%d');

SELECT  year(now()) - date_format(Sage,'%Y') from Student;
46.按照出⽣⽇期来算,当前⽉⽇ < 出⽣年⽉的⽉⽇则年龄减⼀
SELECT  SId 
        ,SName
        ,case when now_month_day<month_day then age-1 else age end as new_age
        ,age
from (
    SELECT  SId 
            ,SName
            ,(year(now()) - date_format(Sage,'%Y') ) as age
            ,date_format(Sage,'%m-%d') as month_day
            ,date_format(now(),'%m-%d') as now_month_day
    from Student
) t1
47.查询本周过⽣⽇的学⽣
SELECT  SId
        ,SName
        ,Sage
        ,week(Sage)
from Student
where week(Sage) = week(now())

ps:

week()函数参见下列网址

https://blog.csdn.net/moakun/article/details/82528773

date_format()函数参见下列网址

https://www.w3school.com.cn/sql/func_date_format.asp

48.查询下周过⽣⽇的学⽣
SELECT  SId
        ,SName
        ,Sage
        ,week(Sage)
from Student
where week(Sage) = week(date_add(now(),INTERVAL 1 Week))
49.查询本⽉过⽣⽇的学⽣
SELECT  SId
        ,SName
        ,Sage
        ,month(Sage)
from Student
where month(Sage) = month(now())
50.查询下⽉过⽣⽇的学⽣
SELECT  SId
        ,SName
        ,Sage
        ,month(Sage)
from Student
where month(Sage) = month(date_add(now(),INTERVAL 1 Month))
posted @ 2022-01-14 21:09  赤兔胭脂小吕布  阅读(167)  评论(0编辑  收藏  举报