zoukankan      html  css  js  c++  java
  • sql-hive笔试题整理 1 (学生表-成绩表-课程表-教师表)

    题记:一直在写各种sql查询语句,最长的有一百多行,自信什么需求都可以接,可。。。。。。,想了想,可能一直在固定的场景下写,平时也是以满足实际需求为目的,竟不知道应试的题都是怎么出的,又应该怎么做。遂找来一些笔试题来练习。

    有四张表如下格式存储:

    --1、查询“001”课程比“002”课程成绩高的所有学生的学号; 
    select t1.s# from
    (select s#,c#,score from sc where c# = 001) t1 inner join (select s#,c#,score from sc where c# = 002) t2
    on t1.s#=t2.s#
    where t1.score > t2.score

    --2、查询平均成绩大于60分的同学的学号和平均成绩; 

    select s#,avg(score)
    from sc
     group by s# having avg(score)>60

    --3、查询所有同学的学号、姓名、选课数、总成绩; 
    select student.s#,student.sname,count(sc.c#),sum(sc.score)
    from student left join sc on student.s#=sc.s#
    group by student.s#,student.sname

    --4、查询姓“李”的老师的个数; 
    select count(t#)
    from teacher
    where tname like '李%'

    --5、查询没学过“叶平”老师课的同学的学号、姓名; 
    select t1.s#,t1.sname
    from student t1
    left join sc t2 on t1.s#=t2.s#
    left join course t3 on t2.c#=t3.c#
    left join teacher t4 on t3.t#=t4.t#
    where t4.tname != '叶平'

    --6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;  

    select t0.s#,t0.sname
    from student t0
    inner join (select s#,c#,score from sc where c# = 001) t1
    on t0.s#=t1.s#
    inner join (select s#,c#,score from sc where c# = 002) t2
    on t0.s#=t2.s#

    --7、查询学过“叶平”老师所教的所有课的同学的学号、姓名; 
    select t1.s#,t1.sname
    from student t1
    left join sc t2 on t1.s#=t2.s#
    left join course t3 on t2.c#=t3.c#
    left join teacher t4 on t3.t#=t4.t#
    where t4.tname = '叶平'

    --8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;
    select t0.s#,t0.sname
    from student t0
    inner join (select s#,c#,score from sc where c# = 001) t1
    on t0.s#=t1.s#
    inner join (select s#,c#,score from sc where c# = 002) t2
    on t0.s#=t2.s#
    where t2.score<t1.score

    --9、查询所有课程成绩小于60分的同学的学号、姓名; 

    select t1.s#,t1.sname
    from student t1
    inner join (select s#,count(c#),count(if(score<60,c#,null)) from sc group by s# having count(c#) = count(if(score<60,c#,null))) t2
    on t1.s#=t2.s#

    --10、查询没有学全所有课的同学的学号、姓名; 

     select t1.s#,t1.sname

    from student t1 inner join sc t2 on t1.s#=t2.s#

    group by t1.s#,t1.sname

    having count(c#) < (select count(c#) from course)

    --11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名; 

     select distinct t1.s#,t1.sname

    from student t1 inner join sc t2 on t1.s#=t2.s#

    left semi join (select c# from sc where s#=1001) t3 on t2.c#=t3.c#

    --12、查询至少学过学号为“001”同学所有一门课的其他同学学号和姓名; 

    --13、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名; 

     select t1.s#,t3.sname

    from sc t1

    inner join sc t2

    on t1.c#=t2.c#

    left join student t3

    on t1.s#=t3.s#

    where t2.s#=1002

    group by t1.s#

    having count(distinct t1.c#) = count(distinct t2.c#)

    --14、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分 

    select c#,max(score),min(score)
    from sc
    group by c#

    --15、按各科平均成绩从低到高和及格率的百分数从高到低顺序 

     select c#,ave,count_g/count_all

    from (select c#,

    average(score) ave,

    count(if(score>=60,s#,null)) count_g,

    count(s#) count_all

    from sc

    group by c#) t1

    order by ave,count_g/count_all desc

    --16、查询如下课程平均成绩和及格率的百分数(用"1行"显示): 企业管理(001),马克思(002),OO&UML (003),数据库(004)

    select c#,ave,count_g/count_all

    from

    (select c#,

    average(score) ave,

    count(if(score>=60,s#,null)) count_g,

    count(s#) count_all

    from sc

    where c# in (001,002,003,004)

    group by c#) t1

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

    select t2.cname,t3.tname,avg(t1.score)
    from sc t1
    inner join course t2 on t1.c#=t2.c#
    inner join teacher t3 on t2.t#=t3.t#
    group by t2.cname,t3.tname
    order by avg(t1.score) desc 

    --18、查询如下课程成绩第 3 名到第 6 名的学生成绩单:企业管理(001),马克思(002),UML (003),数据库(004)  

    select * from
    (select t1.s#,t1.sname,t2.c#,t2.score,row_number() over(partition by t2.c# order by t2.score desc) num
    from student t1
    inner join sc t2 on t1.s#=t2.s#
    where c# in (001,002,003,004)) t
    where t.num >=3 and t.num<=6

    --19、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]  

    select t1.c#,t2.cname,case when t1.score >=85 then [100-85] when (t1.score <85 and t1.score >=70) then [85-70] when (t1.score <70 and t1.score >=60) then [70-60] when t1.score <60 then [ <60] end as score, count(t1.s#)
    from sc t1
    inner join course t2 on t1.c#=t2.c#
    group by t1.c#,t2.cname

    --20、查询学生平均成绩及其名次 

    select t1.s#,t1.sname,avg(t2.score),row_number() over(order by avg(t2.score)) as ranking
    from student t1
    inner join sc t2 on t1.s#=t2.s#

    --21、查询各科成绩前三名的记录:(不考虑成绩并列情况) 

    select * from 
    (select t1.s#,t1.sname,t2.c#,t2.score,row_number() over(partition by t2.c# order by t2.score desc) num
    from student t1
    inner join sc t2 on t1.s#=t2.s#
    ) t 
    where t.num <=3

    --22、查询每门课程被选修的学生数 

    select c#,count(s#)
    from sc group by c#

    --23、查询出只选修了一门课程的全部学生的学号和姓名 

    select t1.s#,t2.sname
    from sc t1
    inner join student t2 on t1.s#=t2.s#
    where count(t1.c#)=1
    group by t1.s#,t2.sname 

    --24、查询男生、女生人数 

    select ssex,count(s#)
    from student group by ssex

    --25、查询姓“张”的学生名单 

    select s#,sname
    from student
    where sname like '张%'

    --26、查询同名同性学生名单,并统计同名人数 

    select sname,count(s#)
    from student
    where count(s#)>1
    group by sname 

    --27、1981年出生的学生名单(注:Student表中Sage列的类型是datetime) 

     select s#,sname

    from student

    where datediff(year,today,sage)=1981

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

     select c#,avg(score) from sc

    group by c# order by avg(score),c# desc

    --29、查询平均成绩大于85的所有学生的学号、姓名和平均成绩

    select t1.s#,t2.sname,avg(t1.score)
    from sc t1 inner join student t2 on t1.s#=t2.s#
    group by t1.s# having avg(t1.score)>85

    --30、查询课程名称为“数据库”,且分数低于60的学生姓名和分数

    select t0.sname,t1.score
    from student t0
    inner join sc t1 on t0.s#=t1.s#
    inner join course t2 on t1.c#=t2.c#
    where t2.cname = '数据库' and t1.score<60

    --31、查询所有学生的选课情况;

    select t1.s#,t1.sname,t2.c#,t3.cname
    from student t1
    left join sc t2 on t1.s#=t2.s#
    inner join course t3 on t2.c#=t3.c#

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

    select t1.sname,t3.cname,t2.score
    from student t1
    inner join sc t2 on t1.s#=t2.s#
    inner join course t3 on t2.c#=t3.c#
    where t2.score>70

    --33、查询不及格的课程,并按课程号从大到小排列

    select c#,avg(score)

    from sc

    group by c#

    order by avg(score) desc

    having avg(score)<60
    --34、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;

    select t1.s#,t1.sname
    from student t1
    inner join sc t2 on t1.s#=t2.s#
    inner join course t3 on t2.c#=t3.c#
    where t3.c#=003 and t2.score>80

    --35、求选了课程的学生人数

    select count(distinct s#)
    from sc

    --36、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩

    select t1.sname,max(t2.score)

    from student t1 inner join sc t2 on t1.s#=t2.s#

    inner join course t3 on t2.c#=t3.c#

    inner join teacher t4 on t3.t#=t4.t#

    where t4.tname='叶平'

    group by t1.sname
    --37、查询各个课程及相应选修人数

    select c#,count(s#)
    from sc
    group by c#

    --38、查询不同课程成绩相同的学生的学号、课程号、学生成绩

    select t1.s#,t1.c#,t2.c#,t1.score

    from sc t1

    inner join sc t2 on t1.s#=t2.s#

    where t1.score=t2.score and t1.c#<>t2.c#

    --39、查询每门课程成绩最好的前两名

    select c#,s# from

    (select c#,s#,score,row_number() over(partition by c# order by score desc) num

    from sc) t

    where t.num=2

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

     select c#,count(s#)

    from sc

    group by c#

    order by count(s#) desc,c#

    having count(s#)>10

    --41、检索至少选修两门课程的学生学号

    select s#

    from sc

    group by s#

    having count(c#)>=2

    --42、查询全部学生都选修的课程的课程号和课程名

    select t1.c#,t2.cname

    from sc t1 inner join course t2 on t1.c#=t2.c#

    group by t1.c#,t2.cname

    having count(s#)=(select count(s#) from student)
    --43、查询没学过“叶平”老师讲授的任一门课程的学生姓名

    select

    from student s

    left join (select t1.sname

    from student t1 inner join sc t2 on t1.s#=t2.s#

    inner join course t3 on t2.c#=t3.c#

    inner join teacher t4 on t3.t#=t4.t#

    where t4.tname='叶平') t

    on s.sname=t.sname

    where t.sname is null

    --44、查询两门以上不及格课程的同学的学号及其平均成绩

    select s#,avg(score)

    from sc

    group by s#

    having count(if(score<60,c#,null))>2
    --45、检索“004”课程分数小于60,按分数降序排列的同学学号

    select s#,score
    from sc
    where c#=004 and score<60
    order by score desc

    以上,均为自己所写,如有错误,敬请指正

  • 相关阅读:
    每日一题20180325
    Linux下MySQL表名区分大小写
    CentOS删除编译安装的Python3
    HTTPS配置
    测试 js 方法运行时间(转)
    使用dbutils进行批处理
    oracle生成主键
    JDBC学习笔记(10)——调用函数&存储过程(转)
    easyui Draggable
    blob
  • 原文地址:https://www.cnblogs.com/xiyouzhi/p/9564972.html
Copyright © 2011-2022 走看看