zoukankan      html  css  js  c++  java
  • hive查询练习

    一、创建数据库并使用数据库

    --创建数据库
    create database if not exists lyq;
    --使用数据库
    use lyq

    二、创建所需要的表,并插入数据

    --创建表
    -- 课程表
    create table if not exists course(course_id int,course_name string,teacher_id int);
    -- 分数表
    create table if not exists score(student_id int,course_id int,score int);
    -- 学生表
    create table if not exists student(student_id int,student_name string,student_birth string,student_sex string);
    -- 教师表
    create table if not exists teacher(teacher_id int,
        teacher_name string
    );
    --插入数据
    -- 课程表
    insert into table course values
    (01,'语文',02),
    (02,'数学',01),
    (03,'英语',03);
    -- 分数表
    insert into table score 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,34),
    (07,02,89),
    (07,03,98);
    -- 学生表
    insert into table student values
    (01,'赵雷',   '1990-01-01','男'),
    (02,'钱电',   '1990-12-21','男'),
    (03,'孙风',   '1990-05-20','男'),
    (04,'李云',   '1990-08-06','男'),
    (05,'周梅',   '1991-12-01','女'),
    (06,'吴兰',   '1992-03-01','女'),
    (07,'郑竹',   '1989-07-01','女'),
    (08,'王菊',   '1990-01-20','女');
    -- 教师表
    insert into table teacher values (01,'张三'),(02,'李四'),(03,'王五');

    三、难的一米的题目

    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,最高分,最低分,平均分,及格率,中等率,优良率,优秀率:
    
    
    19、按各科成绩进行排序,并显示排名:
    – row_number() over()分组排序功能(mysql没有该方法)
    
    
    20、查询学生的总成绩并进行排名:
    
    
    21、查询不同老师所教不同课程平均分从高到低显示:
    
    
    22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩:
    
    
    23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
    
    
    24、查询学生平均成绩及其名次:
    
    
    25、查询各科成绩前三名的记录
    
    
    26、查询每门课程被选修的学生数:
    
    
    27、查询出只有两门课程的全部学生的学号和姓名:
    
    
    28、查询男生、女生人数:
    
    
    29、查询名字中含有"风"字的学生信息:
    
    
    30、查询同名同性学生名单,并统计同名人数:
    
    
    31、查询1990年出生的学生名单:
    
    
    32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列:
    
    
    33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩:
    
    
    34、查询课程名称为"数学",且分数低于60的学生姓名和分数:
    
    
    35、查询所有学生的课程及分数情况:
    
    
    36、查询任何一门课程成绩在70分以上的学生姓名、课程名称和分数:
    
    
    37、查询课程不及格的学生:
    
    
    38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名:
    
    
    39、求每门课程的学生人数:
    
    
    40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩:
    
    
    41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩:
    
    
    42、查询每门课程成绩最好的前三名:
    
    
    43、统计每门课程的学生选修人数(超过5人的课程才统计):
    – 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
    
    
    44、检索至少选修两门课程的学生学号:
    
    
    45、查询选修了全部课程的学生信息:
    
    
    46、查询各学生的年龄(周岁):
    – 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
    
    
    47、查询本周过生日的学生:
    
    
    48、查询下周过生日的学生:
    
    
    49、查询本月过生日的学生:
    
    
    50、查询12月份过生日的学生:

    四、解答

    --1.查询"01"课程比"02"课程成绩高的学生的信息及课程分数
    select s3.*,s1.score,s2.score
    from score s1 join score s2 join student s3
    on s1.student_id=s2.student_id and s2.student_id = s3.student_id
    where s1.course_id=1 and s2.course_id=2 and s1.score>s2.score;
    
    --2.查询"01"课程比"02"课程成绩低的学生的信息及课程分数
    select s3.*,s1.score,s2.score
    from score s1 join score s2 join student s3
    on s1.student_id=s2.student_id and s2.student_id = s3.student_id
    where s1.course_id=1 and s2.course_id=2 and s1.score<s2.score;
    
    --3.查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
    with t1 as
    (select round(avg(s1.score)) r1,s1.student_id
    from score s1  
    group by s1.student_id)
    select t1.student_id,t2.student_name,t1.r1
    from t1 join student t2
    on t1.student_id=t2.student_id
    where t1.r1>=60;
    
    --4.查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
    with t1 as
    (select round(avg(s1.score)) r1,s1.student_id
    from score s1  
    group by s1.student_id)
    select t1.student_id,t2.student_name,t1.r1
    from student t2 left join t1
    on t1.student_id=t2.student_id
    where t1.r1<60 or t1.r1 is null;
    
    --5.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
    with t1 as
    (select s.student_id,count(c.course_id),sum(s.score)
    from score s join course c
    on s.course_id=c.course_id
    group by s.student_id)
    select st.student_name,t1.*
    from student st join t1
    on st.student_id=t1.student_id;
    
    --6.查询"李"姓老师的数量
    select count(teacher_id) from teacher
    where teacher_name like '李%';
    
    --7.查询学过"张三"老师授课的同学的信息
    with t1 as
    (select teacher_id from teacher where teacher_name='张三'),
    t2 as
    (select c.course_id cid from course c join t1 on c.teacher_id=t1.teacher_id),
    t3 as
    (select s.student_id sid from score s join t2 on s.course_id=t2.cid)
    select st.* from student st join t3 on st.student_id=t3.sid; 
    
    --8.查询没学过"张三"老师授课的同学的信息
    select distinct s1.*
    from student s1 left join  score s3 on s1.student_id=s3.student_id
    where  not exists 
    (select s2.*
    from score s1 join course c join teacher t1 join student s2 join (select teacher_id from teacher  where teacher_name='张三') a
    on s1.course_id = c.course_id and t1.teacher_id = c.teacher_id and s1.student_id = s2.student_id and a.teacher_id=t1.teacher_id
    
    --9.查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
    select distinct a.* from 
    (select s1.*,
    count(s2.course_id) over(partition by s2.student_id) as num
    from student s1 join score s2
    on s1.student_id=s2.student_id
    where s2.course_id!=3) a
    where a.num=2;
    
    --10.查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
    with a as
    (select student_id from score where course_id=1),
    b as
    (select s.student_id sid from score s
    join a on s.student_id=a.student_id 
    where s.student_id not in (
    select student_id from score where course_id=2))
    select distinct s.* from student s join b
    on s.student_id=b.sid;
    
    
    --11.查询没有学全所有课程的同学的信息
    select st.* from student st
    where st.student_id not in
    (select a.sid tid from
    (select s.student_id sid,count(1) num from score s
    group by s.student_id) a
    join
    (select count(1) num from course) b
    on a.num=b.num);
    
    --12.查询至少有一门课与学号为"01"的同学所学相同的同学的信息
    select st.* from student st
    join
    (select distinct sc.student_id sid from score sc
    where sc.student_id!=1
    and sc.course_id in 
    (select s.course_id cid from score s
    where s.student_id=1)) a
    on st.student_id=a.sid;
    
    --13.查询和"01"号的同学学习的课程完全相同的其他同学的信息
    select st.* from student st
    join
    (select a.sid cid from
    (select student_id sid,count(course_id) num from score
    where student_id!=1 
    group by student_id) a
    join
    (select count(s.course_id) cont from score s
    where s.student_id=1) b
    where a.num=b.cont) c
    on st.student_id=c.cid;
    
    --14.查询没学过"张三"老师讲授的任一门课程的学生姓名
    select st.* from student st
    where st.student_id not in
    (select s1.student_id from score s1
    where s1.course_id=2);
    
    --15.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
    select * from
    (select s1.student_id,count(s1.score) cont,avg(s1.score) acont from score s1
    where  s1.score<60
    group by s1.student_id) a
    where a.cont>=2;
    
    --16.检索"01"课程分数小于60,按分数降序排列的学生信息
    select st.*,s.score from score s
    join student st
    on st.student_id=s.student_id
    where s.course_id=1 
    and s.score < 60
    order by s.score desc;
    
    --17.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
    select s.score,
    avg(s.score) over(partition by s.student_id) avgg
    from score s
    order by avgg;
    
    --18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
    select s.course_id,c.course_name,
    max(s.score),min(s.score),avg(s.score)
    from score s
    join course c
    on s.course_id=c.course_id
    group by s.course_id,c.course_name;
    
    --19.按各科成绩进行排序,并显示排名
    select s.course_id,st.student_name,s.score,
    row_number() over(partition by s.course_id order by s.score desc) as ronum
    from score s
    join student st
    on s.student_id=st.student_id;
    
    --20.查询学生的总成绩并进行排名
    select s.student_id,st.student_name,sum(s.score) ssum from score s
    join student st
    on st.student_id=s.student_id
    group by s.student_id,st.student_name
    order by ssum desc;
    
    --21.查询不同老师所教不同课程平均分从高到低显示
    select c.teacher_id,t.teacher_name,avg(s.score) avgg 
    from score s
    join course c
    join teacher t
    on c.course_id=s.course_id and c.teacher_id=t.teacher_id
    group by s.course_id,c.teacher_id,t.teacher_name
    order by avgg desc;
    
    --22.查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
    with t1 as
    (select s.student_id sid,s.score sce,
    row_number() over(partition by s.course_id order by s.score desc) as rnum 
    from score s)
    select st.*,t1.sce,t1.rnum 
    from student st 
    join t1 
    on st.student_id=t1.sid
    where t1.rnum in(2,3);
    
    --23.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
    
    
    --24.查询学生平均成绩及其名次
    with t1 as
    (select s.student_id sid,avg(s.score) avgg
    from score s
    group by s.student_id)
    select t1.sid,st.student_name,t1.avgg
    from student st join t1
    on st.student_id=t1.sid
    order by t1.avgg desc;
    
    --25.查询各科成绩前三名的记录
    select a.* from
    (select s.course_id,s.student_id,st.student_name,s.score,
    row_number() over(partition by s.course_id order by s.score) as rnum
    from score s join student st
    on s.student_id=st.student_id) a
    where a.rnum<=3;
    
    --26.查询每门课程被选修的学生数
    select count(s.student_id) from score s
    group by s.course_id;
    
    --27.查询出只有两门课程的全部学生的学号和姓名
    with t1 as
    (select s.student_id sid,count(s.student_id) num from score s
    group by s.student_id)
    select st.student_id,st.student_name from student st
    join t1
    on st.student_id=t1.sid
    where t1.num=2;
    
    --28.查询男生、女生人数 
    select s.student_sex,count(s.student_sex)
    from student s
    group by s.student_sex;
    
    --29.查询名字中含有"风"字的学生信息
    select * from student
    where student_name like '%风%';
    
    --30.查询同名同性学生名单,并统计同名人数
    select student_name,count(*) 
    from student 
    group by student_name 
    having count(*)>1;
    
    --31.查询1990年出生的学生名单
    select * from student
    where year(student_birth)=1990;
    
    --32.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
    select course_id,avg(score) as a 
    from score 
    group by course_id 
    order by a desc,course_id asc;
    
    --33.查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
    with t1 as
    (select student_id sid,avg(score) avgg 
    from score 
    group by student_id
    having avg(score)>=85)
    select t1.sid,st.student_name,t1.avgg
    from t1
    join student st
    on t1.sid=st.student_id;
    
    --34.查询课程名称为"数学",且分数低于60的学生姓名和分数
    select st.student_name,s.score
    from student st
    join score s
    on st.student_id=s.student_id
    where s.course_id 
    in(select course_id from course where course_name='数学');
    
    --35.查询所有学生的课程及分数情况
    select st.student_name,c.course_name,s.score
    from student st
    join course c
    join score s
    on st.student_id=s.student_id and c.course_id=s.course_id;
    
    --36.查询任何一门课程成绩在70分以上的学生姓名、课程名称和分数
    with t1 as
    (select student_id sid,course_id cid,score
    from score
    where score>70)
    select st.student_name,c.course_name,t1.score
    from t1
    join student st
    join course c
    on st.student_id=t1.sid and t1.cid=c.course_id;
    
    
    --37.查询课程不及格的学生
    with
    t1 as(
    select distinct student_id
    from score
    where score <60)
    select s.*
    from student s join t1 on t1.student_id=s.student_id;
    
    --38.查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
    select s.student_id,st.student_name
    from student st
    join score s
    on s.student_id=st.student_id and s.course_id=1
    where s.score>=80;
    
    --39.求每门课程的学生人数
    select course_id,count(1) from score
    group by course_id;
    
    --40.查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
    
    with t1 as
    (select teacher_id from teacher where teacher_name='张三'),
    t2 as
    (select c.course_id cid from course c join t1 on c.teacher_id=t1.teacher_id),
    t3 as
    (select s.student_id sid,s.score score from score s join t2 on s.course_id=t2.cid)
    select st.*,t3.score from student st
    join t3
    on st.student_id=t3.sid
    order by t3.score desc
    limit 1;
    
    --41.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
    select s1.student_id,s2.student_id,s1.course_id,s2.course_id,s1.score
    from score s1 join score s2
    on s1.student_id=s2.student_id
    where s1.score=s2.score and s1.course_id!=s2.course_id;
    
    --42.查询每门课程成绩最好的前三名
    with t1 as
    (select *,row_number() over(partition by course_id order by score) as rnum from score)
    select * from t1
    where t1.rnum<=3;
    
    --43.统计每门课程的学生选修人数(超过5人的课程才统计):
    --要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
    with t1 as
    (select course_id,count(1) cont 
    from score
    group by course_id
    order by course_id)
    select * from t1
    where t1.cont>5;
    
    --44.检索至少选修两门课程的学生学号
    with t1 as
    (select student_id,count(1) cont from score group by student_id)
    select * from student st
    join t1
    on st.student_id=t1.student_id
    where t1.cont>=2;
    
    --45.查询选修了全部课程的学生信息
    select st.* from student st
    where st.student_id in
    (select a.sid tid from
    (select s.student_id sid,count(1) num from score s
    group by s.student_id) a
    join
    (select count(1) num from course) b
    on a.num=b.num);
    
    
    --46.查询各学生的年龄(周岁):
    -- 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
    select *, cast(date_format(current_date(),'yyyy') as int)-cast(date_format(student_birth,'yyyy') as int)
    from student;
    
    --47、查询本周过生日的学生:
    -- 找到下周一-1即为本周最后一天,开始时间为当前天
    select *
    from student
    where unix_timestamp(cast(concat_ws('-',date_format(current_date(),'yyyy'),date_format(student_birth,'MM'),date_format(student_birth,'dd')) as date),'yyyy-MM-dd')
    between unix_timestamp(current_date())
    and unix_timestamp(date_sub(next_day(current_date(),'MON'),1),'yyyy-MM-dd');
    
    --48、查询下周过生日的学生:
    -- 找到下周的最后一天和最开始一天来算 next_day(current_date(),'SUNDAY'); -- 需要注意的是老外以星期天,开始需要+7
    select *
    from student
    where unix_timestamp(cast(concat_ws('-',date_format(current_date(),'yyyy'),date_format(student_birth,'MM'),date_format(student_birth,'dd')) as date),'yyyy-MM-dd')
    between unix_timestamp(next_day(current_date(),'MON'),'yyyy-MM-dd')
    and unix_timestamp(date_add(next_day(current_date(),'SUN'),7),'yyyy-MM-dd');
    
    --49、查询本月过生日的学生:
    select *
    from student
    where month(student_birth) = month(current_date());
    
    --50、查询12月份过生日的学生:
    select *
    from student 
    where month(student_birth)=12;

    转载博客:https://blog.csdn.net/lyq7269/article/details/107437907?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~sobaiduend~default-1-107437907.nonecase&utm_term=hive%20%E6%9F%A5%E8%AF%A2%E7%BB%83%E4%B9%A0&spm=1000.2123.3001.4430


  • 相关阅读:
    微访谈之1:解答各位朋友关心的问题
    深入浅出SQL Server中的死锁(实战篇)
    怎样玩转千万级别的数据
    Another MySQL daemon already running with the same unix socket
    c++ undefined reference to mysqlinit
    Another MySQL daemon already running with the same unix socket
    linxu 挂载分区
    C# RSA
    谷歌地图实现车辆轨迹移动播放(google map api)
    百度地图实现车辆轨迹移动播放(baidu map api)
  • 原文地址:https://www.cnblogs.com/LEPENGYANG/p/14039902.html
Copyright © 2011-2022 走看看