美文网首页
网上流传的50道SQL题刷一刷(使用mysql和hive)

网上流传的50道SQL题刷一刷(使用mysql和hive)

作者: yayooo | 来源:发表于2021-07-28 09:02 被阅读0次
    • 前言:因为hive支持开窗函数的缘故,会在某些场景比mysql省很多事,该篇以网上流传的50道SQL题为例,比较SQL和HQL的实现。21题难度最大。
      附上万能图解:


      Join图

    一、数据表介绍

    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 分数


    二、建表,插入数据

    1.学生表 student

    • mysql建表 / mysql插入数据:
    DROP TABLE IF EXISTS student;
    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-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('08' , '吴八' , '2017-12-20' , '女');
    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' , '女');
    INSERT INTO student VALUES('14' , '十四' , '2014-06-01' , '男');
    
    • hive建表 / hvie插入数据:
    DROP TABLE IF EXISTS student;
    CREATE TABLE student(
      sid int,
      sname string,
      sage string,
      ssex string
    )
    row format delimited fields terminated by '\t';
    
    
    insert into table  student values('01' , 'zhaolei' , '1990-01-01' , 'man'),('02' , 'qiandian' , '1990-12-21' , 'man'),('03' , 'sunfeng' , '1990-12-20' , 'man'),('04' , 'liyun' , '1990-12-06' , 'man'),('05' , 'zhoumei' , '1991-12-01' , 'female'),('06' , 'wulan' , '1992-01-01' , 'female'),('07' , 'zhenzhu' , '1989-01-01' , 'female'),('08' , 'wuba' , '1989-01-01' , 'female'),('09' , 'zhangsan' , '2017-12-20' , 'female'),('10' , 'lisi' , '2017-12-25' , 'female'),('11' , 'lisi' , '2012-06-06' , 'female'),('12' , 'zhaoliu' , '2013-06-13' , 'female'),('13' , 'sunqi' , '2014-06-01' , 'female'),('14' , 'shisi' , '2014-06-01' , 'man');
    
    

    2.科目表 course

    • mysql建表 / mysql插入数据:
    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');
    
    • hive建表 / hvie插入数据:
    create table course(
      cid int,
      cname string,
      tid int
    )
    row format delimited fields terminated by '\t';
    
    
    INSERT INTO table course VALUES('01' , 'chinise' , '02');
    INSERT INTO table course VALUES('02' , 'math' , '01');
    INSERT INTO table course VALUES('03' , 'english' , '03');
    
    上面这种方式插入有点慢,用下面这种方式
    
    insert into table course values('01' , 'chinise' , '02'),('02' , 'math' , '01'),('03' , 'english' , '03');
    

    3.教师表 teacher

    • mysql建表 / mysql插入数据:
    CREATE TABLE teacher(tid VARCHAR(10),tname VARCHAR(10));
    
    INSERT INTO teacher VALUES('01' , '张三');
    INSERT INTO teacher VALUES('02' , '李四');
    INSERT INTO teacher VALUES('03' , '王五');
    
    • hive建表 / hvie插入数据:
      create table teacher(
      tid int,
      tname string
    )
    row format delimited fields terminated by '\t';
    
    insert into table Teacher values('01' , 'zhangsan'),('02' , 'lisi'),('03' , 'wangwu');
    

    4.成绩表 sc

    • mysql建表 / mysql插入数据:
    CREATE TABLE sc(sid VARCHAR(10),cid 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' , 69);
    INSERT INTO sc VALUES('07' , '02' , 90);
    INSERT INTO sc VALUES('07' , '03' , 98);
    INSERT INTO sc VALUES('08' , '01' , 65);
    INSERT INTO sc VALUES('08' , '02' , null);
    INSERT INTO sc VALUES('08' , '03' , 98);
    
    • hive建表 / hvie插入数据:
    create table sc(
      sid int,
      cid int,
      score int
    )
    row format delimited fields terminated by '\t';
    
    insert into table  sc values('01' , '01' , 80),('01' , '02' , 90),('01' , '03' , 99),('02' , '01' , 70),('02' , '02' , 60),('02' , '03' , 80),('03' , '01' , 80),('03' , '02' , 80),('03' , '03' , 80),('04' , '01' , 50),('04' , '02' , 30),('04' , '03' , 20),('05' , '01' , 76),('05' , '02' , 87),('06' , '01' , 31),('06' , '03' , 69),('07' , '02' , 90),('07' , '03' , 98),('08' , '01' , 65),('08' , '02' , null),('08' , '03' , 98);
    

    三、题目

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

    2.查询同时存在" 01 "课程和" 02 "课程的成绩情况

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

    4.查询不存在" 01 "课程但存在" 02 "课程的成绩情况

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

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

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

    8.查有成绩的学生信息

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    30.查询男生、女生人数

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

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

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

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

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

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

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

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

    39.查询不及格的课程

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


    四、答案

    答案不固定,各位如果有好的优化方法可以在评论区贴上。

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

    第一种写法:

    select a1.sid,
           a1.sname,
           a1.sage,
           a1.ssex,
           a2.score1,
           a2.score2
    from student a1
             right join (
        select t1.sid,
               score1,
               score2
        from (select sid, score as score1 from sc where cid = '01') t1,
             (select sid, score as score2 from sc where cid = '02') t2
        where t1.sid = t2.sid
          and t1.score1 > t2.score2
    )a2
    on a1.sid=a2.sid
    ;
    

    去看一下MySql查询select from 两个表与left join on的区别
    https://blog.csdn.net/weixin_33738555/article/details/91689609

    第二种写法

    select a1.sid,
           a1.sname,
           a1.sage,
           a1.ssex,
           a2.score1,
           a2.score2
    from student a1
             right join (
        select t1.sid,
               t1.score1 as score1,
               t2.score2 as score2
        from (
                 select sid,
                        score as score1
                 from sc
                 where cid = '01'
             ) t1
                 left join (
            select sid,
                   score as score2
            from sc
            where cid = '02'
        ) t2
                           on t1.sid = t2.sid
        where t1.score1 > t2.score2
    ) a2
                        on a1.sid = a2.sid
    ;
    
    
    2.查询同时存在" 01 "课程和" 02 "课程的成绩情况
    select t1.sid,
           t1.score1,
           t2.score2
    from
    (select sid,score as score1 from sc where cid = '01')t1,
    (select sid,score as score2 from sc where cid = '02')t2
    where t1.sid = t2.sid;
    

    同时存在即:\color{red}{内连接}

    select t1.sid,
           t1.score1,
           t2.score2
    from
    (select sid,score as score1 from sc where cid = '01')t1
    join 
    (select sid,score as score2 from sc where cid = '02')t2
    where t1.sid = t2.sid;
    
    3.查询存在" 01 "课程但可能不存在" 02 "课程的成绩情况(不存在时显示为 null )

    即:\color{red}{左连接}

    select t1.sid,
           t1.cid,
           t1.score
    from sc t1
    where t1.sid not in(
        select sid from sc where cid = '02'
        )
    and t1.cid = '01'
    ;
    
    select t1.sid,
           t1.score1,
           t2.score2
    from
    (select sid,score as score1 from sc where cid = '01')t1
    left join
    (select sid,score as score2 from sc where cid = '02')t2
    where t1.sid = t2.sid;
    
    4.查询存在" 01 “课程但不存在” 02 "课程的成绩情况
    select *
    from (select sid, score as score1 from sc where cid = '01') t1
             left join
             (select sid, score as score2 from sc where cid = '02') t2
             on t1.sid = t2.sid
    where t2.sid is null;
    
    5.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩

    mysql 写法:

    select t1.sid,
           a1.sname,
           t1.avg_score
    from student a1
             right join (
        select sid,
               sum(score) / count(sid) as avg_score
        from sc
        group by sid
        having avg_score > 60
    ) t1
                        on a1.sid = t1.sid;
    
    

    hive似乎无法使用having,一直报错,有待考证

    select t2.sid,
           a1.sname,
           t2.avg_score
    from student a1
             right join (
        select t1.sid,
               avg_score
        from (
                 select sid,
                        sum(score) / count(sid) as avg_score
                 from sc
                 group by sid
             ) t1
        where t1.avg_score > 60.0
    ) t2
                        on a1.sid = t2.sid;
    
    
    6.查询在 SC 表存在成绩的学生信息
    select distinct (a1.sid),
                    a2.sname,
                    a2.sage,
                    a2.ssex
    from sc a1
             left join student a2 on a1.sid = a2.sid;
    
    
    7.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
    select a1.sid,
           a1.sname,
           a2.c_cnt,
           a2.s_cnt
    from student a1
             left join (
        select t1.sid,
               count(t1.cid) as c_cnt,
               sum(score)    as s_cnt
        from sc t1
        group by t1.sid
    ) a2
                       on a1.sid = a2.sid;
    
    

    思考:

    --笛卡尔积
    select a1.sid, a1.sname, a1.sage, a1.ssex,
           a2.cnt,a2.snt
    from student a1,
         (select t1.sid, count(t1.cid) as cnt, sum(score) snt from sc t1 group by t1.sid) a2;
    
    --join
    select a1.sid, a1.sname, a1.sage, a1.ssex,
           a2.cnt,a2.snt
    from student a1,
         (select t1.sid, count(t1.cid) as cnt, sum(score) snt from sc t1 group by t1.sid) a2
    where a1.sid = a2.sid;
    
    --left join
    select a1.sid,
           a1.sname,
           a2.c_cnt,
           a2.s_cnt
    from student a1
             left join (
        select t1.sid,
               count(t1.cid) as c_cnt,
               sum(score)    as s_cnt
        from sc t1
        group by t1.sid
    ) a2
                       on a1.sid = a2.sid;
    
    
    8.查有成绩的学生信息

    这一题涉及到in和exists的用法,在这种小表中,两种方法的效率都差不多,但是请参考SQL查询中in和exists的区别分析
    当表2的记录数量非常大的时候,选用exists比in要高效很多.
    EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False.
    结论:IN()适合B表比A表数据小的情况
    结论:EXISTS()适合B表比A表数据大的情况

    --8.1 join
    select a1.*
    from student a1
              join (
        select distinct (t1.sid) as sid
        from sc t1
    ) a2
                       on a1.sid = a2.sid;
    
    -- 8.2 exists
    select *
    from student
    where exists (select sc.sid from sc where student.sid = sc.sid);
    
    -- 8.3 in
    select *
    from student
    where student.sid in (select distinct (sid) from sc);
    
    
    9.查询「李」姓老师的数量

    模糊查询:\color{red}{like}

    select count(*)
    from teacher a1
    where a1.tname like 'li%'
    
    10.查询学过「张三」老师授课的同学的信息
    select t4.*
    from student t4
             right join (
        select t3.sid
        from sc t3
                 right join (
            select t2.cid
            from course t2
                     right join (
                select t1.tid
                from teacher t1
                where t1.tname = 'zhangsan'
            ) a1
                                on t2.tid = a1.tid
        ) a2
                            on t3.cid = a2.cid
    ) a3
                        on t4.sid = a3.sid;
    

    写法2:

    select *
    from student a1,sc a2,teacher a3,course a4
    where a1.sid = a2.sid
    and a2.cid = a4.cid
    and a4.tid = a3.tid
    and a3.tname = 'zhangsan';
    
    11.查询没有学全所有课程的同学的信息
    • mysql写法
    select t3.*
    from student t3
             right join (
        select t2.sid,
               count(t2.cid) as cnt
        from sc t2
        group by t2.sid
        having cnt < (
            select count(distinct (t1.cid))
            from course t1
        )
    )a1
    on t3.sid = a1.sid
    ;
    
    
    • hive 写法
    一直报错...
    
    12.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
    select t3.*
    from student t3
             right join (
        select distinct t2.sid
        from sc t2
        where t2.cid in (
            select t1.cid
            from sc t1
            where t1.sid = '01'
        )
    ) a1
    on t3.sid = a1.sid;
    
    
    13.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息

    思路:
    1)查出"01"号学生的课程数目
    2)查出"01"号学生的课程内容
    3)查出课程在"01"学生所学的课程内容中并且课程数目和"01"学生所学课程数目相等的学生
    mysql写法:

    select t4.*
    from student t4
             right join (
        select a1.sid
        from (
                 select t3.sid as sid,
                        t3.cid as cid
                 from sc t3
                 where t3.cid in (
                     select t2.cid
                     from sc t2
                     where t2.sid = '01'
                 )
             ) a1
        group by sid
        having count(sid) = (select count(distinct t1.cid)
                             from sc t1
                             where t1.sid = '01')
    ) a2
                        on t4.sid = a2.sid
    ;
    
    14.查询没学过"张三"老师讲授的任一门课程的学生姓名
    --mysql 写法
    select distinct t4.sname
    from student t4
             right join (
        select t3.sid
    
        from sc t3
        where t3.cid not in (
            select t2.cid
            from course t2
            where tid in (
                select t1.tid
                from teacher t1
                where t1.tname = 'zhangsan'
            )
        )
    )a1
    on t4.sid = a1.sid;
    
    
    15.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
    select a1.sid,
           t2.sname
    from student t2
             right join (
        select t1.sid,
               sum(t1.score) / count(t1.sid)
        from sc t1
        where t1.score < 60
        group by t1.sid
        having count(t1.sid) > 2
    ) a1
                        on t2.sid = a1.sid;
    
    
    16.检索" 01 "课程分数小于 60,按分数降序排列的学生信息
    -- 写法1
    select t2.*,
           a1.score
    from student t2
             right join (
        select t1.sid,
               t1.score as score
        from sc t1
        where t1.cid = '01'
          and t1.score < 60
    )a1
    on a1.sid = t2.sid
    order by a1.score desc;
    
    --写法2
    select t1.*,
           t2.score
    from student t1
             right join sc t2
                        on t1.sid = t2.sid
    where t2.cid = '01'
      and t2.score < 60
    order by t2.score desc;
    
    17.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
    -- mysql /hive 写法
    select a1.sid1,
           a1.score,
           a2.avg_score
    from
    (select t1.sid as sid1,t1.score as score from sc t1)a1,
    (select t2.sid as sid2,avg(t2.score) as avg_score from sc t2 group by t2.sid) a2
    where a1.sid1 = a2.sid2
    order by a2.avg_score desc
    ;
    
    --hive 写法(开窗口)
    select t1.sid,
           t1.score,
           avg(score) over (partition by t1.sid) as avg_score
    from sc t1
    order by avg_score desc
    ;
    
    18.查询各科成绩最高分、最低分和平均分:
    select t1.cid,
           max(t1.score),
           min(t1.score),
           avg(t1.score)
    from sc t1
    group by t1.cid;
    
    19.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率;及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90;要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
    select t1.cid,
           max(t1.score),
           min(t1.score),
           avg(t1.score),
           count(*) as cnt,
           sum(case when t1.score >= 60 then 1 else 0 end ) / count(*) ,
           sum(case when t1.score > 60 and t1.score <= 70 then 1 else 0 end ) / count(*),
           sum(case when t1.score > 70 and t1.score <= 80 then 1 else 0 end ) / count(*),
           sum(case when t1.score > 80 and t1.score <= 90 then 1 else 0 end ) / count(*),
           sum(case when t1.score >= 90 then 1 else 0 end ) / count(*)
    from sc t1
    group by t1.cid
    order by cnt desc , t1.cid
    ;
    
    20.按各科成绩进行排序,并显示排名, score 重复时,合并(即排名:1,1,3....)

    思路: 自交,sc t1自己left join 自己t2,查询t2表中在t1表当前分数的高还有几个

    • mysql
    --mysql
    --思路: sc t1自己left join 自己t2,查询t2表中在t1表当前分数的高还有几个
    select t1.cid,t1.sid,t1.score,count(t2.score) + 1 as rank, t2.cid,t2.sid,t2.score
    from sc t1
    left join sc t2
    on t1.score < t2.score and t1.cid = t2.cid
    group by t1.cid, t1.sid, t1.score
    order by t1.cid, rank asc
    ;
    
    -- 或者
    select a.sid,a.cid,a.score,count(b.cid)+1 as rank from sc a 
    left join sc b on a.cid = b.cid and a.score<b.score
    group by a.cid, a.sid
    order by a.cid, rank asc;
    
    • hive
    -- hive (rank排序打标记)
    -- 排序相同时会重复,总数不变 1,1,3...
    select t1.cid,t1.sid,t1.score,
           rank() over (partition by t1.cid order by t1.score desc) as rank
    from sc t1;
    
    -- 排序相同时会重复,总数变少 1,1,3...
    select t1.cid,t1.sid,t1.score,
           dense_rank() over (partition by t1.cid order by t1.score desc) as rank
    from sc t1;
    
    -- 会根据顺序计算, 1,2,3...
    select t1.cid,t1.sid,t1.score,
           row_number() over (partition by t1.cid order by t1.score desc) as rank
    from sc t1;
    
    21.按各科成绩进行排序,并显示排名, score 重复时,不合并(即排名:1,2,3...)
    • mysql
      思路,用order by a,b 就能够对cid字段进行分组,再对score进行排序。用两个参数,第一个字段记录上一个cid,第二个字段用来标记当前行cid是否为上一个cid,如果是就自加,不是就赋值为1。
    set @rankid =0;
    set @couse_id =0;
    select a.*,
    @rankid := if(@couse_id = cid,@rankid + 1, 1) rankid,
    @couse_id := a.cid couse_id
    from sc a
    order by cid,score desc;
    
    

    可参考:https://blog.csdn.net/qq_41902618/article/details/108514316

    • hive
    -- 会根据顺序计算, 1,2,3...
    select t1.cid,t1.sid,t1.score,
           row_number() over (partition by t1.cid order by t1.score desc) as rank
    from sc t1;
    
    22.查询学生的总成绩,并进行排名,总分重复时,不合并(即排名:1,2,3...)
    • mysql
      mysql 变量:\color{red}{变量}

    mysql写法1

    set @crank = 0;
    select a1.sid,
           a1.total_score,
           @crank := @crank + 1 as rank
    from (
             select t1.sid,
                    sum(t1.score) as total_score
             from sc t1
             group by t1.sid
             order by total_score desc
         ) a1
    ;
    
    23.查询学生的总成绩,并进行排名,总分重复时,合并(即排名:1,1,3....)
    • mysql
      思路:通过自交的方式,求temp3中在temp2中大于当前分数的个数
    DROP TABLE IF EXISTS temp2;
    create temporary table temp2
    select t1.sid,
           sum(t1.score) as total_score
    from sc t1
    group by t1.sid;
    
    DROP TABLE IF EXISTS temp3;
    create temporary table temp3
    select t1.sid,
           sum(t1.score) as total_score
    from sc t1
    group by t1.sid;
    
    select t2.sid,
           t2.total_score,
           count(t3.total_score) + 1 rank
    from temp2 t2 left join temp3 t3
    on t2.total_score < t3.total_score
    group by t2.sid, t2.total_score
    order by rank asc;
    
    • hive
    --hive
    select a1.sid,
           a1.total_score,
           rank() over (order by a1.total_score desc)
    from (
             select t1.sid,
                    sum(t1.score) as total_score
             from sc t1
             group by t1.sid
         ) a1
    ;
    
    
    24.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
    select t2.cname,
           a1.*
    from course t2
             right join (
        select t1.cid                                                                         as cid,
               sum(case when t1.score <= 60 then 1 else 0 end)                                as p_per1,
               sum(case when t1.score >= 60 and t1.score <= 70 then 1 else 0 end)             as p_per2,
               sum(case when t1.score >= 70 and t1.score <= 85 then 1 else 0 end)             as p_per3,
               sum(case when t1.score >= 85 and t1.score <= 100 then 1 else 0 end)            as p_per4,
               sum(case when t1.score <= 60 then 1 else 0 end) / count(*)                     as c_per1,
               sum(case when t1.score >= 60 and t1.score <= 70 then 1 else 0 end) / count(*)  as c_per2,
               sum(case when t1.score >= 70 and t1.score <= 85 then 1 else 0 end) / count(*)  as c_per3,
               sum(case when t1.score >= 85 and t1.score <= 100 then 1 else 0 end) / count(*) as c_per4
        from sc t1
        group by t1.cid
    ) a1
                        on t2.cid = a1.cid
    ;
    
    
    25.查询各科成绩前三名的记录
    • mysql
      写法1:计算比自己分数大的记录有几条,如果小于3 就select,因为对前三名来说不会有3个及以上的分数比自己大了,最后再对所有select到的结果按照分数和课程编号排名即可。
    select *
    from sc t1
    where (
              select count(*)
              from sc t2
              where t1.cid = t2.cid
                and t1.score < t2.score
          ) < 3
                and t1.score is not  null
    order by t1.cid, t1.score desc
    ;
    
    

    写法2:自身左交, rank小于3,就是前3

    select a.sid,a.cid,a.score,count(b.cid)+1 as rank from sc a 
    left join sc b on a.cid = b.cid and a.score<b.score
    group by a.cid, a.sid
    having rank <= 3
    order by a.cid, rank asc;
    

    写法3:

    set @rankid =0;
    set @couse_id =0;
    
    select a1.*
    from (
             select a.*,
                    @rankid := if(@couse_id = cid,@rankid + 1, 1) rankid,
                    @couse_id := a.cid couse_id
             from sc a
             order by cid, score desc
         )a1
    where a1.rankid <= 3
    ;
    
    • hive
    select *
    from (
             select t1.cid,
                    t1.sid,
                    t1.score,
                    row_number() over (partition by t1.cid order by t1.score desc) as rank
             from sc t1
         ) a1
    where a1.rank <= 3;
    
    26.查询每门课程被选修的学生数
    select t1.cid,
           count(t1.sid)
    from sc t1
    group by t1.cid;
    
    
    27.查询出只选修两门课程的学生学号和姓名
    select a1.sid,
           t2.sname
    from student t2
             right join (
        select t1.sid,
               count(t1.cid) as cnt
        from sc t1
        group by t1.sid
        having cnt = 2
    )a1
    on t2.sid = a1.sid;
    
    
    28.查询男生、女生人数
    select t1.ssex,
           count(t1.sid) as cnt
    from student t1
    group by t1.ssex;
    
    29.查询名字中含有「风」字的学生信息
    select *
    from student t1
    where t1.sname like '%风%'
    
    30.查询同名同姓学生名单,并统计同名人数
    select t1.sname,
           count(sid) cnt
    from student t1
    group by t1.sname
    having cnt >= 2;
    
    31.查询 1990 年出生的学生名单
    select *
    from student
    where YEAR(student.sage)=1990;
    
    32.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
    select t1.cid,
           avg(t1.score) avg_score
    from sc t1
    group by t1.cid
    order by avg_score desc,t1.cid desc; 
    
    -- 将排名列出
    set @crank = 0;
    select a1.cid,
           a1.avg_score,
           @crank := @crank + 1 as rank
    from (
             select t1.cid,
                    avg(t1.score) avg_score
             from sc t1
             group by t1.cid
             order by avg_score, t1.cid
         ) a1;
    
    
    33.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
    select t2.sid,
           t2.sname,
           a1.avg_score
    from student t2
             right join (
        select t1.sid,
               avg(t1.score) avg_score
        from sc t1
        group by t1.sid
        having avg_score >= 85
    ) a1
                        on t2.sid = a1.sid;
    
    
    34.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数
    select t3.sname,
           a2.score
    from student t3
             right join (
        select t2.sid,
               t2.score
        from sc t2
                 right join (
            select t1.cid
            from course t1
            where t1.cname = '数学'
        ) a1
                            on a1.cid = t2.cid
        where t2.score < 60
    ) a2
                        on t3.sid = a2.sid;
    
    -- 或者
    select student.sname, sc.score from student, sc, course
    where student.sid = sc.sid
    and course.cid = sc.cid
    and course.cname = "数学"
    and sc.score < 60;
    
    
    35.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
    select student.sname, cid, score from student
    left join sc
    on student.sid = sc.sid;
    
    36.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
    select t1.sid,
           t2.sname,
           t3.cname,
           t1.score
    from sc t1
    left join student t2 on t1.sid = t2.sid
    left join course t3 on t1.cid = t3.cid
    where t1.score > 70;
    
    37.查询存在不及格的课程
    select t2.*,
           t1.*
    from sc t1
             left join course t2 on t1.cid = t2.cid
    where t1.score < 60;
    
    38.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名
    select t2.sid,
           t2.sname
    from sc t1
    left join student t2 on t1.sid = t2.sid
    where t1.cid='01'
    and t1.score >= 80;
    
    
    39.求每门课程的学生人数
    select t1.cid,
           count(t1.sid)  as cnt
    from sc t1
    group by t1.cid;
    
    40.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
    set @crank = 0;
    select t4.*
    from student t4
             right join (
        select a2.sid
        from (
                 select a1.*,
                        @crank := @crank + 1 as rank
                 from (
                     select t1.tid,
                     t2.cid,
                     t3.sid,
                     t3.score
                     from teacher t1
                     left join course t2 on t1.tid = t2.tid
                     left join sc t3 on t2.cid = t3.cid
                     where t1.tname = '张三'
                     order by t3.score desc
                     ) a1
             ) a2
        where a2.rank = 1
    )a3
    on t4.sid = a3.sid;
    
    41.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
    
    
    42.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩

    这里用了inner join后会有概念是重复的记录:“01 课与 03课”=“03 课与 01 课”,所以这里取唯一一条可以直接用group by

    select  a.cid, a.sid,  a.score from sc as a
    inner join 
    sc as b
    on a.sid = b.sid
    and a.cid != b.cid
    and a.score = b.score
    group by cid, sid;
    
    43.查询每门功课成绩最好的前两名

    同25题

    select *
    from sc t1
    where (
              select count(*)
              from sc t2
              where t1.cid = t2.cid
                and t1.score < t2.score
          ) < 2
                and t1.score is not  null
    order by t1.cid, t1.score desc
    ;
    
    
    44.统计每门课程的学生选修人数(超过 5 人的课程才统计)。
    select t1.cid,
           count(t1.sid) as cnt
    from sc t1
    group by t1.cid
    having cnt > 5;
    
    45.检索至少选修两门课程的学生学号
    select t1.sid,
           count(cid) cnt
    from sc t1
    group by sid
    having cnt >= 2
    ;
    
    46.查询选修了全部课程的学生信息
    select t2.*
    from student t2
             right join (
        select t1.sid as sid,
               count(cid) as cnt
        from sc t1
        group by sid
        having cnt = (select count(distinct (cid)) from course)
    
    )a1
    on t2.sid = a1.sid;
    
    
    47.查询各学生的年龄,只按年份来算
    select t1.sid as 学生编号,
           t1.sname as  学生姓名,
           TIMESTAMPDIFF(YEAR, t1.sage, CURDATE()) as 学生年龄
    from student t1;
    
    48.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
    select t1.sid as 学生编号,
           t1.sname as  学生姓名,
           TIMESTAMPDIFF(YEAR, t1.sage, CURDATE()) as 学生年龄
    from student t1;
    
    49.查询本周过生日的学生
    select *
    from student t1
    where WEEKOFYEAR(t1.sage)=WEEKOFYEAR(CURDATE());
    
    50.查询下周过生日的学生
    select *
    from student t1
    where WEEKOFYEAR(t1.sage)=WEEKOFYEAR(CURDATE())+1;
    
    51.查询本月过生日的学生
    select *
    from student t1
    where MONTH(t1.sage)=MONTH(CURDATE());
    
    52.查询下月过生日的学生
    select *
    from student t1
    where MONTH(t1.sage)=MONTH(CURDATE())+1;
    

    相关文章

      网友评论

          本文标题:网上流传的50道SQL题刷一刷(使用mysql和hive)

          本文链接:https://www.haomeiwen.com/subject/uncvmltx.html