數據:
student.csv
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 女
course.csv
01 語文 02
02 數學 01
03 英語 03
teacher.csv
01 張三
02 李四
03 王五
score.csv
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
編寫一個sql腳本創建數據庫和表:
sparksql.sql
drop database if exists 'sparksql';
create database if not exists `sparksql`;
use sparksql;
create table sparksql.student(
sid string,
name string,
birthday string,
sex string
)
row format delimited
fields terminated by '\t';
create table sparksql.course(
cid string,
name string,
tid string
)
row format delimited
fields terminated by '\t';
create table sparksql.teacher(
tid string,
name string
)
row format delimited
fields terminated by '\t';
create table sparksql.score(
sid string,
cid string,
score int
)
row format delimited
fields terminated by '\t';
執行腳本:
hive -f sparksql.sql
編寫一個腳本導入數據:
load.sql
load data local inpath '/myfile/sparksql/student.csv' into table sparksql.student;
load data local inpath '/myfile/sparksql/course.csv' into table sparksql.course;
load data local inpath '/myfile/sparksql/teacher.csv' into table sparksql.teacher;
load data local inpath '/myfile/sparksql/score.csv' into table sparksql.score;
執行腳本:
hive -f load.sql
– 1、查詢"01"課程比"02"課程成績高的學生的信息及課程分數:
select
st.sid sid,
st.name name,
st.birthday birthday,
st.sex sex,
sc01.score,
sc02.score
from student st
join score sc01 on st.sid=sc01.sid and sc01.cid="01"
join score sc02 on st.sid=sc02.sid and sc02.cid="02"
where sc01.score>sc02.score
;
– 2、查詢"01"課程比"02"課程成績低的學生的信息及課程分數:
select
st.sid sid,
st.name name,
st.birthday birthday,
st.sex sex,
sc01.score,
sc02.score
from student st
join score sc01 on st.sid=sc01.sid and sc01.cid="01"
join score sc02 on st.sid=sc02.sid and sc02.cid="02"
where sc01.score<sc02.score
;
– 3、查詢平均成績大於等於60分的同學的學生編號和學生姓名和平均成績:
select
st.sid sid,
st.name name,
tmp.avgScore avgScore
from student st
join (
select
sid,
round(avg(score),2) avgScore
from score
group by sid
) tmp on st.sid=tmp.sid
where tmp.avgScore>60
;
– 4、查詢平均成績小於60分的同學的學生編號和學生姓名和平均成績:
select
st.sid sid,
st.name name,
tmp.avgScore avgScore
from student st
join (
select
sid,
round(avg(score),2) avgScore
from score
group by sid
) tmp on st.sid=tmp.sid
where tmp.avgScore<60
;
– 5、查詢所有同學的學生編號、學生姓名、選課總數、所有課程的總成績:
select
st.sid sid,
st.name name,
count(sc.cid) totalCourses,
sum(sc.score) totalScore
from student st
join score sc on st.sid=sc.sid
group by st.sid,st.name
;
– 6、查詢"李"姓老師的數量:
select
count(*) countLi
from teacher
where name like "李%"
;
– 7、查詢學過"張三"老師授課的同學的信息:
select
st.sid sid,
st.name name,
st.birthday birthday,
st.sex sex
from student st
join score sc on st.sid=sc.sid
join course co on sc.cid=co.cid
join teacher te on co.tid=te.tid
where te.name="張三"
;
– 8、查詢沒學過"張三"老師授課的同學的信息:
select
st.sid sid,
st.name name,
st.birthday birthday,
st.sex sex
from student st
join (
select
sid
from score sc
join course co on sc.cid=co.cid
join teacher te on co.tid=te.tid and te.name="張三"
) tmp on st.sid=tmp.sid
where tmp.sid is null
;
– 9、查詢學過編號爲"01"並且也學過編號爲"02"的課程的同學的信息:
select
st.sid sid,
st.name name,
st.birthday birthday,
st.sex sex
from student st
join (
select
sid
from score
where cid="01"
) tmp1 on st.sid=tmp1.sid
join (
select
sid
from score
where cid="02"
) tmp2 on st.sid=tmp2.sid
;
– 10、查詢學過編號爲"01"但是沒有學過編號爲"02"的課程的同學的信息:
select
st.sid sid,
st.name name,
st.birthday birthday,
st.sex sex
from student st
join (
select
sid
from score
where cid="01"
) tmp1 on st.sid=tmp1.sid
join (
select
sid
from score
where cid="02"
) tmp2 on st.sid=tmp2.sid
where tmp2.sid is null
;
– 11、查詢沒有學全所有課程的同學的信息:
select
st.sid sid,
st.name name,
st.birthday birthday,
st.sex sex
from student st
join (
select count(cid) num1
from course
) tmp01
join (
select
sid,
count(cid) num2
from score
group by sid
) tmp02 on st.sid=tmp02.sid and tmp01.num1=tmp02.num2
where tmp02.sid is null
;
– 12、查詢至少有一門課與學號爲"01"的同學所學相同的同學的信息:
select
st.sid sid,
st.name name,
st.birthday birthday,
st.sex sex
from student st
join (
select
cid
from score
where score.sid="01"
)tmp01
join (
select
sid,
cid
from score
) tmp02 on tmp01.cid=tmp02.cid and st.sid=tmp02.sid
where st.sid not in ("01")
group by st.sid,st.name,st.birthday,st.sex
;
– 13、查詢和"01"號的同學學習的課程完全相同的其他同學的信息:
select
st.sid sid,
st.name name,
st.birthday birthday,
st.sex sex
from student st
join (
select
sid,
concat_ws("|",collect_set(cid)) course_id
from score
group by sid
having sid not in ("01")
) tmp01 on st.sid=tmp01.sid
join (
select
concat_ws("|",collect_set(cid)) course_id
from score
where sid="01"
) tmp02 on tmp01.course_id=tmp02.course_id
;
– 14、查詢沒學過"張三"老師講授的任一門課程的學生姓名:
select
st.sid sid,
st.name name
from student st
join (
select
sid
from score sc
join course co on sc.cid=co.cid
join teacher te on co.tid=te.tid and te.name="張三"
) tmp on st.sid=tmp.sid
where tmp.sid is null
;
– 15、查詢兩門及其以上不及格課程的同學的學號,姓名及其平均成績:
select
st.sid sid,
st.name name,
tmp2.avgScore
from student st
join (
select sid
from score
where score<60
group by score.sid
having count(sid)>1
) tmp1 on st.sid = tmp1.sid
join (
select sid,
round(avg (score.score)) avgScore
from score
group by sid
) tmp2 on tmp2.sid=st.sid
;
– 16、檢索"01"課程分數小於60,按分數降序排列的學生信息:
select
st.sid sid,
st.name name,
st.birthday birthday,
st.sex sex,
sc.score score
from student st
join score sc on st.sid=sc.sid and cid="01"
where sc.score<60
order by score desc
;
– 17、按平均成績從高到低顯示所有學生的所有課程的成績以及平均成績:
select
st.sid sid,
st.name name,
tmp1.score Chinese,
tmp2.score Math,
tmp3.score English,
round(avg(sc.score),2) avgScore
from student st
join score sc on st.sid=sc.sid
join (
select
sid,
score
from score sc01
where cid="01"
) tmp1 on st.sid=tmp1.sid
join (
select
sid,
score
from score sc02
where cid="02"
) tmp2 on st.sid=tmp2.sid
join (
select
sid,
score
from score sc03
where cid="03"
) tmp3 on st.sid=tmp3.sid
group by st.sid,st.name,tmp1.score,tmp2.score,tmp3.score
order by avgscore desc
;
– 18、查詢各科成績最高分、最低分和平均分:以如下形式顯示:課程ID,課程name,最高分,最低分,平均分,及格率,中等率,優良率,優秀率:
– –及格爲>=60,中等爲:70-80,優良爲:80-90,優秀爲:>=90
select
co.cid cid,
co.name coName,
tmp.maxScore maxScore,
tmp.minScore minScore,
tmp.avgScore avgscore,
tmp.passRate passRate,
tmp.moderate moderate,
tmp.goodRate goodRate,
tmp.excellentRate excellentRate
from course co
join (
select
cid,
max(score) maxScore,
min(score) minScore,
round(avg(score),2) avgscore,
round(sum(case when score>=60 then 1 else 0 end)/count(cid),2) passRate,
round(sum(case when score>=60 and score<70 then 1 else 0 end)/count(cid),2) moderate,
round(sum(case when score>=70 and score<80 then 1 else 0 end)/count(cid),2) goodRate,
round(sum(case when score>=80 and score<90 then 1 else 0 end)/count(cid),2) excellentRate
from score
group by cid
) tmp on co.cid=tmp.cid
;
– 19、按各科成績進行排序,並顯示排名:
select
sc01.*,
row_number() over(order by sc01.score desc) rn
from score sc01
where sc01.sid="01"
union all
select
sc02.*,
row_number() over(order by sc02.score desc) rn
from score sc02
where sc02.sid="02"
union all
select
sc03.*,
row_number() over(order by sc03.score desc) rn
from score sc03
where sc03.sid="03"
;
– 20、查詢學生的總成績並進行排名:
select
st.sid sid,
st.name name,
sum(sc.score) sumScore,
row_number() over(order by sum(sc.score) desc) rn
from student st
join score sc on st.sid=sc.sid
group by st.sid,st.name
;
– 21、查詢不同老師所教不同課程平均分從高到低顯示:
select
co.cid cid,
co.tid tid,
te.name name,
round(avg(score),2) avgScore
from course co
join teacher te on te.tid=co.tid
join score sc on co.cid=sc.cid
group by co.cid,co.tid,te.name
order by avgScore desc
;
– 22、查詢所有課程的成績第2名到第3名的學生信息及該課程成績:
select tmp1.*
from (
select *
from score
where cid="01"
order by score desc
limit 3
) tmp1
order by score asc
limit 2
union all
select tmp2.*
from (
select *
from score
where cid="02"
order by score desc
limit 3
) tmp2
order by score asc
limit 2
union all
select tmp3.*
from (
select *
from score
where cid="03"
order by score desc
limit 3
) tmp3
order by score asc
limit 2
;
– 23、統計各科成績各分數段人數:課程編號,課程名稱,[100-85],[85-70],[70-60],[0-60]及所佔百分比
select
co.cid,
co.name,
tmp1.s0_60,
tmp1.percentum,
tmp2.s60_70,
tmp2.percentum,
tmp3.s70_85,
tmp3.percentum,
tmp4.s85_100,
tmp4.percentum
from course co
join (
select cid,
sum(case when score<60 then 1 else 0 end ) s0_60,
round(100*sum(case when score<60 then 1 else 0 end )/count(cid),2) percentum
from score
group by cid
) tmp1 on tmp1.cid =co.cid
join (
select cid,
sum(case when score<70 and score>=60 then 1 else 0 end ) s60_70,
round(100*sum(case when score<70 and score>=60 then 1 else 0 end )/count(cid),2) percentum
from score
group by cid
) tmp2 on tmp2.cid =co.cid
join (
select cid,
sum(case when score<85 and score>=70 then 1 else 0 end ) s70_85,
round(100*sum(case when score<85 and score>=70 then 1 else 0 end )/count(cid),2) percentum
from score
group by cid
) tmp3 on tmp3.cid =co.cid
join (
select cid,
sum(case when score>=85 then 1 else 0 end ) s85_100,
round(100*sum(case when score>=85 then 1 else 0 end )/count(cid),2) percentum
from score
group by cid
) tmp4 on tmp4.cid =co.cid
;
– 24、查詢學生平均成績及其名次:
select
tmp.*,
row_number() over(order by tmp.avgscore desc) rank
from (
select
st.sid,
st.name,
round(avg(sc.score),2) avgScore
from student st
join score sc on st.sid=sc.sid
group by st.sid,st.name
) tmp
order by avgscore desc
;
– 25、查詢各科成績前三名的記錄:
select *
from (
select
sc.cid cid,
co.name coName,
st.name name,
sc.score score
from student st
join score sc on st.sid=sc.sid
join course co on sc.cid=co.cid and sc.cid="01"
order by score desc
limit 3
) tmp1
union all
select *
from (
select
sc.cid cid,
co.name coName,
st.name name,
sc.score score
from student st
join score sc on st.sid=sc.sid
join course co on sc.cid=co.cid and sc.cid="02"
order by score desc
limit 3
) tmp2
union all
select *
from (
select
sc.cid cid,
co.name coName,
st.name name,
sc.score score
from student st
join score sc on st.sid=sc.sid
join course co on sc.cid=co.cid and sc.cid="03"
order by score desc
limit 3
) tmp3
;
– 26、查詢每門課程被選修的學生數:
select
co.cid cid,
co.name coName,
tmp.num num
from course co
join (
select
cid,
count(1) num
from score
where score<60
group by cid
) tmp on co.cid=tmp.cid
;
– 27、查詢出只有兩門課程的全部學生的學號和姓名:
select
st.sid sid,
st.name name
from student st
join (
select
sid
from score
group by sid
having count(cid)=2
) tmp on st.sid=tmp.sid
;
– 28、查詢男生、女生人數:
select
tmp1.male male,
tmp2.female female
from (
select
count(1) as male
from student
where sex="男"
) tmp1,
(select
count(1) female
from student
where sex="女"
) tmp2
;
– 29、查詢名字中含有"風"字的學生信息:
select
st.sid sid,
st.name name,
st.birthday birthday,
st.sex sex
from student st
where name like "%風%"
;
– 30、查詢同名同性學生名單,並統計同名人數:
select
st01.sid sid,
st01.name name,
st01.sex sex,
count(*) sameName
from student st01
join student stu02 on st01.name=stu02.name and st01.sex=stu02.sex
where st01.sid<>stu02.sid
group by st01.sid,st01.name,st01.sex
;
– 31、查詢1990年出生的學生名單:
select
st.sid sid,
st.name name,
st.birthday birthday,
st.sex sex
from student st
where birthday like "1990%"
;
select
st.sid sid,
st.name name,
st.birthday birthday,
st.sex sex
from student st
where substring(birthday,0,4)="1990"
;
– 32、查詢每門課程的平均成績,結果按平均成績降序排列,平均成績相同時,按課程編號升序排列:
select
sc.cid cid,
co.name name,
round(avg(sc.score),2) avgScore
from score sc
join course co on sc.cid=co.cid
group by sc.cid,co.name
order by avgScore desc,cid asc
;
– 33、查詢平均成績大於等於85的所有學生的學號、姓名和平均成績:
select
st.sid sid,
st.name name,
round(avg(sc.score),2) avgScore
from student st
join score sc on st.sid=sc.sid
group by st.sid,st.name
having avg(sc.score)>85
;
– 34、查詢課程名稱爲"數學",且分數低於60的學生姓名和分數:
select
st.name name,
tmp.score mathScore
from student st
join (
select
sid,
score
from score sc,course co
where sc.cid=co.cid and co.name="數學"
) tmp on st.sid=tmp.sid
where tmp.score<60
;
– 35、查詢所有學生的課程及分數情況:
select
st.sid sid,
st.name name,
sum(case when co.name="語文" then sc.score else 0 end) as chineseScore,
sum(case when co.name="數學" then sc.score else 0 end) as mathScore,
sum(case when co.name="英語" then sc.score else 0 end) as englishScore
from student st
join score sc on st.sid=sc.sid
join course co on sc.cid=co.cid
group by st.sid,st.name
;
– 36、查詢任何一門課程成績在70分以上的學生姓名、課程名稱和分數:
select
st.name stName,
co.name coName,
sc.score score
from student st
join score sc on st.sid=sc.sid
join course co on sc.cid=co.cid
group by st.name,co.name,sc.score
having min(sc.score)>70
;
– 37、查詢課程不及格的學生:
select
st.sid sid,
st.name name,
tmp.name courseName,
tmp.score score
from student st
join (
select
sid,
score,
name
from score,course
where score.cid=course.cid and score<60
) tmp on st.sid=tmp.sid
;
– 38、查詢課程編號爲01且課程成績在80分以上的學生的學號和姓名:
select
st.sid sid,
st.name name
from student st
join score sc on st.sid=sc.sid
where sc.cid="01" and sc.score>80
;
– 39、求每門課程的學生人數:
select
co.cid cid,
co.name name,
count(1) stuNum
from course co
join score sc on co.cid=sc.cid
group by co.cid,co.name
;
– 40、查詢選修"張三"老師所授課程的學生中,成績最高的學生信息及其成績:
select
st.sid sid,
st.name name,
st.birthday birthday,
st.sex sex,
tmp.cid cid,
tmp.score score
from student st
join (
select
sc.sid sid,
co.cid cid,
sc.score score,
row_number() over(distribute by sc.cid sort by sc.score desc) rn
from score sc
join course co on sc.cid=co.cid
join teacher te on co.tid=te.tid
where te.name="張三" and rn=1
) tmp on st.sid=tmp.sid
;
– 41、查詢不同課程成績相同的學生的學生編號、課程編號、學生成績:
select
distinct sc01.sid sid,
sc01.cid cid,
sc01.score score
from score sc01
join score sc02 on sc01.score=sc02.score
where sc01.cid<>sc02.cid
;
– 42、查詢每門課程成績最好的前三名:
select tmp01.*
from (
select
sid,
cid,
score,
row_number() over(order by score desc) rank
from score
where cid="01"
limit 3
) tmp01
union all
select tmp02.*
from (
select
sid,
cid,
score,
row_number() over(order by score desc) rank
from score
where cid="02"
limit 3
) tmp02
union all
select tmp03.*
from (
select
sid,
cid,
score,
row_number() over(order by score desc) rank
from score
where cid="03"
limit 3
) tmp03
;
– 43、統計每門課程的學生選修人數(超過5人的課程才統計):
– 要求輸出課程號和選修人數,查詢結果按人數降序排列,若人數相同,按課程號升序排列
select
distinct co.cid cid,
tmp.num num
from course co
join (
select
cid,
count(1) num
from score
group by cid
) tmp
where tmp.num>=5
order by tmp.num desc,co.cid asc
;
– 44、檢索至少選修兩門課程的學生學號:
select
sid,
count(cid) totalCourses
from score
group by sid
having count(cid)>=2
;
– 45、查詢選修了全部課程的學生信息:
select
st.sid sid,
st.name name,
st.birthday birthday,
st.sex sex
from student st
join(
select
sid,
count(cid) totalCourses
from score
group by sid
) tmp on st.sid=tmp.sid
where tmp.totalCourses=3
;
– 46、查詢各學生的年齡(週歲):
– 按照出生日期來算,當前月日 < 出生年月的月日則,年齡減一
select
sid,
name,
floor((datediff(current_date,birthday)-floor((year(current_date)-year(birthday))/4))/365) age
from student
;
– 47、查詢本週過生日的學生:
select
sid,
name,
birthday
from student
where weekofyear(current_date)=weekofyear(birthday)
;
– 48、查詢下週過生日的學生:
select
sid,
name,
birthday
from student
where weekofyear(current_date)+1=weekofyear(birthday)
;
– 49、查詢本月過生日的學生:
select
sid,
name,
birthday
from student
where month(current_date)=month(birthday)
;
– 50、查詢12月份過生日的學生:
select
sid,
name,
birthday
from student
where substring(birthday,6,2)="12"
;