数据
Student(Sid,Sname,Sage,Ssex)学生表
Sid:学号
Sname:学生姓名
Sbirth:学生生日
Ssex:学生性别
Course(Cid,Cname,T
Cid:课程编号
Cname:课程名称
Tid:教师编号
SC(Sid,Cid,score)成绩表
Sid:学号
Cid:课程编号
score:成绩
Teacher(Tid,Tname)教师表
Tid:教师编号:
Tname:教师名字
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 女
01 语文 02
02 数学 01
03 英语 03
01 张三
02 李四
03 王五
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
题目和答案
select sc1.sid,sc1.score score1,sc2.score score2
from sc sc1
join sc sc2 on sc1.sid=sc2.sid
where sc1.cid = 1 and sc2.cid = 2 and sc1.score>sc2.score
select sid,avg(score) avgscore
from sc
group by sid
having avgscore>60;
select stu.sid,stu.sname,
count(sc.cid) countcourse,
case when sum(sc.score) is null then 0 else sum(sc.score) end sumscore
from student stu
left join sc
on sc.sid=stu.sid
group by stu.sid,stu.sname;
select count(*) from teacher where tname like '李%';
select stu.sid,sname
from student stu
join course cs
join teacher t
left join sc on stu.sid=sc.sid and cs.cid=sc.cid and t.tid=cs.tid
where tname='张三'
group by stu.sid,sname
having sum(case when sc.sid is null then 0 else 1 end)=0;
select stu.sid,sname
from student stu
join course cs
join teacher t
left join sc on stu.sid=sc.sid and cs.cid=sc.cid and t.tid=cs.tid
where tname='张三'
group by stu.sid,sname
having sum(case when sc.sid is null then 1 else 0 end)=0;
select stu.sid,stu.sname
from student stu
join sc sc1 on sc1.sid=stu.sid
join sc sc2 on sc2.sid=sc1.sid
where sc1.cid=01 and sc2.cid=02;
select stu.sid,stu.sname
from student stu
join sc sc1 on sc1.sid=stu.sid and sc1.cid=01
left join sc sc2 on sc2.sid=sc1.sid and sc2.cid=02
where sc1.score>sc2.score or sc2.score is null;
select stu.sid,stu.sname
from student stu
left join sc
on sc.sid=stu.sid and sc.score>=60
group by stu.sid,stu.sname
having sum(case when sc.sid is null then 0 else 1 end)=0;
select stu.sid,stu.sname
from student stu
left join course cs
left join sc on sc.sid=stu.sid and cs.cid=sc.cid
group by stu.sid,stu.sname
having sum(case when sc.cid is null then 1 else 0 end)>0;
select distinct st.sid,st.sname
from student st
join sc sc1
on st.sid=sc1.sid
join sc sc2
on sc1.cid=sc2.cid
where sc2.sid=1;
select distinct st.sid,st.sname
from student st
join sc sc1
on st.sid=sc1.sid
join sc sc2
on sc1.cid=sc2.cid
where sc2.sid=1 and sc1.sid!=1;
select avg(score) avgscore
from sc
join course co
on sc.cid=co.cid
join teacher t
on t.tid=co.tid
where tname='张三';
select stu.sid,stu.sname
from student stu
join
(
select stu.sid as stid,sc1.sid as scid,case when stu.sid is null then sc1.sid else stu.sid end as all_id
from student stu
join (
select sc.cid
from sc
where sc.sid = 2
) aa
full outer join sc sc1 on sc1.cid = aa.cid and sc1.sid = stu.sid
) a on a.all_id = stu.sid
group by stu.sid,stu.sname
having sum(case when stid is null or scid is null then 1 else 0 end) = 0 and st.sid!=2
;
select sc.*
from sc
join course c
on sc.cid=c.cid
join teacher t
on c.tid=t.tid
where t.tname='张三';
select sc.*
from sc
left join
(select * from sc where sc.cid = '3') sc2
on sc.sid =sc2.sid
where sc2.cid is null and sc.cid=2
;
select sc.sid,
max(case course.cname when '语文' then sc.score else 0 end) yuwen,
max(case course.cname when '数学' then sc.score else 0 end) shuxue,
max(case course.cname when '英语' then sc.score else 0 end) yingyu,
count(sc.cid) kechengshu,
avg(sc.score) pingjunfen
from sc join course
on sc.cid=course.cid
group by sc.sid
order by pingjunfen;
select cid,max(score) maxscore,min(case when score is null then 0 else score end) minscore
from sc
group by cid;
select avg(score) avgscore,concat(cast(sum(case when score >= 60 then 1 else 0 end)/count(sc.sid) as string),'%') jigelv
from sc
group by cid
order by avgscore asc,jigelv desc;
select
max(case t1.cid when 1 then concat(t1.avgscore,':',jigelv) else 0 end) as yuwen,
max(case t1.cid when 2 then concat(t1.avgscore,':',jigelv) else 0 end) as shuxue,
max(case t1.cid when 3 then concat(t1.avgscore,':',jigelv) else 0 end) as yingyu
from
(select sc.cid,avg(score) avgscore,
concat(cast(sum(case when score >= 60 then 1 else 0 end)*100/count(sc.sid) as string),'%') jigelv
from sc
join course cs
on sc.cid=cs.cid
group by sc.cid,cs.cname having cs.cname='语文' or cs.cname='数学' or cs.cname='英语') t1;
select cs.tid,avg(score) avgscore
from sc
join course cs
on sc.cid=cs.cid
join teacher t
on t.tid=cs.tid
group by cs.tid,cs.cid
order by avgscore desc;
select a.*
from
(
select sc.*,
rank() over(distribute by sc.cid sort by sc.score desc) rk
from sc) a
where a.rk between 3 and 6;
select a.cid,a.cname,a.px,
count(a.px)
from
(
select cs.cid,cs.cname,
(case when score<60 then '[小于60]'
when score<70 then '[70-60]'
when score<85 then '[85-70]'
else '[100-85]' end) as px
from sc
join course cs
on sc.cid=cs.cid
) a
group by a.cid,a.cname,a.px
;
select a.*,
rank() over(distribute by 1 sort by a.avgscore) rk
from
(
select sc.sid,
avg(sc.score) avgscore
from sc
group by sc.sid
) a;
select a.*,cs.cname
from
(
select sc.*,
row_number() over(distribute by sc.cid sort by sc.score desc) rk
from sc
) a
join course cs
on cs.cid=a.cid
where a.rk<4;
select cs.cid,cs.cname,sum(case when sc.sid is null then 0 else 1 end) cd
from sc
right join course cs
on sc.cid=cs.cid
group by cs.cid,cs.cname;
select stu.sid,stu.sname
from student stu
join sc
on sc.sid=stu.sid
group by stu.sid,stu.sname
having count(stu.sid)=1;
select sum(if(ssex='男',1,0)) male,
sum(if(ssex='女',1,0)) female
from student;
select * from student
where sname like '张%';
select stu.*,
count(sid) over(distribute by stu.sname) stucount
from student stu;
select stu.*
from student stu
where substring(stu.sage,1,4)='1990';
select stu.sid,stu.sname,
avg(score) avgscore
from student stu
join sc
on sc.sid=stu.sid
group by stu.sid,stu.sname
having avgscore>80;
select sc.cid,cs.cname,avg(score) avgscore
from sc
join course cs
on cs.cid=sc.cid
group by sc.cid,cs.cname
order by avgscore asc,sc.cid desc;
select sname,score
from sc
join student stu
on stu.sid=sc.sid
join course cs
on sc.cid=cs.cid
where cs.cname='数学' and score<60;
select stu.sid,stu.sname,cs.cname
from sc
join course cs
on cs.cid=sc.cid
join student stu
on
stu.sid=sc.sid;
select sname,cname,score
from student stu
join sc
on sc.sid=stu.sid
join course cs
on cs.cid=sc.cid
where sc.score>70;
select sc.cid,cname
from sc
join course cs
on cs.cid=sc.cid
where sc.score<60
group by sc.cid,cname
order by sc.cid desc;
select stu.sid,stu.sname
from student stu
join sc
on sc.sid=stu.sid
where sc.cid=3 and score>=80;
select count(aa.sid) from
(select sid
from sc
group by sid) aa;
select
first_value(sname)
over(distribute by tname sort by score desc ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) name,
first_value(score)
over(distribute by tname sort by score desc ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) score
from sc
join student stu
on stu.sid=sc.sid
join course cs
on cs.cid=sc.cid
join teacher t
on t.tid=cs.tid
where tname='张三'
limit 1
;
select sname,score
from sc
join student stu
on stu.sid=sc.sid
join course cs
on cs.cid=sc.cid
join teacher t
on t.tid=cs.tid
where tname='张三'
order by score desc
limit 1;
select sc.cid,cs.cname,count(sc.sid) cnt
from sc
join course cs
on sc.cid=cs.cid
group by sc.cid,cs.cname;
select stu.sname,stu.sid,sc.cid,sc.score
from student stu
join sc
on sc.sid=stu.sid
join course cs
on cs.cid=sc.cid
order by sc.score
select sc.sid,sc.cid,sc.score
from sc
join
(
select sc.score,count(sc.score) cntscore
from sc
group by sc.score
) a on a.score=sc.score
join course cs
on cs.cid=sc.cid
where a.cntscore>1;
select sid,cid,score
from
(
select sc.*,
rank() over(distribute by cid sort by score) rk
from sc
) aa
where aa.rk<3;
select cid,count(sid) cntsid
from sc
group by cid having cntsid>5
order by cntsid desc,cid asc;
select sid
from sc
group by sid
having count(cid)>=2
;
select cs.cid,cs.cname
from student stu
join course cs
left join sc
on sc.cid=cs.cid and stu.sid=sc.sid
group by cs.cid,cs.cname
Having sum(case when sc.score is null then 1 else 0 end)=0;
select cid,cname,sum1 from (
select sc.cid,cs.cname,sum(case when score is null then 0 else 1 end) sum1
from student stu
join course cs
left join sc
on sc.cid=cs.cid and stu.sid=sc.sid
group by sc.cid,cs.cname
) aa
join (select count(*) c from student) bb
where sum1=bb.c;
select stu.sname
from student stu
join course cs
left join teacher t
on t.tid=cs.tid
left join sc
on sc.sid=stu.sid and cs.cid=sc.cid
where tname = '张三'
group by sname
having sum(case when score is null then 0 else 1 end)=0
;
select sid,count(cid) cd
from sc
where score<60
group by sid
having cd>=2;
select sc.sid,score
from sc
where sc.score<60 and sc.cid=2
order by score desc;
select sname,cname,score
from student stu
join sc
on sc.sid=stu.sid
join course cs
on cs.cid=sc.cid
where score>70;