Hive SQL題庫-初級

第一章 環境準備

1.1 建表語句

hive>

-- 創建學生表
DROP TABLE IF EXISTS student;
create table if not exists student_info(
  stu_id string COMMENT '學生id',
  stu_name string COMMENT '學生姓名',
  birthday string COMMENT '出生日期',
  sex string COMMENT '性別'
) row format delimited fields terminated by ',' stored as textfile;

-- 創建課程表
DROP TABLE IF EXISTS course;
create table if not exists course_info(
  course_id string COMMENT '課程id',
  course_name string COMMENT '課程名',
  tea_id string COMMENT '任課老師id'
) row format delimited fields terminated by ',' stored as textfile;

-- 創建老師表
DROP TABLE IF EXISTS teacher;
create table if not exists teacher_info(
  tea_id string COMMENT '老師id',
  tea_name string COMMENT '學生姓名'
) row format delimited fields terminated by ',' stored as textfile;

 
-- 創建分數表
DROP TABLE IF EXISTS score;
create table if not exists score_info(
  stu_id string COMMENT '學生id',
  course_id string COMMENT '課程id',
  score int COMMENT '成績'
) row format delimited fields terminated by ',' stored as textfile;

1.2 數據準備

(1)創建/opt/module/data目錄

[atguigu@hadoop102 module]$ mkdir data

(2)將如下4個文件放到/opt/module/data目錄下

img imgimgimg

(3)數據樣式說明

[atguigu@hadoop102 data]$ vim student_info.txt
001,彭于晏,1995-05-16,男
002,胡歌,1994-03-20,男
003,周杰倫,1995-04-30,男
004,劉德華,1998-08-28,男
005,唐國強,1993-09-10,男
006,陳道明,1992-11-12,男
007,陳坤,1999-04-09,男
008,吳京,1994-02-06,男
009,郭德綱,1992-12-05,男
010,于謙,1998-08-23,男
011,潘長江,1995-05-27,男
012,楊紫,1996-12-21,女
013,蔣欣,1997-11-08,女
014,趙麗穎,1990-01-09,女
015,劉亦菲,1993-01-14,女
016,周冬雨,1990-06-18,女
017,范冰冰,1992-07-04,女
018,李冰冰,1993-09-24,女
019,鄧紫棋,1994-08-31,女
020,宋丹丹,1991-03-01,女
[atguigu@hadoop102 data]$ vim course_info.txt
01,語文,1003
02,數學,1001
03,英語,1004
04,體育,1002
05,音樂,1002
[atguigu@hadoop102 data]$ vim teacher_info.txt
1001,張高數
1002,李體音
1003,王子文
1004,劉麗英
[atguigu@hadoop102 data]$ vim score_info.txt
001,01,94
002,01,74
004,01,85
005,01,64
006,01,71
007,01,48
008,01,56
009,01,75
010,01,84
011,01,61
012,01,44
013,01,47
014,01,81
015,01,90
016,01,71
017,01,58
018,01,38
019,01,46
020,01,89
001,02,63
002,02,84
004,02,93
005,02,44
006,02,90
007,02,55
008,02,34
009,02,78
010,02,68
011,02,49
012,02,74
013,02,35
014,02,39
015,02,48
016,02,89
017,02,34
018,02,58
019,02,39
020,02,59
001,03,79
002,03,87
004,03,89
005,03,99
006,03,59
007,03,70
008,03,39
009,03,60
010,03,47
011,03,70
012,03,62
013,03,93
014,03,32
015,03,84
016,03,71
017,03,55
018,03,49
019,03,93
020,03,81
001,04,54
002,04,100
004,04,59
005,04,85
007,04,63
009,04,79
010,04,34
013,04,69
014,04,40
016,04,94
017,04,34
020,04,50
005,05,85
007,05,63
009,05,79
015,05,59
018,05,87

1.3 插入數據

(1)插入數據

hive>
load data local inpath '/opt/module/data/student_info.txt' into table student_info;
load data local inpath '/opt/module/data/course_info.txt' into table course_info;
load data local inpath '/opt/module/data/teacher_info.txt' into table teacher_info;
load data local inpath '/opt/module/data/score_info.txt' into table score_info;

(2)驗證插入數據情況

hive>
select * from student_info limit 5;
select * from course_info limit 5;
select * from teacher_info limit 5;
select * from score_info limit 5;

第二章 簡單查詢

2.1 查找特定條件

2.1.1 查詢姓名中帶“冰”的學生名單

hive> select* from student_info where stu_name like "%冰%";

結果

stu_id  stu_name   birthday  sex
017      范冰冰       1992-07-04    女
018      李冰冰       1993-09-24    女

2.1.2 查詢姓“王”老師的個數

hive> select count(*)  wang_count from teacher_info where tea_name like "王%";

結果

wang_count
1

2.1.3 檢索課程編號爲“04”且分數小於60的學生的課程信息,結果按分數降序排列

hive> select stu_id,course_id,score from score_info 
where course_id ='04' and score<60
order by score desc;

結果

stu_id  course_id  score
004   04     59
001   04     54
020   04     50
014   04     40
017   04     34
010   04     34

2.1.4 查詢數學成績不及格的學生和其對應的成績,按照學號升序排序

hive>

select s.stu_id,s.stu_name,t1.score
from student_info s
join (
  select *
  from score_info
  where course_id=(select course_id from course_info where course_name='數學') and score < 60
  ) t1 on s.stu_id = t1.stu_id
order by s.stu_id;

結果

s.stu_id  s.stu_name  t1.score
005   唐國強    44
007   陳坤     55
008   吳京     34
011   潘長江    49
013   蔣欣     35
014   趙麗穎    39
015   劉亦菲    48
017   范冰冰    34
018   李冰冰    58
019   鄧紫棋    39
020   宋丹丹    59

第三章 彙總分析

3.1 彙總分析

3.1.1 查詢編號爲“02”的課程的總成績

hive>
select course_id,sum(score) score_sum
from score_info
where course_id='02'
group by course_id;

結果

course_id   score_sum  
02      1133 

3.1.2 查詢參加考試的學生個數

思路:對成績表中的學號做去重並count

hive> select count(distinct stu_id) stu_num from score_info;

結果

stu_num                                       
19

3.2 分組

3.2.1 查詢各科成績最高和最低的分,以如下的形式顯示:課程號,最高分,最低分

思路:按照學科分組並使用max和min。

hive> select course_id,max(score) max_score,min(score) min_score
from score_info
group by course_id;

結果

course_id  max_score min_score 
01     94      38 
02     93      34 
03     99      32 
04     100     34 
05     87      59 

3.2.2 查詢每門課程有多少學生參加了考試(有考試成績)

hive> select course_id,count(stu_id) stu_num
from score_info
group by course_id;

結果

course_id   stu_num
01      19
02      19
03      19
04      12
05      5

3.2.3 查詢男生、女生人數

hive> select sex,count(stu_id) count
from student_info
group by sex;

結果

sex   count
女    9
男    11

3.3 分組結果的條件

3.3.1 查詢平均成績大於60分的學生的學號和平均成績

1)思路分析

(1)平均成績:展開來說就是計算每個學生的平均成績

(2)這裏涉及到“每個”就是要分組了

(3)平均成績大於60分,就是對分組結果指定條件

(4)首先要分組求出每個學生的平均成績,篩選高於60分的,並反查出這批學生,統計出這些學生總的平均成績。

2)Hql實操

hive> 
select
  stu_id,
  avg(score) score_avg
from score_info
group by stu_id
having score_avg > 60;

結果

stu_id  score_avg
001   72.5
002   86.25
004   81.5
005   75.4
006   73.33333333333333
009   74.2
013   61.0
015   70.25
016   81.25
020   69.75

3.3.2 查詢至少選修四門課程的學生學號

1)思路分析

(1)需要先計算出每個學生選修的課程數據,需要按學號分組

(2)至少選修兩門課程:也就是每個學生選修課程數目>=4,對分組結果指定條件

2)Hql實操

hive> 
select
  stu_id,
  count(course_id) course_count
from score_info
group by stu_id
having course_count >=4;

結果

stu_id	course_num
001		4
002		4
004		4
005		5
007		5
009		5
010		4
013		4
014		4
015		4
016		4
017		4
018		4
020		4

3.3.3 查詢同姓(假設每個學生姓名的第一個字爲姓)的學生名單並統計同姓人數大於2的姓

思路:先提取出每個學生的姓並分組,如果分組的count>=2則爲同姓

hive>
select
  t1.first_name,
  count(*) count_first_name
from (
 select
    stu_id,
    stu_name,
    substr(stu_name,0,1) first_name
    from student_info
) t1
group by t1.first_name
having count_first_name >= 2;

結果

t1.first_name  count_first_name
劉    2
周    2
陳    2

3.3.4 查詢每門課程的平均成績,結果按平均成績升序排序,平均成績相同時,按課程號降序排列

思路:按照課程號分組並求組內的平均值

hive>
select
  course_id,
  avg(score) score_avg
from score_info
group by course_id
order by score_avg asc, course_id desc;

結果

course_id  score_avg
02     59.63157894736842
04     63.416666666666664
01     67.15789473684211
03     69.42105263157895
05     74.6

3.3.5 統計參加考試人數大於等於15的學科

按課程分組並統計組內人數,過濾條件大於等於15

hive>
select
  course_id,
  count(stu_id) stu_count
from score_info
group by course_id
having stu_count >= 15;

結果

course_id  stu_count
01     19
02     19
03     19

3.4 查詢結果排序&分組指定條件

3.4.1 查詢學生的總成績並按照總成績降序排序

思路:分組、sum、排序

hive>
select
  stu_id,
  sum(score) sum_score
from score_info
group by stu_id
order by sum_score desc;

結果

stu_id	sum_score
005	377
009	371
002	345
004	326
016	325
007	299
001	290
015	281
020	279
013	244
010	233
018	232
006	220
014	192
017	181
012	180
011	180
019	178
008	129

3.4.2 按照如下格式顯示學生的語文、數學、英語三科成績,沒有成績的輸出爲0,按照學生的有效平均成績降序顯示

學生id 語文 數學 英語 有效課程數 有效平均成績

hive>
select
 si.stu_id,
 sum(if(ci.course_name='語文',score,0))  `語文`,
 sum(if(ci.course_name='數學',score,0))  `數學`,
 sum(if(ci.course_name='英語',score,0))  `英語`,
 count(*)  `有效課程數`,
 avg(si.score)  `平均成績`
from
 score_info si
join
 course_info ci
on
 si.course_id=ci.course_id
group by
 si.stu_id
order by
 `平均成績` desc;

結果

學生id  語文   數學   英語  有效課程數    平均成績
002   74    84    87    4        86.25
004   85    93    89    4        81.5
016   71    89    71    4        81.25
005   64    44    99    5        75.4
009   75    78    60    5        74.2
006   71    90    59    3        73.33333333333333
001   94    63    79    4        72.5
015   90    48    84    4        70.25
020   89    59    81    4        69.75
013   47    35    93    4        61.0
012   44    74    62    3        60.0
011   61    49    70    3        60.0
007   48    55    70    5        59.8
019   46    39    93    3        59.333333333333336
010   84    68    47    4        58.25
018   38    58    49    4        58.0
014   81    39    32    4        48.0
017   58    34    55    4        45.25
008   56    34    39    3        43.0

3.4.3 查詢一共參加三門課程且其中一門爲語文課程的學生的id和姓名

hive>
select
  t2.stu_id,
  s.stu_name
from (
select t1.stu_id
    from (
        select stu_id,
        course_id
        from score_info
        where stu_id in (
            -- 有一門課程是語文地學生id
            select stu_id
            from score_info
            where course_id = "01"
        )
    ) t1
    group by t1.stu_id
    having count(t1.course_id) = 3
   ) t2
   join student_info s on t2.stu_id = s.stu_id;

結果

t2.stu_id    s.stu_name
006      陳道明
008      吳京
011      潘長江
012      楊紫
019      鄧紫棋

第四章 複雜查詢

4.1 子查詢

4.1.1 查詢所有課程成績均小於60分的學生的學號、姓名

hive>
select s.stu_id,
s.stu_name
from (
    select stu_id,
    -- 巧用 sum與if組合
    sum(if(score >= 60, 1, 0)) flag
    from score_info
    group by stu_id
    having flag = 0
) t1
join student_info s on s.stu_id = t1.stu_id;

結果

s.stu_id  s.stu_name
008      吳京
017      范冰冰

4.1.2 查詢沒有學全所有課的學生的學號、姓名

解釋:沒有學全所有課,也就是該學生選修的課程數 < 總的課程數

hive>
select 
  s.stu_id,
  s.stu_name
from student_info s
left join score_info sc on s.stu_id = sc.stu_id
group by s.stu_id, s.stu_name
having count(course_id) < (
    -- 總課數
    select 
    count(course_id) 
    from course_info
);

結果

s.stu_id  s.stu_name
001   彭于晏
002   胡歌
003   周杰倫
004   劉德華
006   陳道明
008   吳京
010   于謙
011   潘長江
012   楊紫
013   蔣欣
014   趙麗穎
015   劉亦菲
016   周冬雨
017   范冰冰
018   李冰冰
019   鄧紫棋
020   宋丹丹

4.1.3 查詢出只選修了三門課程的全部學生的學號和姓名

解釋:學生選修的課程數 = 3

hive>
select
  s.stu_id,
  s.stu_name
from student_info s
join (
  -- 只選修了三門課程的學生學號
  select
    stu_id
    from score_info
    group by stu_id
    having count(course_id) =3
  ) t1
on s.stu_id = t1.stu_id;

結果

s.stu_id  s.stu_name
006   陳道明
008   吳京
011   潘長江
012   楊紫
019   鄧紫棋

第五章 多表查詢

5.1 表聯結

5.1.1 查詢有兩門以上的課程不及格的同學的學號及其平均成績

① 先找出有兩門以上不及格的學生名單,按照學生分組,過濾組內成績低於60的並進行count,count>=2。

② 接着做出一張表查詢學生的平均成績並和上一個子查詢中的學生學號進行連接

hive>
select
  t1.stu_id,
  t2.avg_score
from (
    -- 兩門課以上不及格學號
    select
    stu_id
    from score_info
    group by stu_id
    having sum(if(score < 60,1,0)) >= 2
) t1
join (
  select
    stu_id,
    avg(score) avg_score
    from score_info
    group by stu_id
) t2 on t1.stu_id = t2.stu_id;

結果

t1.stu_id    t2.avg_score
007      59.8
008      43.0
010      58.25
013      61.0
014      48.0
015      70.25
017      45.25
018      58.0
019      59.333333333333336
020      69.75

5.1.2 查詢所有學生的學號、姓名、選課數、總成績

hive>
select
  s.stu_id,
  s.stu_name,
  count(sc.course_id) count_course,
  sum(sc.score) sum_score
from student_info s
left join score_info sc on s.stu_id = sc.stu_id
group by s.stu_id,s.stu_name;

結果

stu_id    stu_name   course_count   course_sum
001      彭于晏        4        290
002      胡歌         4        345
003	      周杰倫        0        0
004      劉德華        4        326
005      唐國強        5        377
006      陳道明        3        220
007      陳坤         5        299
008      吳京         3        129
009      郭德綱        5        371
010      于謙         4        233
011      潘長江        3        180
012      楊紫         3        180
013      蔣欣         4        244
014      趙麗穎        4        192
015      劉亦菲        4        281
016      周冬雨        4        325
017      范冰冰        4        181
018      李冰冰        4        232
019      鄧紫棋        3        178
020      宋丹丹        4        279

5.1.3 查詢平均成績大於85的所有學生的學號、姓名和平均成績

hive>
select s.stu_id,
s.stu_name,
avg(sc.score) avg_score
from score_info sc
left join student_info s on s.stu_id = sc.stu_id
group by s.stu_id, s.stu_name
having avg_score > 85

結果

stu_id     stu_name    avg_score
002      胡歌       86.25

5.1.4 查詢學生的選課情況:學號,姓名,課程號,課程名稱

hive>
select
  s.stu_id,
  s.stu_name,
  c.course_id,
  c.course_name
from score_info sc
join course_info c on sc.course_id = c.course_id
join student_info s on sc.stu_id = s.stu_id;

結果

s.stu_id	s.stu_name	c.course_id	c.course_name
001	彭于晏	01	語文
002	胡歌	01	語文
004	劉德華	01	語文
005	唐國強	01	語文
006	陳道明	01	語文
007	陳坤	01	語文
008	吳京	01	語文
009	郭德綱	01	語文
010	于謙	01	語文
011	潘長江	01	語文
012	楊紫	01	語文
013	蔣欣	01	語文
014	趙麗穎	01	語文
015	劉亦菲	01	語文
016	周冬雨	01	語文
017	范冰冰	01	語文
018	李冰冰	01	語文
019	鄧紫棋	01	語文
020	宋丹丹	01	語文
001	彭于晏	02	數學
002	胡歌	02	數學
004	劉德華	02	數學
005	唐國強	02	數學
006	陳道明	02	數學
007	陳坤	02	數學
008	吳京	02	數學
009	郭德綱	02	數學
010	于謙	02	數學
011	潘長江	02	數學
012	楊紫	02	數學
013	蔣欣	02	數學
014	趙麗穎	02	數學
015	劉亦菲	02	數學
016	周冬雨	02	數學
017	范冰冰	02	數學
018	李冰冰	02	數學
019	鄧紫棋	02	數學
020	宋丹丹	02	數學
001	彭于晏	03	英語
002	胡歌	03	英語
004	劉德華	03	英語
005	唐國強	03	英語
006	陳道明	03	英語
007	陳坤	03	英語
008	吳京	03	英語
009	郭德綱	03	英語
010	于謙	03	英語
011	潘長江	03	英語
012	楊紫	03	英語
013	蔣欣	03	英語
014	趙麗穎	03	英語
015	劉亦菲	03	英語
016	周冬雨	03	英語
017	范冰冰	03	英語
018	李冰冰	03	英語
019	鄧紫棋	03	英語
020	宋丹丹	03	英語
001	彭于晏	04	體育
002	胡歌	04	體育
004	劉德華	04	體育
005	唐國強	04	體育
007	陳坤	04	體育
009	郭德綱	04	體育
010	于謙	04	體育
013	蔣欣	04	體育
014	趙麗穎	04	體育
016	周冬雨	04	體育
017	范冰冰	04	體育
020	宋丹丹	04	體育
005	唐國強	05	音樂
007	陳坤	05	音樂
009	郭德綱	05	音樂
015	劉亦菲	05	音樂
018	李冰冰	05	音樂

5.1.5 查詢出每門課程的及格人數和不及格人數

hive> 
select
  c.course_id,
  c.course_name,
  t1.`及格人數`,
  t1.`不及格人數`
from course_info c
join (
  select
    course_id,
    sum(if(score >= 60,1,0)) as `及格人數`,
    sum(if(score < 60,1,0)) as `不及格人數`
  from score_info
  group by course_id
  ) t1 on c.course_id = t1.course_id;

結果

c.course_id   c.course_name  t1.及格人數   t1.不及格人數
01       語文       12       7
02       數學       8       11
03       英語       13       6
04       體育       6       6
05       音樂       4       1

5.1.6 查詢課程編號爲03且課程成績在80分以上的學生的學號和姓名及課程信息

hive>
select
  s.stu_id,
  s.stu_name,
  t1.score,
  t1.course_id,
  c.course_name
from student_info s
join (
  select
    stu_id,
    score,
    course_id
  from score_info
  where score > 80 and course_id = '03'
  ) t1
on s.stu_id = t1.stu_id
join course_info c on c.course_id = t1.course_id;

結果

s.stu_id     s.stu_name    t1.score    t1.course_id   c.course_name
002      胡歌       87       03       英語
004      劉德華      89       03       英語
005      唐國強      99       03       英語
013      蔣欣       93       03       英語
015      劉亦菲      84       03       英語
019      鄧紫棋      93       03       英語
020      宋丹丹      81       03       英語

5.2 多表連接

5.2.1 課程編號爲"01"且課程分數小於60,按分數降序排列的學生信息

hive>
select
  s.stu_id,
  s.stu_name,
  s.birthday,
  s.sex,
  t1.score
from student_info s
join (
  select
  stu_id,
  course_id,
  score
  from score_info
  where score < 60 and course_id = '01'
  ) t1
on s.stu_id=t1.stu_id
order by t1.score desc;

結果

s.stu_id     s.stu_name    s.birthday    s.sex  t1.score
017      范冰冰     1992-07-04     女    58
008      吳京      1994-02-06     男    56
007      陳坤      1999-04-09     男    48
013      蔣欣      1997-11-08     女    47
019      鄧紫棋     1994-08-31     女    46
012      楊紫      1996-12-21     女    44
018      李冰冰     1993-09-24     女    38

5.2.2 查詢所有課程成績在70分以上的學生的姓名、課程名稱和分數,按分數升序排列

hive>
select
  s.stu_id,
  s.stu_name,
  c.course_name,
  s2.score
from student_info s
join (
  select
    stu_id,
    sum(if(score >= 70,0,1)) flage
  from score_info
  group by stu_id
  having flage =0
  ) t1
on s.stu_id = t1.stu_id
left join score_info s2 on s.stu_id = s2.stu_id
left join course_info c on s2.course_id = c.course_id;

結果

s.stu_id    s.stu_name   c.course_name  s2.course
002   胡歌   語文   74
002   胡歌   數學   84
002   胡歌   英語   87
002   胡歌   體育   100
016   周冬雨  語文   71
016   周冬雨  數學   89
016   周冬雨  英語   71
016   周冬雨  體育   94

5.2.3 查詢該學生不同課程的成績相同的學生編號、課程編號、學生成績

hive>
select
  sc1.stu_id,
  sc1.course_id,
  sc1.score
from score_info sc1 
join score_info sc2 on sc1.stu_id = sc2.stu_id
and sc1.course_id <> sc2.course_id
and sc1.score = sc2.score;

結果

sc1.stu_id  sc1.course_id   sc1.score
016    03       71
017    04       34
016    01       71
005    05       85
007    05       63
009    05       79
017    02       34
005    04       85
007    04       63
009    04       79

5.2.4 查詢課程編號爲“01”的課程比“02”的課程成績高的所有學生的學號

知識點:多表連接 + 條件

hive>
select
  s1.stu_id
from
(
  select
    sc1.stu_id,
    sc1.score
  from  score_info sc1
  where sc1.course_id ='01'
) s1
join
(
  select
    sc2.stu_id,
    score
  from score_info sc2
  where sc2.course_id ="02"
)s2
on s1.stu_id=s2.stu_id
where s1.score > s2.score;

結果

stu_id
001
005
008
010
011
013
014
015
017
019
020

5.2.5 查詢學過編號爲“01”的課程並且也學過編號爲“02”的課程的學生的學號、姓名

hive>
select
  t1.stu_id as `學號`,
  s.stu_name as `姓名`
from
(
  select
    stu_id
  from score_info sc1
  where sc1.course_id='01'
  and stu_id in (
      select
      stu_id
      from score_info sc2
      where sc2.course_id='02'
  )
)t1
join student_info s
on t1.stu_id = s.stu_id;

結果

學號   姓名
001   彭于晏
002   胡歌
004   劉德華
005   唐國強
006   陳道明
007   陳坤
008   吳京
009   郭德綱
010   于謙
011   潘長江
012   楊紫
013   蔣欣
014   趙麗穎
015   劉亦菲
016   周冬雨
017   范冰冰
018   李冰冰
019   鄧紫棋
020   宋丹丹

5.2.6 課堂講解查詢學過“李體音”老師所教的所有課的同學的學號、姓名

hive>
select
  t1.stu_id,
  si.stu_name
from
(
  select
    stu_id
  from score_info si
  where course_id in
  (
      select
      course_id
      from course_info c
      join teacher_info t
      on c.tea_id = t.tea_id
      where tea_name='李體音'   -- 李體音教的所有課程
  )
  group by stu_id
  having count(*)=2    -- 學習所有課程的學生
)t1
left join student_info si
on t1.stu_id=si.stu_id;

結果

s.stu_id   s.stu_name
005    唐國強
007    陳坤
009    郭德綱

5.2.7 查詢學過“李體音”老師所講授的任意一門課程的學生的學號、姓名

hive>
select
  t1.stu_id,
  si.stu_name
from
(
  select
    stu_id
  from score_info si
  where course_id in
  (
      select
      course_id
      from course_info c
      join teacher_info t
      on c.tea_id = t.tea_id
      where tea_name='李體音'
  )
  group by stu_id
)t1
left join student_info si
on t1.stu_id=si.stu_id;

結果

s.stu_id   s.stu_name
001    彭于晏
002    胡歌
004    劉德華
005    唐國強
007    陳坤
009    郭德綱
010    于謙
013    蔣欣
014    趙麗穎
015    劉亦菲
016    周冬雨
017    范冰冰
018    李冰冰
020    宋丹丹

5.2.8 查詢沒學過"李體音"老師講授的任一門課程的學生姓名

hive>
select
  stu_id,
  stu_name
from student_info
where stu_id not in
(
  select
    stu_id
  from score_info si
  where course_id in
  (
      select
      course_id
      from course_info 
      join teacher_info t
      on c.tea_id = t.tea_id
      where tea_name='李體音'
  )
  group by stu_id
);

結果

stu_id  stu_name
003   周杰倫
006   陳道明
008   吳京
011   潘長江
012   楊紫
019   鄧紫棋

5.2.9 查詢至少有一門課與學號爲“001”的學生所學課程相同的學生的學號和姓名

hive>
select
  si.stu_id,
  si.stu_name
from score_info sc
join student_info si
on sc.stu_id = si.stu_id
where sc.course_id in
(
  select
    course_id
  from score_info
  where stu_id='001'   -- 001的課程
) and sc.stu_id <> '001'  -- 排除001學生
group by si.stu_id,si.stu_name;

結果

s1.stu_id   s2.stu_name
002      胡歌
004      劉德華
005      唐國強
006      陳道明
007      陳坤
008      吳京
009      郭德綱
010      于謙
011      潘長江
012      楊紫
013      蔣欣
014      趙麗穎
015      劉亦菲
016      周冬雨
017      范冰冰
018      李冰冰
019      鄧紫棋
020      宋丹丹

5.2.10 按平均成績從高到低顯示所有學生的所有課程的成績以及平均成績

hive>
select
  si.stu_name,
  ci.course_name,
  sc.score,
  t1.avg_score
from score_info sc
join student_info si
on sc.stu_id=si.stu_id
join course_info ci
on sc.course_id=ci.course_id
join
(
  -- 學生平均成績
  select
    stu_id,
    avg(score) avg_score
  from score_info
  group by stu_id
)t1
on sc.stu_id=t1.stu_id
order by t1.avg_score desc;

結果

t2.stu_name  t2.course_name  t2.score    t1.avg_score
胡歌   體育   100   86.25
胡歌   數學   84    86.25
胡歌   英語   87    86.25
胡歌   語文   74    86.25
劉德華  體育   59    81.5
劉德華  語文   85    81.5
劉德華  英語   89    81.5
劉德華  數學   93    81.5
周冬雨  英語   71    81.25
周冬雨  數學   89    81.25
周冬雨  體育   94    81.25
周冬雨  語文   71    81.25
唐國強  數學   44    75.4
唐國強  音樂   85    75.4
唐國強  語文   64    75.4
唐國強  體育   85    75.4
唐國強  英語   99    75.4
郭德綱  音樂   79    74.2
郭德綱  體育   79    74.2
郭德綱  英語   60    74.2
郭德綱  語文   75    74.2
郭德綱  數學   78    74.2
陳道明  語文   71    73.33333333333333
陳道明  數學   90    73.33333333333333
陳道明  英語   59    73.33333333333333
……
李冰冰  音樂   87    58.0
李冰冰  語文   38    58.0
李冰冰  英語   49    58.0
李冰冰  數學   58    58.0
趙麗穎  數學   39    48.0
趙麗穎  語文   81    48.0
趙麗穎  體育   40    48.0
趙麗穎  英語   32    48.0
范冰冰  英語   55    45.25
范冰冰  體育   34    45.25
范冰冰  數學   34    45.25
范冰冰  語文   58    45.25
吳京   語文   56    43.0
吳京   數學   34    43.0
吳京   英語   39    43.0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章