HQL练习_02,经典sql50题

题目

Student(Sid,Sname,Sage,Ssex)学生表

Sid:学号

Sname:学生姓名

Sbirth:学生生日

Ssex:学生性别

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(Cid,Cname,Tid)课程表

Cid:课程编号

Cname:课程名称

Tid:教师编号

01 语文 02

02 数学 01

03 英语 03

04 hadoop 01

 

Teacher(Tid,Tname)教师表

Tid:教师编号:

Tname:教师名字

01 张三

02 李四

03 王五

SC(Sid,Cid,score)成绩表

Sid:学号Cid:课程编号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
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
01    04    50
07    04    60

建表:

建表:
CREATE TABLE IF NOT EXISTS `myhive2.student2` (
sid int,
sname string,
sbirth string,
ssex string
) 
row format delimited 
fields terminated by '\t'
;

load data local inpath "/zgm/student2.txt" into table myhive2.student2;


CREATE TABLE IF NOT EXISTS myhive2.course2 (
cid int,
cname string,
tid int
) 
row format delimited 
fields terminated by '\t'
;

load data local inpath "/zgm/course2.txt" into table myhive2.course2;


CREATE TABLE IF NOT EXISTS myhive2.teacher2 (
tid int,
tname string
) 
row format delimited 
fields terminated by '\t'
;

load data local inpath "/zgm/teacher2.txt" into table myhive2.teacher2;



CREATE TABLE IF NOT EXISTS myhive2.sc2 (
sid int,
cid int,
score int
) 
row format delimited 
fields terminated by '\t'
;

load data local inpath "/zgm/sc2.txt" into table myhive2.sc2;

1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数:

select c.*,a.score as `01课程成绩`, b.score as `02课程成绩`
from student2 c
join sc2 a on c.sid=a.sid and a.cid=1
left join sc2 b on a.sid=b.sid and b.cid=2 
where a.score>b.score or b.sid is null;


2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数:

select c.*,b.score as `01课程成绩`, a.score as `02课程成绩`
from student2 c
join sc2 a on c.sid=a.sid and a.cid=2
left join sc2 b on a.sid=b.sid and b.cid=1 
where a.score>b.score or b.sid is null;

 

3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩:

select a.sid,a.sname,avg(b.score) as `平均成绩`
from student2 a
join sc2 b on a.sid=b.sid 
group by a.sid,a.sname
having `平均成绩`>=60;





4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩:
(包括有成绩的和无成绩的)

select a.sid,a.sname,avg(nvl(b.score,0)) as `平均成绩`   //或者nvl(avg(b,score),0) as `平均成绩`
from student2 a
left join sc2 b on a.sid=b.sid 
group by a.sid,a.sname
having `平均成绩`<=60;

结果:
a.sid   a.sname 平均成绩
4       李云    33.333333333333336
6       吴兰    32.5
8       王菊    0.0

-----------------------------------------------------
第二种不符合题目要求
select a.sid,a.sname,avg(b.score) as `平均成绩`
from student2 a
left join sc2 b on a.sid=b.sid 
group by a.sid,a.sname
having `平均成绩`<=60;

结果:
a.sid   a.sname 平均成绩
4       李云    33.333333333333336
6       吴兰    32.5


5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩:

select a.sid,a.sname,count(b.cid),sum(nvl(score,0))
from student2 a
left join sc2 b on a.sid=b.sid
group by a.sid,a.sname;

 

6、查询"李"姓老师的数量:

select count(1)
from 
teacher2 a
where a.tname like "李%";

7、查询学过"张三"老师授课的同学的信息:

//
select  d.*
from student2 d
where d.sid in(
select c.sid from
 sc2 c  
join course2 a on c.cid=a.cid
join teacher2 b on a.tid=b.tid
where b.tname='张三');

3个job
73.366 seconds

//
select  d.*
from student2 d
left join sc2 c on c.sid=d.sid 
join course2 a on c.cid=a.cid
join teacher2 b on a.tid=b.tid
where b.tname='张三' 
group by d.sid,d.sname,d.sbirth,d.ssex;

1个job,59.455 seconds

//
select  distinct d.*
from student2 d
left join sc2 c on c.sid=d.sid 
join course2 a on c.cid=a.cid
join teacher2 b on a.tid=b.tid
where b.tname='张三' 
;
1个job 54.246 seconds,

更正:不用left join 直接join就可以!!!


8、查询没学过"张三"老师授课的同学的信息:

 

 

select d.*
from student2 d
left join
(select c.sid
from sc2 c
join course2 a on c.cid=a.cid
join teacher2 b on a.tid=b.tid
where b.tname='张三' 
)t
on d.sid=t.sid
where t.sid is null;

3job  83.587 seconds, 

---------------------------------------------------------------------------------------
select d.sid,d.sname,d.sbirth,d.ssex
from student2 d
join teacher2 b on  b.tname='张三'   
join course2 a on b.tid=a.tid
left join sc2 c on d.sid=c.sid and a.cid=c.cid
group by d.sid,d.sname,d.sbirth,d.ssex
having sum(case when c.score is null then 0 else 1 end)=0;

1个job  55.531 seconds



join 后的情况:

d.sid   d.sname d.sbirth        d.ssex  b.tid   b.tname a.cname c.score
1       赵雷    1990-01-01      男      1       张三    数学    90
1       赵雷    1990-01-01      男      1       张三    hadoop  90
1       赵雷    1990-01-01      男      1       张三    hadoop  50
2       钱电    1990-12-21      男      1       张三    数学    60
2       钱电    1990-12-21      男      1       张三    hadoop  NULL
3       孙风    1990-05-20      男      1       张三    数学    80
3       孙风    1990-05-20      男      1       张三    hadoop  90
4       李云    1990-08-06      男      1       张三    数学    30
4       李云    1990-08-06      男      1       张三    hadoop  NULL
5       周梅    1991-12-01      女      1       张三    数学    87
5       周梅    1991-12-01      女      1       张三    hadoop  NULL
6       吴兰    1992-03-01      女      1       张三    数学    NULL
6       吴兰    1992-03-01      女      1       张三    hadoop  NULL
7       郑竹    1989-07-01      女      1       张三    数学    89
7       郑竹    1989-07-01      女      1       张三    hadoop  60
8       王菊    1990-01-20      女      1       张三    数学    NULL
8       王菊    1990-01-20      女      1       张三    hadoop  NULL

----------------------------------------------------------------------------------
select d.*
from student2 d
where d.sid not in 
(select c.sid
from sc2 c
join course2 a on c.cid=a.cid
join teacher2 b on a.tid=b.tid
where b.tname='张三' 
);
6个job

9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息:

select a.*
from student2 a 
 join sc2 b on a.sid=b.sid and b.cid=1
join sc2 c on a.sid=c.sid and c.cid=2;

1个job

10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息:

select a.*
from student2 a
join sc2 b on a.sid=b.sid and b.cid=1
left join sc2 c on a.sid=c.sid and c.cid=2
where c.sid is null;

 

11、查询没有学全所有课程的同学的信息:
–先查询出课程的总数量–再查询所需结果


select distinct a.*
from student2 a
left join course2 b
left join sc2 c on c.sid=a.sid and c.cid=b.cid
where c.sid is null ;

 

 

12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息:

select a.sid
from student2 a
left join sc2 b on b.sid=1
join sc2 c on a.sid=c.sid and b.cid=c.cid
where a.sid<>1
group by a.sid; 


13、查询和"01"号的同学学习的课程完全相同的其他同学的信息

//以下都不行
select  a.sid,a.sname,a.sbirth,a.ssex
from student2 a
join sc2 b on a.sid=b.sid and a.sid <> 4
left join sc2 c on c.sid=4 and c.cid=b.cid
group by a.sid,a.sname,a.sbirth,a.ssex
having sum(case when c.sid is null then 1 else 0 end) <=0
;

select  a.sid,a.sname,b.cid,c.sid,c.cid
from student2 a
join sc2 b on a.sid=b.sid and a.sid <> 4
left join sc2 c on c.sid=4 and c.cid=b.cid;
group by a.sid,a.sname,a.sbirth,a.ssex
having sum(case when c.sid is null then 1 else 0 end) <=0
;


select a.sid,a.sname,b.sid,b.cid,c.sid,c.cid
from student2 a
join sc2 b on b.sid=4
right join sc2 c on c.sid=a.sid and c.cid=b.cid;
group by a.sid,a.sname,a.sbirth,a.ssex
having sum(case when c.sid is null then 1 else 0 end) <=0;  


select *
from student2 a
left join sc2 b on b.sid=5
full outer join sc2 c on a.sid=c.sid and b.cid=c.cid;
where a.sid<>1
group by a.sid; 

14、查询没学过"张三"老师讲授的任一门课程的学生姓名:


select a.sid,a.sname
from
student2 a
join teacher2 b on b.tname="李四"
join course2 c on c.tid=b.tid
left join sc2 d on d.sid=a.sid and d.cid=c.cid
group by a.sid,a.sname
having sum(if(d.score is not null,1,0))=0;

15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩:

select a.sid,a.sname,avg(b.score) 
from
student2 a 
left join sc2 b on a.sid=b.sid
where b.score<40
group by a.sid,a.sname
having count(*)>=2; 


//不过
这样求得平均成绩是小于60的哪几门的平均成绩


16、检索"01"课程分数小于60,按分数降序排列的学生信息:

select a.*,b.score
from student2 a
join sc2 b on a.sid=b.sid and b.cid=1
where b.score <60
order by b.score desc;
 


17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩:

select a.*,avg(score) as av,
sum(if(b.cid=1,b.score,0)) as `01成绩`,
sum(if(b.cid=2,b.score,0)) as `02成绩`,
sum(if(b.cid=3,b.score,0)) as `03成绩`,
sum(if(b.cid=4,b.score,0)) as `04成绩`
from student2 a
left join sc2 b on a.sid=b.sid 
group by a.sid,a.sname,a.sbirth,a.ssex
order by av desc;
 


******18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率:

select 
a.cid,a.cname,
max(b.score) as `最高分`,
min(b.score) as `最低分`,
avg(b.score) as `平均分`,
round(sum(case when b.score >=60 then 1 else 0 end)/count(*),2) as `及格率`,
round(sum(case when b.score >60 and b.score <=60 then 1 else 0 end)/count(*),2) as `中等率`,
round(sum(case when b.score >80 and b.score <=90 then 1 else 0 end)/count(*),2) as `优良率`,
round(sum(case when b.score >90 then 1 else 0 end)/count(*),2) as `优秀率`
from course2 a
join sc2 b on a.cid=b.cid
group by a.cid,a.cname;


19、按各科成绩进行排序,并显示排名:– row_number() over()分组排序功能

select 
b.cid,a.sname,b.score,
row_number() over(partition by b.cid order by b.score desc) as `排名`
from student2 a 
join sc2 b on a.sid=b.sid
;


20、查询学生的总成绩并进行排名:
 

select 
a.sid,a.sname,sum(b.score) as ss
from student2 a 
join sc2 b on a.sid=b.sid
group by a.sid,a.sname
order by ss desc
;


21、查询不同老师所教不同课程平均分从高到低显示:

select
t.tname,t.cname,av,
row_number() over(partition by t.tname order by av desc) as rn 
from(
select 
a.tname,b.cname,avg(c.score) as av
from teacher2 a
join course2 b on a.tid=b.tid 
join sc2 c on  b.cid=c.cid
group by a.tname,b.cname
) t
;

这个好像写麻烦了

22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩:

select 
t.*
from
(
select 
a.*,b.score,
row_number() over(partition by b.cid order by b.score desc) as rn
from student2 a 
join sc2  b on a.sid=b.sid
) t
where t.rn=2 or t.rn=3;



23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],

select
a.cname,
sum(case when b.score>85 and  b.score<=100  then 1 else 0 end) as `100-85`,
sum(case when b.score>70 and  b.score<=85  then 1 else 0 end) as `85-70`,
sum(case when b.score>60 and  b.score<=70  then 1 else 0 end) as `70-60`
from course2 a
join sc2 b on a.cid=b.cid
group by a.cname;


24、查询学生平均成绩及其名次:

select 
sname,av,
row_number() over(order by av desc) 
from 
(select 
a.sname,avg(b.score) as av
from student2 a 
join sc2 b on a.sid=b.sid
group by a.sname)t
;



优化:

select
a.sid,b.sname,avg(a.score) as av,
row_number() over(order by avg(a.score) desc) as rn
from sc2 a
join student2 b on a.sid=b.sid
group by a.sid,b.sname;


原来2个job,150s,现在两个job,106s


25、查询各科成绩前三名的记录三个语句
 

select 
t.*
from
(
select 
b.cid,a.sname,b.score,
row_number() over(partition by b.cid order by b.score desc) as rn
from student2 a 
join sc2  b on a.sid=b.sid
) t
where t.rn<4;

26、查询每门课程被选修的学生数:

select
a.cname,count(b.cid) as `选课人数`
from course2 a 
join sc2 b on a.cid=b.cid
group by a.cname;


27、查询出只有两门课程的全部学生的学号和姓名:

select
c.sname,count(1) as `选修门数`
from course2 a 
join sc2 b on a.cid=b.cid
join student2 c on b.sid=c.sid
group by c.sname
having `选修门数`=2;

 

28、查询男生、女生人数:
 

select 
a.ssex,count(1)
from student2 a
group by a.ssex;

29、查询名字中含有"风"字的学生信息:

select 
a.*
from student2 a
where a.sname like "%风%";

30、查询同名同性学生名单,并统计同名人数:

select 
a.sname,a.ssex
from student2 a
group by a.sname,a.ssex
having count(*)>1
;

 

31、查询1990年出生的学生名单:

select *
from student2 
where year(sbirth)=1990;

32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列:
 

select 
cid,avg(score) as av
from sc2
group by cid
order by av desc,cid;


33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩:
 

select 
a.sid,a.sname,avg(b.score) as av
from student2 a
join sc2 b on a.sid=b.sid
group by a.sid,a.sname
having av>=85
;

34、查询课程名称为"数学",且分数低于60的学生姓名和分数:

select 
c.sname,b.score
from course2 a
join sc2 b on a.cid=b.cid and a.cname='数学' and b.score<60
join student2 c on b.sid=c.sid
; 

 

35、查询所有学生的课程及分数情况:

select 
b.sname,
sum(if(a.cid=1,a.score,0)) as `1号课程成绩`,
sum(if(a.cid=2,a.score,0)) as `2号课程成绩`,
sum(if(a.cid=3,a.score,0)) as `3号课程成绩`,
sum(if(a.cid=4,a.score,0)) as `4号课程成绩`
from sc2 a right join student2 b on a.sid=b.sid
group by b.sname;


36、查询任何一门课程成绩在70分以上的学生姓名、课程名称和分数:
 

select
a.sid,a.sname,c.cname,b.score
from 
(select
t1.sid ,t2.sname
from sc2 t1 join student2 t2 on t1.sid=t2.sid
group by t1.sid,t2.sname
having min(t1.score)>70) a
join sc2 b on a.sid=b.sid
join course2 c on b.cid=c.cid
;

37、查询课程不及格的学生:

select 
a.sname,b.score
from student2 a join
sc2 b on a.sid=b.sid
where b.score<60;

38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名:
 

select 
a.sid,a.sname
from student2 a join
sc2 b on a.sid=b.sid
where b.score>80 and b.cid=1;

39、求每门课程的学生人数 
 

select
a.cid,count(a.sid) 
from sc2 a 
group by a.cid;

40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩:

select 
a.sname,b.score
from student2 a join sc2 b on a.sid=b.sid
join course2 c on b.cid=c.cid
join teacher2 d on c.tid=d.tid and d.tname="张三"
order by b.score desc 
limit 1;

41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩:
 

select 
distinct a.sid,a.cid,a.score,b.sid,b.cid,a.score
from  sc2 a left join sc2 b 
on a.score=b.score
where a.cid<>b.cid and a.sid <>b.sid
;

42、查询每门课程成绩最好的前三名:

 

select
t.sname,t.rn
from(
select 
a.sname,
row_number() over(partition by b.cid order by b.score desc) as rn
from student2 a join sc2 b on a.sid=b.sid
)t
where t.rn<=3;


43、统计每门课程的学生选修人数(超过5人的课程才统计):–

要求输出课程号和选修人数,查询结果按人数降序排列,
若人数相同,按课程号升序排列

select 
a.cid,count(a.sid) as cs
from sc2 a 
group by a.cid
having cs>=5
order by cs desc ,cid asc;


44、检索至少选修两门课程的学生学号:

select 
a.sid
from student2 a join sc2 b on a.sid=b.sid
group by a.sid
having count(b.cid)>2;


45、查询选修了全部课程的学生信息:

 

select c.*
from student2 c
left semi join 
(
select 
a.sid
from sc2 a,(select count(*) as co from course2 ) b
group by a.sid,b.co
having count(*)=b.co
)t
on c.sid=t.sid
;

46、查询各学生的年龄(周岁):
– 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一

select
sname,
year(current_timestamp)-year(a.sbirth)-(case when date_format(current_timestamp,"mm-dd")<date_format(sbirth,"mm-dd") then 1 else 0 end)
from student2 a 
;

47、查询本周过生日的学生:
 

select
sname,sbirth
from student2 a 
where weekofyear(current_timestamp)=weekofyear(a.sbirth)
;

48、查询下周过生日的学生:

select
sname,sbirth
from student2 a 
where weekofyear(current_timestamp)+1=weekofyear(a.sbirth)
;

 

 49、查询本月过生日的学生:

select
sname,sbirth
from student2 a 
where month(current_timestamp)=month(a.sbirth)
;


 50、查询12月份过生日的学生:
 

select * from student2 where month(sbirth)=12;

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值