1. 程式人生 > >**HQL經典練習50題之(二)**

**HQL經典練習50題之(二)**

26、查詢每門課程被選修的學生數:

select course_id,count(stu_id)
from
t_score
group by course_id
;

執行結果:

1	6
2	6
3	6

27、查詢出只有兩門課程的全部學生的學號和姓名:

select a.*,count(a.stu_id)
from
t_stu_info a join t_score b on a.stu_id=b.stu_id
group by a.stu_id,a.stu_name,a.birthday,a.gender
having count(a.stu_id)=2
;

執行結果:
 
5	周梅	1991-12-01	女	2
6	吳蘭	1992-03-01	女	2
7	鄭竹	1989-07-01	女	2

28、查詢男生、女生人數:

select gender,count(gender)
from
t_stu_info
group by gender
;

執行結果:
 
女	4
男	4

29、查詢名字中含有"風"字的學生資訊:

select *
from
t_stu_info
where stu_name like '%風%'
;

執行結果:
 
3	孫風	1990-05-20	男

//從該題開始向表中添加了兩條 同名 資料

30、查詢同名同性學生名單,並統計同名人數:

select stu_name,gender,count(stu_name)
from
t_stu_info
group by stu_name,gender
having count(stu_name)>1
;

執行結果:

王菊	女	2
鄭竹	女	2

31、查詢1990年出生的學生名單:

select t_stu_info.*
from
t_stu_info
where substring(birthday,0,4)='1990'
;

執行結果:

1	趙雷	1990-01-01	男
2	錢電	1990-12-21	男
3	孫風	1990-05-20	男
4	李雲	1990-08-06	男
8	王菊	1990-01-20	女
10	王菊	1990-02-09	男

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

select course_id,
round(avg(score),2) as avg
from
t_score
group by course_id
order by avg desc,course_id asc
;
執行結果:

2	72.67
3	68.5
1	64.5

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

select *
from
(
select a.*,
round(avg(b.score),2) as avg
from
t_stu_info a join t_score b on a.stu_id=b.stu_id
group by a.stu_id,a.stu_name,a.birthday,a.gender
) a
where a.avg>85
;

執行結果:

1	趙雷	1990-01-01	男	89.67
7	鄭竹	1989-07-01	女	93.5

34、查詢課程名稱為"數學",且分數低於60的學生姓名和分數:

select 
a.*,
b.score,
c.course
from
t_stu_info a  left join t_score b on a.stu_id=b.stu_id
left join t_course c on b.course_id=c.course_id
where (c.course='數學' and b.score<60) or c.course is null
;

執行結果:

4	李雲	1990-08-06	男	30		數學
8	王菊	1990-01-20	女	NULL	NULL
9	鄭竹	1989-07-02	男	NULL	NULL
10	王菊	1990-02-09	男	NULL	NULL

35、查詢所有學生的課程及分數情況:

select a.*,b.score,c.course
from
t_stu_info a left join t_score b on a.stu_id=b.stu_id
left join t_course c on b.course_id=c.course_id
;

執行結果:

1	趙雷	1990-01-01	男	80	語文
1	趙雷	1990-01-01	男	90	數學
1	趙雷	1990-01-01	男	99	英語
2	錢電	1990-12-21	男	70	語文
2	錢電	1990-12-21	男	60	數學
2	錢電	1990-12-21	男	80	英語
3	孫風	1990-05-20	男	80	語文
3	孫風	1990-05-20	男	80	數學
3	孫風	1990-05-20	男	80	英語
4	李雲	1990-08-06	男	50	語文
4	李雲	1990-08-06	男	30	數學
4	李雲	1990-08-06	男	20	英語
5	周梅	1991-12-01	女	76	語文
5	周梅	1991-12-01	女	87	數學
6	吳蘭	1992-03-01	女	31	語文
6	吳蘭	1992-03-01	女	34	英語
7	鄭竹	1989-07-01	女	89	數學
7	鄭竹	1989-07-01	女	98	英語
8	王菊	1990-01-20	女	NULL	NULL
9	鄭竹	1989-07-02	男	NULL	NULL
10	王菊	1990-02-09	男	NULL	NULL

36、查詢任何一門課程成績在70分以上的學生姓名、課程名稱和分數:

select c.stu_id,c.stu_name,e.course,d.score
from
(
select a.stu_id,a.stu_name
from
t_stu_info a  left join t_score b on a.stu_id=b.stu_id
group by a.stu_id,stu_name
having sum(case when b.score >=70 then 1 else 0 end)> 0
) c join t_score d on c.stu_id=d.stu_id
join t_course e on d.course_id=e.course_id
;

執行結果:

1	趙雷		語文		80
1	趙雷		數學		90
1	趙雷		英語		99
2	錢電		語文		70
2	錢電		數學		60
2	錢電		英語		80
3	孫風		語文		80
3	孫風		數學		80
3	孫風		英語		80
5	周梅		語文		76
5	周梅		數學		87
7	鄭竹		數學		89
7	鄭竹		英語		98

37、查詢課程不及格的學生:

select a.stu_id,a.stu_name
from
t_stu_info a left join t_score b on a.stu_id=b.stu_id
group by a.stu_id,a.stu_name
having sum(case when b.score<60 then 1 else 0 end)>0
;

執行結果:

4	李雲
6	吳蘭

38、查詢課程編號為01且課程成績在80分以上的學生的學號和姓名:

select a.*
from
t_stu_info a join t_score b on a.stu_id=b.stu_id and b.course_id='1' and b.score>=80
;

執行結果:

1	趙雷	1990-01-01	男
3	孫風	1990-05-20	男

39、求每門課程的學生人數:

select a.course_id,b.course,count(a.course_id)
from
t_score a join t_course b on a.course_id=b.course_id
group by a.course_id,b.course
;

執行結果:

1	語文	6
2	數學	6
3	英語	6

40、查詢選修"張三"老師所授課程的學生中,成績最高的學生資訊及其成績:

select b.*,a.score
from
(
select b.stu_id,b.score,
first_value(b.score) over (partition by b.course_id order by b.score desc rows between unbounded preceding and unbounded following)
from
t_score b
join t_course c on b.course_id=c.course_id
join t_teach d on c.teach_id=d.teach_id
where d.teach_name='張三'
limit 1
) a join t_stu_info b on a.stu_id=b.stu_id
;

執行結果:

1	趙雷	1990-01-01	男	90

41、查詢不同課程成績相同的學生的學生編號、課程編號、學生成績:

select distinct a.stu_id,b.stu_id,a.course_id,b.course_id,b.score
from
t_score a join t_score b on a.score=b.score 
where a.stu_id<>b.stu_id and a.course_id<>b.course_id

執行結果:

1	2	1	3	80
1	3	1	2	80
1	3	1	3	80
2	1	3	1	80
2	3	3	1	80
2	3	3	2	80
3	1	2	1	80
3	1	3	1	80
3	2	1	3	80
3	2	2	3	80

42、查詢每門課程成績最好的前三名:

select *
from
(
select course_id,score,
row_number() over(partition by course_id order by score desc) as rn
from
t_score
) a
where a.rn<4
;

執行結果:

1	80	1
1	80	2
1	76	3
2	90	1
2	89	2
2	87	3
3	99	1
3	98	2
3	80	3

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

select course_id,
count(1) as total
from
t_score 
group by course_id
having count(1) > 5	
order by course_id asc,total desc
;

執行結果:

1	6
2	6
3	6

44、檢索至少選修兩門課程的學生學號:

select stu_id,
count(stu_id) as total
from
t_score
group by stu_id
having total>1
;

執行結果:

1	3
2	3
3	3
4	3
5	2
6	2
7	2

45、查詢選修了全部課程的學生資訊:

select stu.stu_id,stu.stu_name 
from t_stu_info stu
join t_course cs
left join t_score sc on sc.stu_id = stu.stu_id and cs.course_id = sc.course_id
group by stu.stu_id,stu.stu_name
having sum(case when sc.score is null then 1 else 0 end)=0
;

執行結果:

1	趙雷
2	錢電
3	孫風
4	李雲

46、查詢各學生的年齡(週歲):
– 按照出生日期來算,當前月日 < 出生年月的月日則,年齡減一

select a.*,
(case when month(a.birthday)>=month(current_date) and dayofmonth(a.birthday)>dayofmonth(current_date) 
then 
year(current_date)-year(a.birthday)-1
else
year(current_date)-year(a.birthday)
end)
from
t_stu_info a
;

執行結果:

1	趙雷	1990-01-01	男	28
2	錢電	1990-12-21	男	28
3	孫風	1990-05-20	男	28
4	李雲	1990-08-06	男	28
5	周梅	1991-12-01	女	27
6	吳蘭	1992-03-01	女	26
7	鄭竹	1989-07-01	女	29
8	王菊	1990-01-20	女	28
9	鄭竹	1989-07-02	男	29
10	王菊	1990-02-09	男	28.

47、查詢本週過生日的學生:

select a.*,
weekofyear(a.birthday),
weekofyear(current_date)
from
t_stu_info a
where (weekofyear(a.birthday)=weekofyear(current_date))
;

執行結果:

當前時間2018-12-27  本週沒過生日的....

48、查詢下週過生日的學生:

select a.*,
weekofyear(a.birthday),
weekofyear(current_date)
from
t_stu_info a
where 
(case when weekofyear(current_date)=52 then 1 else  weekofyear(current_date)+1 end)= weekofyear(a.birthday)
;

執行結果:

1	趙雷	1990-01-01	男	1	52

49、查詢本月過生日的學生:

select a.*,
month(a.birthday),
month(current_date)
from
t_stu_info a
where (month(a.birthday)= month(current_date))
;

執行結果:

2	錢電	1990-12-21	男	12	12
5	周梅	1991-12-01	女	12	12

50、查詢12月份過生日的學生:

select a.*,
month(a.birthday)
from
t_stu_info a
where (month(a.birthday)= 12)
;

執行結果:

2	錢電	1990-12-21	男	12	12
5	周梅	1991-12-01	女	12	12