1. 程式人生 > >hive學習之經典sql50題 hive版(四)

hive學習之經典sql50題 hive版(四)

21.查詢男生、女生人數 
select f.c,m.c
from
(
select count(sid) c from student where ssex='男'
) f
join
(
select count(sid) c from student where ssex='女'

) m;

22.查詢名字中含有"風"字的學生資訊

select * from student where sname like '%風%';

23.查詢同名同性學生名單,並統計同名人數 
select sname,ssex,count(1)
from student
group by sname,ssex

having count(1)>1;

24.查詢1990年出生的學生名單

select sname from student where substring(sage,0,4)='1990';

25.查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列
select cid c,round(avg(score),1) a from sc group by cid order by a,c desc;


26.查詢不及格的課程,並按課程號從大到小排列 

select cid c,score from sc where score<60 order by c desc; 

27.查詢每門功課成績最好的前兩名 
select s.sid,s.cid,s.score
from
(
select  sid,cid,score,row_number()over(partition by cid order by score desc) rank from sc
) s

where s.rank<=2;

28.統計每門課程的學生選修人數(超過5人的課程才統計)。要求輸出課程號和選修人數,查詢結果按人數降序排列,若人數相同,按課程號升序排列

select cid,count(sid) c from sc group by cid having count(sid)>5 order by c desc,cid;

29.檢索至少選修兩門課程的學生學號

select sid,count(cid) c from sc group by sid having c>=2;

30.查詢選修了全部課程的學生資訊 
select student.sid,student.sname
from
student
join
(
select s.sid
from
(
select count(cid) c from course
) cou
join
(
select sid,count(cid) c from sc group by sid
) s
on cou.c=s.c
) o
on student.sid=o.sid;