hive sql练习_经典的SparkSQL/Hive-SQL/MySQL面试-练习题
第一題
需求:
已知一個表order,有如下字段:date_time,order_id,user_id,amount。 數(shù)據(jù)樣例:2020-10-10,1003003981,00000001,1000,請用sql進行統(tǒng)計: (1)2019年每個月的訂單數(shù)、用戶數(shù)、總成交金額。 (2)2020年10月的新客數(shù)(指在2020年10月才有第一筆訂單)實現(xiàn):
(1) SELECT t1.year_month, count(t1.order_id) AS order_cnt, count(DISTINCT t1.user_id) AS user_cnt, sum(amount) AS total_amount FROM(SELECT order_id,user_id,amount, date_format(date_time,'yyyy-MM') year_month FROM test_db.test3 WHERE date_format(date_time,'yyyy') = '2019') t1 GROUP BY t1.year_month;(2) SELECT count(user_id) FROM test_db.test3 GROUP BY user_id HAVING date_format(min(date_time),'yyyy-MM')='2020-10';第二題
需求:
存在如下客戶訪問商鋪的數(shù)據(jù),訪問日志存儲的表名為user_visit,訪客的用戶id為user_id,被訪問的店鋪名稱為shop_name。 數(shù)據(jù)如下: +--------+-----------+ |user_id | shop_name| +--------+-----------+ | u1|beautiful_a| | u2|beautiful_b| | u1|beautiful_b| | u3|beautiful_c| | u4|beautiful_b| | u1|beautiful_a| | u5|beautiful_b| | u4|beautiful_b| | u6|beautiful_c| | u1|beautiful_b| | u2|beautiful_a| | u5|beautiful_a| +--------+-----------+實現(xiàn):
(1) SELECT shop_name, count(*) uv FROM(SELECT user_id,shop_name FROM test_db.user_visit GROUP BY user_id, shop_name) t GROUP BY shop_name as t;(2) SELECT t2.shop_name,t2.user_id,t2.cnt FROM(SELECT t1.*,row_number() over(partition BY t1.shop_name ORDER BY t1.cnt DESC) rank FROM(SELECT user_id,shop_name, count(*) AS cnt FROM test_db.user_visit GROUP BY user_id, shop_name) t1) t2 WHERE rank < 4;第三題
需求:
有如下的用戶訪問數(shù)據(jù) +-------+----------+-----------+ |user_id|visit_date|visit_count| +-------+----------+-----------+ | u01| 2017/1/21| 5| | u02| 2017/1/23| 6| | u03| 2017/1/22| 8| | u04| 2017/1/20| 3| | u01| 2017/1/23| 6| | u01| 2017/2/21| 8| | u02| 2017/1/23| 6| | u01| 2017/2/22| 4| +-------+----------+-----------+要求使用SQL統(tǒng)計出每個用戶的累積訪問次數(shù),如下表所示: +-------+-----------+------------------+---------------+ |user_id|visit_month|month_total_visit_cnt|total_visit_cnt| +-------+-----------+------------------+---------------+ | u01| 2017-01| 11| 11| | u01| 2017-02| 12| 23| | u02| 2017-01| 12| 12| | u03| 2017-01| 8| 8| | u04| 2017-01| 3| 3| +-------+-----------+------------------+---------------+實現(xiàn):
SELECT t2.user_id,t2.visit_month,month_total_visit_cnt, sum(month_total_visit_cnt) over (partition BY user_id ORDER BY visit_month) AS total_visit_cnt FROM(SELECT user_id,visit_month, sum(visit_count) AS month_total_visit_cnt FROM(SELECT user_id, date_format(regexp_replace(visit_date,'/','-'),'yyyy-MM') AS visit_month,visit_count FROM test_db.test1) t1 GROUP BY user_id, visit_month) t2 ORDER BY t2.user_id, t2.visit_month;第四題
需求:
表user(user_id,name,age)記錄用戶信息,表view_record(user_id,movie_name)記錄用戶觀影信息,請根據(jù)年齡段(每10歲為一個年齡段,70以上的單獨作為一個年齡段)觀看電影的次數(shù)進行排序?實現(xiàn):
SELECTt2.age_group, sum(t1.cnt) as view_cnt FROM(SELECT user_id, count(*) cnt FROM test_db.view_record GROUP BY user_id) t1 JOIN(SELECT user_id, CASE WHEN age <= 10 AND age > 0 THEN '0-10' WHEN age <= 20 AND age > 10 THEN '10-20' WHEN age >20 AND age <=30 THEN '20-30' WHEN age >30 AND age <=40 THEN '30-40' WHEN age >40 AND age <=50 THEN '40-50' WHEN age >50 AND age <=60 THEN '50-60' WHEN age >60 AND age <=70 THEN '60-70' ELSE '70以上' END as age_group FROM test_db.user) t2 ON t1.user_id = t2.user_id GROUP BY t2.age_group ORDER BY t2.age_group;第五題
需求:
有日志如下,請用SQL求得所有用戶和活躍用戶的總數(shù)及平均年齡。(活躍用戶指連續(xù)兩天都有訪問記錄的用戶) 日期 用戶 年齡 +----------+-------+---+ | date_time|user_id|age| +----------+-------+---+ |2019-02-12| 2| 19| |2019-02-11| 1| 23| |2019-02-11| 3| 39| |2019-02-11| 1| 23| |2019-02-11| 3| 39| |2019-02-13| 1| 23| |2019-02-15| 2| 19| |2019-02-11| 2| 19| |2019-02-11| 1| 23| |2019-02-16| 2| 19| +----------+-------+---+實現(xiàn):
SELECT sum(total_user_cnt) total_user_cnt,sum(total_user_avg_age) total_user_avg_age,sum(two_days_cnt) two_days_cnt,sum(avg_age) avg_age FROM(SELECT 0 total_user_cnt,0 total_user_avg_age,count(*) AS two_days_cnt,cast(sum(age) / count(*) AS decimal(5,2)) AS avg_ageFROM(SELECT user_id,max(age) ageFROM(SELECT user_id,max(age) ageFROM(SELECT user_id,age,date_sub(date_time,rank) flagFROM(SELECT date_time,user_id,max(age) age,row_number() over(PARTITION BY user_id ORDER BY date_time) rankFROM test_db.test5GROUP BY date_time,user_id) t1 ) t2GROUP BY user_id, flagHAVING count(*) >=2) t3GROUP BY user_id) t4UNION ALL SELECT count(*) total_user_cnt,cast(sum(age) /count(*) AS decimal(5,2)) total_user_avg_age,0 two_days_cnt,0 avg_ageFROM(SELECT user_id,max(age) ageFROM test_db.test5GROUP BY user_id) t5) t6;第六題
需求:
請用sql寫出所有用戶中在2020年10月份第一次購買商品的金額,表order字段: 購買用戶:user_id,金額:money,購買時間:pay_time(格式:2017-10-01),訂單id:order_id實現(xiàn):
SELECT user_id, pay_time, money, order_id FROM (SELECT user_id, money, pay_time, order_id, row_number() over (PARTITION BY user_id ORDER BY pay_time) rankFROM test_db.order WHERE date_format(pay_time,'yyyy-MM') = '2020-10') t WHERE rank = 1;第七題
需求:
有一個賬號表如下,請寫出SQL語句,查詢各自區(qū)組的money排名前3的賬號 dist_id string '區(qū)組id', account string '賬號', gold_coin int '金幣'實現(xiàn):
SELECT dist_id,account,gold_coin FROM(SELECT dist_id,account,gold_coin,row_number () over (PARTITION BY dist_id ORDER BY gold_coin DESC) rankFROM test_db.test9) t WHERE rank <= 3;第八題
需求:
充值日志表credit_log,字段如下: `dist_id` int '區(qū)組id', `account` string '賬號', `money` int '充值金額', `create_time` string '訂單時間' 請寫出SQL語句,查詢充值日志表2020年08月08號每個區(qū)組下充值額最大的賬號,要求結果: 區(qū)組id,賬號,金額,充值時間實現(xiàn):
WITH temp AS(SELECT dist_id, account, sum(`money`) sum_money FROM test_db.test8 WHERE date_format(create_time,'yyyy-MM-dd') = '2020-08-08' GROUP BY dist_id, account) SELECT t1.dist_id,t1.account,t1.sum_money FROM(SELECT temp.dist_id,temp.account,temp.sum_money, rank() over(partition BY temp.dist_id ORDER BY temp.sum_money DESC) ranks FROM TEMP) t1 WHERE ranks = 1;第九題
需求:
有一個線上服務器訪問日志格式如下(用sql答題) 時間 接口 IP +----------------------------------------+------------+ | date_time |interface |ip | +-------------------+--------------------+------------+ |2016-11-09 15:22:05|/request/user/logout| 110.32.5.23| |2020-09-28 14:23:1 |/api_v1/user/detail | 57.2.1.16 | |2020-09-28 14:59:40|/api_v2/read/buy | 172.6.5.166| +-------------------+--------------------+------------+求2020年9月28號下午14點(14-15點),訪問/api_v1/user/detail接口的top10的ip地址實現(xiàn):
SELECT ip, count(*) AS count FROM test_db.test7 WHERE date_format(date_time,'yyyy-MM-dd HH') >= '2020-09-28 14' AND date_format(date_time,'yyyy-MM-dd HH') < '2020-09-28 15' AND interface='/api_v1/user/detail' GROUP BY ip ORDER BY count desc LIMIT 10;第十題
存在如下表: table student(s_id string, s_name string, s_birth string, s_sex string) table course(c_id string, c_name string, t_id string) table teacher(t_id string, t_name string) table score(s_id string, c_id string, s_score int) 示例數(shù)據(jù): student: 01 趙雷 1993-01-01 男 02 錢電 1989-12-21 男 03 孫雷 2000-05-20 男 04 李云 1990-08-06 男 05 周天 1978-12-01 女 06 吳蘭 1992-03-01 女 07 鄭竹 1989-07-01 男 08 王霞 1993-01-20 女course: 01 語文 02 02 數(shù)學 01 03 英語 03teacher: 01 張三 02 李四 03 王五score: 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分別實現(xiàn)以下需求:
1.查詢"01"課程比"02"課程成績高的學生的信息及課程分數(shù)
SELECT student.*,
a.s_score as s1_score,
b.s_score as s2_score
FROM student
JOIN score a ON a.c_id='01'
JOIN score b ON b.c_id='02'
WHERE a.s_id = student.s_id
AND b.s_id =student.s_id AND a.s_score > b.s_score;
2.查詢"01"課程比"02"課程成績低的學生的信息及課程分數(shù)
SELECT student.*,
a.s_score as s1_score,
b.s_score as s2_score
FROM student
JOIN score a ON a.c_id='01'
JOIN score b ON b.c_id='02'
WHERE a.s_id = student.s_id AND b.s_id = student.s_id AND a.s_score < b.s_score;
3.查詢平均成績大于等于60分的同學的學生編號和學生姓名和平均成績
SELECT student.s_id,
student.s_name,
round(avg (score.s_score),1) as 平均成績
FROM student
JOIN score ON student.s_id = score.s_id
GROUP BY student.s_id,student.s_name
HAVING avg(score.s_score) >= 60;
4.查詢平均成績小于60分的同學的學生編號和學生姓名和平均成績
-- 包括有成績的和無成績的
SELECT student.s_id,
student.s_name,
tmp.avgScore
FROM student
JOIN (
select score.s_id,round(avg(score.s_score),1)as avgScore FROM score group by s_id
) tmp ON tmp.avgScore < 60
WHERE student.s_id=tmp.s_id
UNION ALL
SELECT s2.s_id,s2.s_name,0 as avgScore FROM student s2
WHERE s2.s_id NOT IN (select distinct sc2.s_id FROM score sc2);
5.查詢所有同學的學生編號、學生姓名、選課總數(shù)、所有課程的總成績
SELECT student.s_id,
student.s_name,
(count(score.c_id) )as total_count,
sum(score.s_score)as total_score
FROM student
LEFT JOIN score ON student.s_id = score.s_id
GROUP BY student.s_id,
student.s_name;
6.查詢"李"姓老師的數(shù)量
SELECT t_name,count(1) FROM teacher WHERE t_name LIKE '李%' GROUP BY t_name;
7.查詢學過"張三"老師授課的同學的信息
SELECT student.* FROM student
JOIN score ON student.s_id = score.s_id
JOIN course ON course.c_id = score.c_id
JOIN teacher ON course.t_id = teacher.t_id AND t_name = '張三';
8.查詢沒學過"張三"老師授課的同學的信息
SELECT student.*
FROM student
LEFT JOIN (select s_id FROM score
JOIN course ON course.c_id = score.c_id
JOIN teacher ON course.t_id = teacher.t_id and t_name = '張三') tmp
ON student.s_id = tmp.s_id
WHERE tmp.s_id is null;
9.查詢學過編號為"01"并且也學過編號為"02"的課程的同學的信息
SELECT * from student
JOIN (select s_id FROM score WHERE c_id =1 ) t1
ON student.s_id=t1.s_id
JOIN (select s_id FROM score WHERE c_id =2 ) t2
ON student.s_id=t2.s_id;
10.查詢學過編號為"01"但是沒有學過編號為"02"的課程的同學的信息
SELECT student.* FROM studentJOIN (select s_id FROM score WHERE c_id =1 ) tmp1on student.s_id=tmp1.s_idLEFT JOIN (select s_id FROM score WHERE c_id =2 ) tmp2on student.s_id =tmp2.s_idWHERE tmp2.s_id is null;
11.查詢沒有學全所有課程的同學的信息
select student.* from studentjoin (select count(c_id)num1 from course) tmp1left join(select s_id,count(c_id) num2from score group by s_id) tmp2on student.s_id=tmp2.s_id and tmp1.num1=tmp2.num2where tmp2.s_id is null;
12. 查詢至少有一門課與學號為"01"的同學所學相同的同學的信息
select student.* from studentjoin (select c_id from score where score.s_id=01) tmp1join (select s_id,c_id from score) tmp2on tmp1.c_id =tmp2.c_id and student.s_id =tmp2.s_idwhere student.s_id not in('01')group by student.s_id,s_name,s_birth,s_sex;
13. 查詢和"01"號的同學學習的課程完全相同的其他同學的信息
select student.*,tmp1.course_id from student join (select s_id ,concat_ws('|', collect_set(c_id)) course_id from score group by s_id having s_id not in (1) ) tmp1 on student.s_id = tmp1.s_idjoin (select concat_ws('|', collect_set(c_id)) course_id2 from score where s_id=1 ) tmp2on tmp1.course_id = tmp2.course_id2;
14.查詢沒學過"張三"老師講授的任一門課程的學生姓名
select student.* from student left join (select s_id from score join (select c_id from course join teacher on course.t_id=teacher.t_id and t_name='張三')tmp2on score.c_id=tmp2.c_id )tmpon student.s_id = tmp.s_idwhere tmp.s_id is null;
15.查詢兩門及其以上不及格課程的同學的學號,姓名及其平均成績
select student.s_id,student.s_name,tmp.avg_score from student inner join (select s_id from score where s_score<60 group by score.s_id having count(s_id)>1 ) tmp2 on student.s_id = tmp2.s_idleft join ( select s_id,round(AVG (score.s_score)) avg_score from score group by s_id ) tmpon tmp.s_id=student.s_id;
16.檢索"01"課程分數(shù)小于60,按分數(shù)降序排列的學生信息
select student.*,s_score from student,score
where student.s_id=score.s_id and s_score<60 and c_id='01'
order by s_score desc;
17.按平均成績從高到低顯示所有學生的所有課程的成績以及平均成績
select a.s_id,tmp1.s_score as chinese,tmp2.s_score as math,tmp3.s_score as english,round(avg (a.s_score),2) as avgScorefrom score aleft join (select s_id,s_score from score s1 where c_id='01') tmp1 on tmp1.s_id=a.s_idleft join (select s_id,s_score from score s2 where c_id='02') tmp2 on tmp2.s_id=a.s_idleft join (select s_id,s_score from score s3 where c_id='03') tmp3 on tmp3.s_id=a.s_idgroup by a.s_id,tmp1.s_score,tmp2.s_score,tmp3.s_score order by avgScore desc;
18.查詢各科成績最高分、最低分和平均分
-- 以如下形式顯示:課程ID,課程name,最高分,最低分,平均分,及格率,中等率,優(yōu)良率,優(yōu)秀率:–及格為>=60,中等為:70-80,優(yōu)良為:80-90,優(yōu)秀為:>=90
select course.c_id,course.c_name,tmp.maxScore,tmp.minScore,tmp.avgScore,tmp.passRate,tmp.moderate,tmp.goodRate,tmp.excellentRates from course
join(select c_id,max(s_score) as maxScore,min(s_score)as minScore,
round(avg(s_score),2) avgScore,
round(sum(case when s_score>=60 then 1 else 0 end)/count(c_id),2)passRate,
round(sum(case when s_score>=60 and s_score<70 then 1 else 0 end)/count(c_id),2) moderate,
round(sum(case when s_score>=70 and s_score<80 then 1 else 0 end)/count(c_id),2) goodRate,
round(sum(case when s_score>=80 and s_score<90 then 1 else 0 end)/count(c_id),2) excellentRates
from score group by c_id)tmp on tmp.c_id=course.c_id;
19. 按各科成績進行排序,并顯示排名
select s1.*,row_number()over(order by s1.s_score desc) Ranking
from score s1 where s1.c_id='01'order by noRanking asc
union all select s2.*,row_number()over(order by s2.s_score desc) Ranking
from score s2 where s2.c_id='02'order by noRanking asc
union all select s3.*,row_number()over(order by s3.s_score desc) Ranking
from score s3 where s3.c_id='03'order by noRanking asc;
20.查詢學生的總成績并進行排名
select score.s_id,s_name,sum(s_score) sumscore,row_number()over(order by sum(s_score) desc) Rankingfrom score , studentwhere score.s_id=student.s_idgroup by score.s_id,s_name order by sumscore desc;
21.查詢不同老師所教不同課程平均分從高到低顯示
select course.c_id,course.t_id,t_name,round(avg(s_score),2)as avgscore from course
join teacher on teacher.t_id=course.t_id
join score on course.c_id=score.c_id
group by course.c_id,course.t_id,t_name order by avgscore desc;
22.查詢所有課程的成績第2名到第3名的學生信息及該課程成績
select tmp1.* from
(select * from score where c_id='01' order by s_score desc limit 3)tmp1
order by s_score asc limit 2
union all select tmp2.* from
(select * from score where c_id='02' order by s_score desc limit 3)tmp2
order by s_score asc limit 2
union all select tmp3.* from
(select * from score where c_id='03' order by s_score desc limit 3)tmp3
order by s_score asc limit 2;
23.統(tǒng)計各科成績各分數(shù)段人數(shù)
-- 課程編號,課程名稱,[100-85],[85-70],[70-60],[0-60]及所占百分比
select c.c_id,c.c_name,tmp1.s0_60, tmp1.percentum,tmp2.s60_70, tmp2.percentum,tmp3.s70_85, tmp3.percentum,tmp4.s85_100, tmp4.percentum
from course c
join(select c_id,sum(case when s_score<60 then 1 else 0 end )as s0_60,
round(100*sum(case when s_score<60 then 1 else 0 end )/count(c_id),2)as percentum
from score group by c_id)tmp1 on tmp1.c_id =c.c_id
left join(select c_id,sum(case when s_score<70 and s_score>=60 then 1 else 0 end )as s60_70,
round(100*sum(case when s_score<70 and s_score>=60 then 1 else 0 end )/count(c_id),2)as percentum
from score group by c_id)tmp2 on tmp2.c_id =c.c_id
left join(select c_id,sum(case when s_score<85 and s_score>=70 then 1 else 0 end )as s70_85,
round(100*sum(case when s_score<85 and s_score>=70 then 1 else 0 end )/count(c_id),2)as percentum
from score group by c_id)tmp3 on tmp3.c_id =c.c_id
left join(select c_id,sum(case when s_score>=85 then 1 else 0 end )as s85_100,
round(100*sum(case when s_score>=85 then 1 else 0 end )/count(c_id),2)as percentum
from score group by c_id)tmp4 on tmp4.c_id =c.c_id;
24.查詢學生平均成績及其名次
select tmp.*,row_number()over(order by tmp.avgScore desc) Ranking from
(select student.s_id,
student.s_name,
round(avg(score.s_score),2) as avgScore
from student join score
on student.s_id=score.s_id
group by student.s_id,student.s_name)tmp
order by avgScore desc;
25.查詢各科成績前三名的記錄
1.課程id為01的前三名
select score.c_id,course.c_name,student.s_name,s_score from score
join student on student.s_id=score.s_id
join course on score.c_id='01' and course.c_id=score.c_id
order by s_score desc limit 3;
2.課程id為02的前三名
select score.c_id,course.c_name,student.s_name,s_score
from score
join student on student.s_id=score.s_id
join course on score.c_id='02' and course.c_id=score.c_id
order by s_score desc limit 3;
3.課程id為03的前三名
select score.c_id,course.c_name,student.s_name,s_score
from score
join student on student.s_id=score.s_id
join course on score.c_id='03' and course.c_id=score.c_id
order by s_score desc limit 3;
26.查詢每門課程被選修的學生數(shù)
select c.c_id,c.c_name,tmp.number from course cjoin (select c_id,count(1) as number from score where score.s_score<60 group by score.c_id ) tmpon tmp.c_id=c.c_id;
27.查詢出只有兩門課程的全部學生的學號和姓名
select st.s_id,st.s_name from student stjoin (select s_id from score group by s_id having count(c_id) =2) tmpon st.s_id=tmp.s_id;
28.查詢男生、女生人數(shù)
select tmp1.man,tmp2.women from
(select count(1) as man from student where s_sex='男')tmp1,
(select count(1) as women from student where s_sex='女')tmp2;
29.查詢名字中含有"風"字的學生信息
select * from student where s_name like '%風%';
30.查詢同名同性學生名單,并統(tǒng)計同名人數(shù)
select s1.s_id,s1.s_name,s1.s_sex,count(*) as sameName
from student s1,student s2
where s1.s_name=s2.s_name and s1.s_id<>s2.s_id and s1.s_sex=s2.s_sex
group by s1.s_id,s1.s_name,s1.s_sex;
31.查詢1990年出生的學生名單
select * from student where s_birth like '1990%';
32.查詢每門課程的平均成績,結果按平均成績降序排列,平均成績相同時,按課程編號升序排列
select score.c_id,c_name,round(avg(s_score),2) as avgScore from score
join course on score.c_id=course.c_id
group by score.c_id,c_name order by avgScore desc,score.c_id asc;
33.查詢平均成績大于等于85的所有學生的學號、姓名和平均成績
select score.s_id,s_name,round(avg(s_score),2)as avgScore from score
join student on student.s_id=score.s_id
group by score.s_id,s_name having avg(s_score) >= 85;
34.查詢課程名稱為"數(shù)學",且分數(shù)低于60的學生姓名和分數(shù)
select s_name,s_score as mathScore from studentjoin (select s_id,s_score from score,course where score.c_id=course.c_id and c_name='數(shù)學' ) tmpon tmp.s_score < 60 and student.s_id=tmp.s_id;
35.查詢所有學生的課程及分數(shù)情況
select a.s_name,
SUM(case c.c_name when '語文' then b.s_score else 0 end ) as chainese,
SUM(case c.c_name when '數(shù)學' then b.s_score else 0 end ) as math,
SUM(case c.c_name when '英語' then b.s_score else 0 end ) as english,
SUM(b.s_score) as sumScore
from student a
join score b on a.s_id=b.s_id
join course c on b.c_id=c.c_id
group by s_name,a.s_id;
36.查詢任何一門課程成績在70分以上的學生姓名、課程名稱和分數(shù)
select student.s_id,s_name,c_name,s_score from student
join (select sc.* from score sc
left join(select s_id from score where s_score < 70 group by s_id)tmp
on sc.s_id=tmp.s_id where tmp.s_id is null)tmp2
on student.s_id=tmp2.s_id
join course on tmp2.c_id=course.c_id
order by s_id;
-- 查詢全部及格的信息
select sc.* from score sc
left join(select s_id from score where s_score < 60 group by s_id)tmp
on sc.s_id=tmp.s_id
where tmp.s_id is null;
-- 或(效率低)
select sc.* from score sc
where sc.s_id not in (select s_id from score where s_score < 60 group by s_id);
37.查詢課程不及格的學生
select s_name,c_name as courseName,tmp.s_score
from student
join (select s_id,s_score,c_name
from score,course
where score.c_id=course.c_id and s_score < 60)tmp
on student.s_id=tmp.s_id;
38.查詢課程編號為01且課程成績在80分以上的學生的學號和姓名
select student.s_id,s_name,s_score as score_01
from student
join score on student.s_id=score.s_id
where c_id='01' and s_score >= 80;
39.求每門課程的學生人數(shù)
select course.c_id,course.c_name,count(1)as selectNum
from course
join score on course.c_id=score.c_id
group by course.c_id,course.c_name;
40.查詢選修"張三"老師所授課程的學生中,成績最高的學生信息及其成績
select student.*,tmp3.c_name,tmp3.maxScore
from (select s_id,c_name,max(s_score)as maxScore from score
join (select course.c_id,c_name from course join
(select t_id,t_name from teacher where t_name='張三')tmp
on course.t_id=tmp.t_id)tmp2
on score.c_id=tmp2.c_id group by score.s_id,c_name
order by maxScore desc limit 1)tmp3
join student
on student.s_id=tmp3.s_id;
41.查詢不同課程成績相同的學生的學生編號、課程編號、學生成績
select distinct a.s_id,a.c_id,a.s_score from score a,score b
where a.c_id <> b.c_id and a.s_score=b.s_score;
42.查詢每門課程成績最好的前三名
select tmp1.* from
(select *,row_number()over(order by s_score desc) ranking
from score where c_id ='01')tmp1
where tmp1.ranking <= 3
union all
select tmp2.* from
(select *,row_number()over(order by s_score desc) ranking
from score where c_id ='02')tmp2
where tmp2.ranking <= 3
union all
select tmp3.* from
(select *,row_number()over(order by s_score desc) ranking
from score where c_id ='03')tmp3
where tmp3.ranking <= 3;
43.統(tǒng)計每門課程的學生選修人數(shù)(超過5人的課程才統(tǒng)計)
– 要求輸出課程號和選修人數(shù),查詢結果按人數(shù)降序排列,若人數(shù)相同,按課程號升序排列
select distinct course.c_id,tmp.num from course
join (select c_id,count(1) as num from score group by c_id)tmp
where tmp.num>=5 order by tmp.num desc ,course.c_id asc;
44.檢索至少選修兩門課程的學生學號
select s_id,count(c_id) as totalCourse
from score
group by s_id
having count(c_id) >= 2;
45.查詢選修了全部課程的學生信息
select student.*
from student,
(select s_id,count(c_id) as totalCourse
from score group by s_id)tmp
where student.s_id=tmp.s_id and totalCourse=3;
46.查詢下周過生日的學生
select s_name,s_sex,s_birth from student
where substring(s_birth,6,2)='10'
and substring(s_birth,9,2)>=15
and substring(s_birth,9,2)<=21;
47.查詢本月過生日的學生
select s_name,s_sex,s_birth from student where substring(s_birth,6,2)='10';
48.查詢12月份過生日的學生
select s_name,s_sex,s_birth from student where substring(s_birth,6,2)='12';
49.查詢各學生的年齡(周歲)
–- 按照出生日期來算,當前月日 < 出生年月的月日則,年齡減一
select s_name,s_birth,
(year(CURRENT_DATE)-year(s_birth)-
(case when month(CURRENT_DATE) < month(s_birth) then 1
when month(CURRENT_DATE) = month(s_birth) and day(CURRENT_DATE) < day(s_birth) then 1
else 0 end)
) as age
from student;
50.查詢本周過生日的學生
select s_name,s_sex,s_birth from student
where substring(s_birth,6,2)='10'
and substring(s_birth,9,2)=14;
關注 微信公眾號:大數(shù)據(jù)學習與分享,獲取更多技術干貨
推薦文章:
聊聊今年如何準備找工作?mp.weixin.qq.com筆試編程 | 二分查找、數(shù)組、排序?mp.weixin.qq.comHive Join優(yōu)化?mp.weixin.qq.com系統(tǒng)解析Apache Hive?mp.weixin.qq.comSpark SQL | 目前Spark社區(qū)最活躍的組件之一?mp.weixin.qq.com總結
以上是生活随笔為你收集整理的hive sql练习_经典的SparkSQL/Hive-SQL/MySQL面试-练习题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mcem r语言代码_R语言阈值自回归模
- 下一篇: 猴子选大王 java,PAT-JAVA-