题目
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;
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;
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;
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;
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
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;
select count(1)
from
teacher2 a
where a.tname like "李%";
//
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就可以!!!
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
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
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;
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 ;
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;
//以下都不行
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;
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;
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的哪几门的平均成绩
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;
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;
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;
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
;
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
;
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
;
这个好像写麻烦了
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;
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;
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
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;
select
a.cname,count(b.cid) as `选课人数`
from course2 a
join sc2 b on a.cid=b.cid
group by a.cname;
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;
select
a.ssex,count(1)
from student2 a
group by a.ssex;
select
a.*
from student2 a
where a.sname like "%风%";
select
a.sname,a.ssex
from student2 a
group by a.sname,a.ssex
having count(*)>1
;
select *
from student2
where year(sbirth)=1990;
select
cid,avg(score) as av
from sc2
group by cid
order by av desc,cid;
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
;
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
;
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;
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
;
select
a.sname,b.score
from student2 a join
sc2 b on a.sid=b.sid
where b.score<60;
select
a.sid,a.sname
from student2 a join
sc2 b on a.sid=b.sid
where b.score>80 and b.cid=1;
select
a.cid,count(a.sid)
from sc2 a
group by a.cid;
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;
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
;
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;
select
a.cid,count(a.sid) as cs
from sc2 a
group by a.cid
having cs>=5
order by cs desc ,cid asc;
select
a.sid
from student2 a join sc2 b on a.sid=b.sid
group by a.sid
having count(b.cid)>2;
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
;
select
sname,
year(current_timestamp)-year(a.sbirth)-(case when date_format(current_timestamp,"mm-dd")
select
sname,sbirth
from student2 a
where weekofyear(current_timestamp)=weekofyear(a.sbirth)
;
select
sname,sbirth
from student2 a
where weekofyear(current_timestamp)+1=weekofyear(a.sbirth)
;
select
sname,sbirth
from student2 a
where month(current_timestamp)=month(a.sbirth)
;
select * from student2 where month(sbirth)=12;