hive50道题带查询结果

建表

create table if not exists student(
id int,
name string,
birthday string,
sex string
)
row format delimited fields terminated by '\t';
create table if not exists teacher(
tid int,
tname string
)
row format delimited fields terminated by '\t';
create table if not exists score(
sid int,
cid int,
scores int
)
row format delimited fields terminated by '\t';
create table if not exists course(
cid int,
cname string,
tid int
)
row format delimited fields terminated by '\t';

加载数据

load data local inpath '/root/stu/student.txt' into table student;
load data local inpath '/root/stu/teacher.txt' into table teacher;
load data local inpath '/root/stu/score.txt' into table score;
load data local inpath '/root/stu/course.txt' into table course;

表数据

student

student.id      student.name    student.birthday        student.sex
1       赵雷    1990-01-012       钱电    1990-12-213       孙风    1990-05-204       李云    1990-08-065       周梅    1991-12-016       吴兰    1992-03-017       郑竹    1989-07-018       王菊    1990-01-20

score

score.sid       score.cid       score.scores
1       1       80
1       2       90
1       3       99
2       1       70
2       2       60
2       3       80
3       1       80
3       2       80
3       3       80
4       1       50
4       2       30
4       3       20
5       1       76
5       2       87
6       1       31
6       3       34
7       2       89
7       3       98

course

course.cid      course.cname    course.tid
1       语文    2
2       数学    1
3       英语    3

teacher

teacher.tid     teacher.tname
1       张三
2       李四
3       王五

注意:开窗函数是用在select后,可以起别名,但别名的使用要在子查询(子表)中

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

select t1.*,t2.scores
from
(select
student.*,score.scores
from
student,score
where
student.id=score.sid
and
score.cid='1') t1,
(select
student.*,score.scores
from
student,score
where
student.id=score.sid
and
score.cid='2') t2
where
t1.id=t2.id and
t1.scores>t2.scores;

t1.id   t1.name t1.birthday     t1.sex  t1.scores       t2.scores
2       钱电    1990-12-2170      60
4       李云    1990-08-0650      30

2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数:

   select t1.*,t2.scores
from
(select
student.*,score.scores
from
student,score
where
student.id=score.sid
and
score.cid='1') t1,
(select
student.*,score.scores
from
student,score
where
student.id=score.sid
and
score.cid='2') t2
where
t1.id=t2.id and
t1.scores<t2.scores;

t1.id   t1.name t1.birthday     t1.sex  t1.scores       t2.scores
1       赵雷    1990-01-0180      90
5       周梅    1991-12-0176      87

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

select
student.id,student.name,avg(score.scores) sc_avg
from
student,score
where
student.id=score.sid
group by student.id,student.name
having sc_avg>=60;

student.id      student.name    sc_avg
1       赵雷    89.66666666666667
2       钱电    70.0
3       孙风    80.0
5       周梅    81.5
7       郑竹    93.5

4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩:

select
student.id,student.name,avg(nvl(score.scores,0)) sc_avg
from
student
left join
score
on
student.id=score.sid
group by student.id,student.name
having sc_avg<60;

student.id      student.name    sc_avg
4       李云    33.333333333333336
6       吴兰    32.5
8       王菊    0.0

5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩:

select
student.id,student.name,count(*),sum(score.scores)
from
student,score
where
student.id=score.sid
group by student.id,student.name;

student.id      student.name    _c2     _c3
1       赵雷    3       269
2       钱电    3       210
3       孙风    3       240
4       李云    3       100
5       周梅    2       163
6       吴兰    2       65
7       郑竹    2       187

6、查询"李"姓老师的数量:

select
count(*)
from
teacher
where tname like '李%';

7、查询学过"张三"老师授课的同学的信息:

select
student.*
from
student,score,course,teacher
where student.id=score.sid and score.cid=course.cid and course.tid=teacher.tid
and teacher.tname='张三';

student.id      student.name    student.birthday        student.sex
1       赵雷    1990-01-012       钱电    1990-12-213       孙风    1990-05-204       李云    1990-08-065       周梅    1991-12-017       郑竹    1989-07-01

8、查询没学过"张三"老师授课的同学的信息:

select
distinct(student.id),student.name,student.birthday,student.sex
from
student,score,course,teacher
where student.id=score.sid and score.cid=course.cid and course.tid=teacher.tid
and teacher.tname!='张三';

student.id      student.name    student.birthday        student.sex
1       赵雷    1990-01-012       钱电    1990-12-213       孙风    1990-05-204       李云    1990-08-065       周梅    1991-12-016       吴兰    1992-03-017       郑竹    1989-07-01

9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息:

select student.*
from
student,
(select sid from score where cid=1) s1,
(select sid from score where cid=2) s2
where 
student.id=s1.sid 
and  s1.sid=s2.sid;

student.id      student.name    student.birthday        student.sex
1       赵雷    1990-01-012       钱电    1990-12-213       孙风    1990-05-204       李云    1990-08-065       周梅    1991-12-01

10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息:

select
student.*
from
student join
(select sid from score where cid=1) s1
on student.id=s1.sid
left join
(select sid from score where cid=2) s2
on student.id=s2.sid
where s2.sid is null;

student.id      student.name    student.birthday        student.sex
6       吴兰    1992-03-01

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

select student.* from student
left join(
select sid
from score
group by sid
having count(cid)=3)tmp
on student.id=tmp.sid
where tmp.sid is null; 

student.id      student.name    student.birthday        student.sex
5       周梅    1991-12-016       吴兰    1992-03-017       郑竹    1989-07-018       王菊    1990-01-20

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

  select
    id,name,birthday,sex
    from
    student,score,(select cid from score where sid=1) s1
    where student.id=score.sid and score.cid=s1.cid
    group by id,name,birthday,sex;

   id      name    birthday        sex
1       赵雷    1990-01-012       钱电    1990-12-213       孙风    1990-05-204       李云    1990-08-065       周梅    1991-12-016       吴兰    1992-03-017       郑竹    1989-07-01

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

select stu.*,count(tmp2.cid) from student stu
join (select cid from score where sid=1) tmp1
join (select sid,cid from score) tmp2
on tmp1.cid=tmp2.cid and stu.id=tmp2.sid
where stu.id not in (1)
group by stu.id,name,birthday,sex
having count(tmp2.cid) in (select count(cid) from score where sid=1);

2       钱电    1990-12-213
3       孙风    1990-05-203
4       李云    1990-08-063

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

 select name
    from student left join
(select student.id
from
student,score,course,teacher
where student.id=score.sid and score.cid=course.cid 
and course.tid=teacher.tid and teacher.tname='张三'
) s1
on student.id=s1.id
where s1.id is null;

name
吴兰
王菊

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

select 
count(score.cid) count,id,name,avg(scores)
from student,score
where student.id=score.sid and scores<60
group by id,name
having count>=2;

count   id      name    _c3
3       4       李云    33.333333333333336
2       6       吴兰    32.5

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

  select student.*,scores
    from student join score
    on id=sid
    where cid=1 and scores<60
    order by scores desc;

    student.id      student.name    student.birthday        student.sex     scores
4       李云    1990-08-0650
6       吴兰    1992-03-0131

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

select 
s1.*,score.cid,score.scores
from
score,
(select sid,name,avg(scores) avg_sc
    from student,score
    where student.id=score.sid
    group by sid,name
) s1
where score.sid=s1.sid
order by avg_sc desc;

s1.sid  s1.name s1.avg_sc       score.cid       score.scores
7       郑竹    93.5    3       98
7       郑竹    93.5    2       89
1       赵雷    89.66666666666667       1       80
1       赵雷    89.66666666666667       3       99
1       赵雷    89.66666666666667       2       90
5       周梅    81.5    2       87
5       周梅    81.5    1       76
3       孙风    80.0    2       80
3       孙风    80.0    1       80
3       孙风    80.0    3       80
2       钱电    70.0    3       80
2       钱电    70.0    2       60
2       钱电    70.0    1       70
4       李云    33.333333333333336      2       30
4       李云    33.333333333333336      1       50
4       李云    33.333333333333336      3       20
6       吴兰    32.5    3       34
6       吴兰    32.5    1       31

18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率:

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

select score.cid,course.cname,max(scores),min(scores),avg(scores),
round(sum(if(scores>=90,1,0))/count(*),2) s1,
round(sum(if(scores>=80 and scores<90,1,0))/count(*),2) s2,
round(sum(if(scores>=70 and scores<80,1,0))/count(*),2) s3,
round(sum(if(scores>=60,1,0))/count(*),2) s4
from
score,course
where score.cid=course.cid
group by score.cid,course.cname;

score.cid       course.cname    _c2     _c3     _c4     s1      s2      s3      s4
1       语文    80      31      64.5    0.0     0.33    0.33    0.67
2       数学    90      30      72.66666666666667       0.17    0.5     0.0     0.83
3       英语    99      20      68.5    0.33    0.33    0.0     0.67

19、按各科成绩进行排序,并显示排名:

select name,cid,scores,
rank() over(partition by cid order by scores desc)
from student join score
on student.id=score.sid;

 name    _c1     _c2     rank_window_0
孙风    1       80      1
赵雷    1       80      1
周梅    1       76      3
钱电    1       70      4
李云    1       50      5
吴兰    1       31      6
赵雷    2       90      1
郑竹    2       89      2
周梅    2       87      3
孙风    2       80      4
钱电    2       60      5
李云    2       30      6
赵雷    3       99      1
郑竹    3       98      2
钱电    3       80      3
孙风    3       80      3
吴兰    3       34      5
李云    3       20      6

20、查询学生的总成绩并进行排名:

select s1.*,
rank() over(order by s1.sum_sc desc)
from (
select name,sum(scores) sum_sc
from student,score
where student.id=score.sid
group by name) s1;

	s1.name s1.sum_sc       rank_window_0
赵雷    269     1
孙风    240     2
钱电    210     3
郑竹    187     4
周梅    163     5
李云    100     6
吴兰    65      7

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

select 
course.cid,course.cname,teacher.tname,avg(scores) avg_sc
from
score,course,teacher
where score.cid=course.cid and course.tid=teacher.tid
group by course.cid,course.cname,teacher.tname
order by avg_sc desc;

course.cid      course.cname    teacher.tname   avg_sc
2       数学    张三    72.66666666666667
3       英语    王五    68.5
1       语文    李四    64.5

22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩:

 select *
    from(
select name,cid,scores,
    rank() over(partition by cid order by scores desc) rn
    from student join score
    on student.id=score.sid) s1 
    where s1.rn between 2 and 3;

  s1.name s1.cid  s1.scores       s1.rn
周梅    1       76      3
郑竹    2       89      2
周梅    2       87      3
郑竹    3       98      2
钱电    3       80      3
孙风    3       80      3

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

select score.cid,course.cname,
sum(if(scores<=100 and scores>=85,1,0))/count(*) sc1,
sum(if(scores<=85 and scores>=70,1,0))/count(*) sc2,
sum(if(scores<=70 and scores>=60,1,0))/count(*) sc3,
sum(if(scores<=60 and scores>=0,1,0))/count(*) sc4
from score,course
where score.cid=course.cid
group by score.cid,course.cname;

score.cid       course.cname    sc1     sc2     sc3     sc4
1       语文    0.0     0.6666666666666666      0.16666666666666666     0.3333333333333333
2       数学    0.5     0.16666666666666666     0.16666666666666666     0.3333333333333333
3       英语    0.3333333333333333      0.3333333333333333      0.0     0.3333333333333333

24、查询学生平均成绩及其名次:

select s1.*,
    rank() over(order by s1.avg_sc desc)
    from (
    select name,avg(scores) avg_sc
    from student,score
    where student.id=score.sid
    group by name) s1;

郑竹    93.5    1
赵雷    89.66666666666667       2
周梅    81.5    3
孙风    80.0    4
钱电    70.0    5
李云    33.333333333333336      6
吴兰    32.5    7

25、查询各科成绩前三名的记录

    select *
    from(
select name,cid,scores,
    rank() over(partition by cid order by scores desc) rn
    from student join score
    on student.id=score.sid) s1 
    where s1.rn<=3;

s1.name s1.cid  s1.scores       s1.rn
孙风    1       80      1
赵雷    1       80      1
周梅    1       76      3
赵雷    2       90      1
郑竹    2       89      2
周梅    2       87      3
赵雷    3       99      1
郑竹    3       98      2
钱电    3       80      3
孙风    3       80      3

26、查询每门课程被选修的学生数:

 select course.cid,count(*)
    from course,score
    where course.cid=score.cid
    group by course.cid;

course.cid      _c1
1       6
2       6
3       6

27、查询出只有两门课程的全部学生的学号和姓名:

 select
    id,name,count(*) count_sc
    from score,student
    where student.id=score.sid
    group by id,name
    having count_sc=2;

id      name    count_sc
5       周梅    2
6       吴兰    2
7       郑竹    2

28、查询男生、女生人数:

 select
    sex,count(id)
    from student
    group by sex;

sex     _c1
女      44

29、查询名字中含有"风"字的学生信息:

 select *
    from student
    where name like '%风%';

student.id      student.name    student.birthday        student.sex
3       孙风    1990-05-20

30、查询同名同性学生名单,并统计同名人数:

select count(*),s1.name,s1.sex
from student s1,student s2
where s1.id=s2.id and s1.name=s2.name and s1.sex=s2.sex
group by s1.name,s1.sex;

_c0     s1.name s1.sex
1       吴兰    女
1       周梅    女
1       孙风    男
1       李云    男
1       王菊    女
1       赵雷    男
1       郑竹    女
1       钱电    男

31、查询1990年出生的学生名单:

select *
from student
where substring(birthday,1,4)=1990;

student.id      student.name    student.birthday        student.sex
1       赵雷    1990-01-012       钱电    1990-12-213       孙风    1990-05-204       李云    1990-08-068       王菊    1990-01-20

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

select
avg(scores) avg_sc,cid
from score
group by cid
order by avg_sc desc,cid;

avg_sc  cid
72.66666666666667       2
68.5    3
64.5    1

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

  select 
    avg(scores) avg_sc,id,name
    from student,score
    where student.id=score.sid
    group by id,name
    having avg_sc>=85;

avg_sc  id      name
89.66666666666667       1       赵雷
93.5    7       郑竹

34、查询课程名称为"数学",且分数低于60的学生姓名和分数:

   select
    name,scores
    from
    student,score,course
    where student.id=score.sid and course.cid=score.cid 
    and cname='数学' and scores<60;

name    scores
李云    30

35、查询所有学生的课程及分数情况:

select name,cid,scores
    from student join score
    on student.id=score.sid;

name    cid     scores
赵雷    1       80
赵雷    2       90
赵雷    3       99
钱电    1       70
钱电    2       60
钱电    3       80
孙风    1       80
孙风    2       80
孙风    3       80
李云    1       50
李云    2       30
李云    3       20
周梅    1       76
周梅    2       87
吴兰    1       31
吴兰    3       34
郑竹    2       89
郑竹    3       98

36、查询任何一门课程成绩在70分以上的学生姓名、课程名称和分数:

   select
    name,cname,scores
    from
    student,score,course
    where student.id=score.sid and score.cid=course.cid
    and scores>=70;

name    cname   scores
赵雷    语文    80
赵雷    数学    90
赵雷    英语    99
钱电    语文    70
钱电    英语    80
孙风    语文    80
孙风    数学    80
孙风    英语    80
周梅    语文    76
周梅    数学    87
郑竹    数学    89
郑竹    英语    98

37、查询课程不及格的学生:

  select name
    from student,score
    where id=sid and scores<60
    group by name;

name
吴兰
李云

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

select
id,name
from
student,score
where id=sid and cid=1 and scores>=80;

id      name
1       赵雷
3       孙风

39、求每门课程的学生人数:

 select course.cid,count(*)
    from course,score
    where course.cid=score.cid
    group by course.cid;

course.cid      _c1
1       6
2       6
3       6

40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩:

select
student.*,scores
from
student,score,course,teacher
where student.id=score.sid and score.cid=course.cid and course.tid=teacher.tid
and teacher.tname='张三'
order by scores desc
limit 1;

student.id      student.name    student.birthday        student.sex     scores
1       赵雷    1990-01-0190

41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩:

select s1.*
from score s1 join score s2
on s1.sid=s2.sid where s1.cid!=s2.cid and s1.scores=s2.scores;

42、查询每门课程成绩最好的前三名:

 select *
    from(
select name,cid,scores,
    row_number() over(partition by cid order by scores desc) rn
    from student join score
    on student.id=score.sid) s1 
    where s1.rn<=3;

s1.name s1.cid  s1.scores       s1.rn
孙风    1       80      1
赵雷    1       80      1
周梅    1       76      3
赵雷    2       90      1
郑竹    2       89      2
周梅    2       87      3
赵雷    3       99      1
郑竹    3       98      2
钱电    3       80      3
孙风    3       80      3

43、统计每门课程的学生选修人数(超过5人的课程才统计):

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

select course.cid,count(*) count
    from course,score
    where course.cid=score.cid
    group by course.cid
    having count>5
    order by count desc,cid;

course.cid      count
1       6
2       6
3       6

44、检索至少选修两门课程的学生学号:

 select 
    id,count(cid) count
    from student,score
    where id=sid
    group by id
    having count>=2;

id      count
1       3
2       3
3       3
4       3
5       2
6       2
7       2

45、查询选修了全部课程的学生信息:

select 
    id,count(cid) count
    from student,score
    where id=sid
    group by id
    having count=3;

id      count
1       3
2       3
3       3
4       3

46、查询各学生的年龄(周岁):

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

select if(month(from_unixtime(unix_timestamp(),'yyyy-MM-dd'))<month(birthday) and
day(from_unixtime(unix_timestamp(),'yyyy-MM-dd'))<day(birthday),
year(from_unixtime(unix_timestamp(),'yyyy-MM-dd'))-year(birthday)-1,
year(from_unixtime(unix_timestamp(),'yyyy-MM-dd'))-year(birthday)) ,
student.*
from student;

_c0     student.id      student.name    student.birthday        student.sex
30      1       赵雷    1990-01-0130      2       钱电    1990-12-2130      3       孙风    1990-05-2030      4       李云    1990-08-0629      5       周梅    1991-12-0128      6       吴兰    1992-03-0131      7       郑竹    1989-07-0130      8       王菊    1990-01-20

47、查询本周过生日的学生:

 select weekofyear(from_unixtime(unix_timestamp(),'yyyy-MM-dd')),student.*
    from student
    where weekofyear(student.birthday)=weekofyear(from_unixtime(unix_timestamp(),'yyyy-MM-dd'));

_c0     student.id      student.name    student.birthday        student.sex
51      2       钱电    1990-12-21

48、查询下周过生日的学生:

select weekofyear(date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),7)),student.*
    from student
    where weekofyear(student.birthday)=weekofyear(date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),7));

49、查询本月过生日的学生:

select month(from_unixtime(unix_timestamp(),'yyyy-MM-dd')),student.*
from student
where month(student.birthday)=month(from_unixtime(unix_timestamp(),'yyyy-MM-dd'));

_c0     student.id      student.name    student.birthday        student.sex
12      2       钱电    1990-12-2112      5       周梅    1991-12-01

50、查询12月份过生日的学生:

select 
*
from student where month(birthday)=12;

student.id      student.name    student.birthday        student.sex
2       钱电    1990-12-215       周梅    1991-12-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值