正文共:4832 字 22 图 预计阅读时间:13 分钟
本文目录:
1-- 此题只使用Score单表也可以
2select
3 c.cname,
4 count(s.sid) as '选课人数'
5from Score s, Course c
6where s.cid = c.cid
7group by c.cname
sql50_26
1-- 此题可以在第三题基础上增加限制
2-- 没有这样的学生。
3SELECT a.sid,a.sname,
4count(b.cid) as '选课数'
5FROM Student a
6left join Score b
7on a.sid = b.sid
8group by a.sid,a.sname
9having count(b.cid) = 1
1SELECT
2 ssex,
3 count(sid) as '人数'
4FROM Student
5GROUP BY ssex
1SELECT
2 sid,
3 sname,
4 sage,
5 ssex
6FROM Student
7WHERE sname like N'%风%' --编码原因加了N,视实际情况而定
sql50_29
1-- 根据姓名和性别分组即可
2SELECT
3 sname,
4 ssex,
5 count(sid)
6FROM Student
7GROUP BY sname,ssex
sql50_30
1SELECT
2 *
3FROM Student
4WHERE year(sage) = 1990
sql50_31
1-- 同第十九题
2select
3 s.cid,
4 c.cname,
5 AVG(s.score) as mean_score
6from Score s, Course c
7where s.cid = c.cid
8group by s.cid,c.cname
9order by AVG(s.score) asc, s.cid desc
sql50_32
1select
2 sc.cid,
3 s.sname,
4 c.cname,
5 sc.score
6from Score sc, Course c, Student s
7where sc.cid = c.cid
8and sc.sid = s.sid
9and sc.score < 60
10order by sc.cid desc
sql50_33
1select
2 s.sid,
3 s.sname,
4 sc.score
5from Score sc, Course c, Student s
6where sc.cid = c.cid
7and sc.sid = s.sid
8and sc.cid = '01'
9and sc.score > 60
sql50_34
1-- 查看每个人的年龄,性别,三门课成绩
2-- 就是在开头使用的用于便捷判断结果的 all_info
3-- 利用了pivot来行转列
4select
5sid,sname,sage,ssex,[语文],[数学],[英语]
6from
7(
8select a.sid,a.sname,a.sage,a.ssex,c.cname,b.score
9 from Student a
10 left join Score b
11 on a.sid=b.sid
12 left join Course c
13 on b.cid = c.cid
14) source_table
15pivot(
16 sum(score) for
17cname in (
18 [语文],[数学],[英语]
19)
20 ) t
sql50_35
1select
2 s.sname,
3 c.cname,
4 sc.score
5from Score sc, Course c, Student s
6where sc.cid = c.cid
7and sc.sid = s.sid
8and sc.score > 70
sql50_36
1select
2 s.sname,
3 sc.score
4from Score sc, Course c, Student s
5where sc.cid = c.cid
6and sc.sid = s.sid
7and sc.score < 60
8and c.cname = N'数学'
sql50_37
1-- 和第三十四题是一样的,混进来的题目?
2select
3 s.sid,
4 s.sname,
5 sc.score
6from Score sc, Course c, Student s
7where sc.cid = c.cid
8and sc.sid = s.sid
9and sc.cid = '03'
10and sc.score > 80
sql50_38
1-- 混进来的题目?
2select
3 cid,
4 count(sid)
5from Score
6group by cid
1-- 利用 top
2select
3 top 1 s.sid, s.sname, sc.score
4from Score sc, Course c, Teacher t, Student s
5where sc.cid = c.cid
6and c.tid=t.tid
7and sc.sid = s.sid
8and t.tname=N'张三'
sql50_40
1-- 同表级联查询
2select
3 distinct
4 s1.sid,
5 s1.cid,
6 s1.score
7from Score s1, Score s2
8where s1.sid = s2.sid
9and s1.score = s2.score
10and s1.cid != s2.cid
sql50_41
1-- 同第二十二题和第二十五题
2--
3-- row_number() over(partition by 分组字段 order by 排序字段 排序方式) as 别名
4select * from (
5select
6 sc.sid,
7 s.sname,
8 s.ssex,
9 s.sage,
10 c.cname,
11 sc.score,
12 ROW_NUMBER() over(partition BY sc.cid order by score desc) as myrank
13from Score sc,Student s,Course c
14where sc.sid = s.sid
15and sc.cid = c.cid) t
16where t.myrank <3
sql50_42
1select
2 cid,
3 count(sid) as '选修人数'
4from Score
5group by cid
6having count(sid) > 5
7order by count(sid) desc, cid asc
sql50_43
1select
2 sid,
3 count(cid) as '选修课程数'
4from Score
5group by sid
6having count(cid) >= 2
sql50_44
1-- 同第十题(条件相反)
2SELECT a.sid,a.sname,
3count(b.cid) as '选课数'
4FROM Student a
5left join Score b
6on a.sid = b.sid
7group by a.sid,a.sname
8having count(b.cid) = (select count(distinct cid) from Course)
9order by a.sid
sql50_45
1-- 利用SYSDATETIME()/getdate() 获取当前时间
2SELECT SYSDATETIME();
3SELECT
4 sid,
5 sname,
6 year(SYSDATETIME()) - year(sage) AS '年龄'
7FROM Student
sql50_46
1select getdate();
2select DATEADD(wk, DATEDIFF(wk,0,getdate()), 0); -- 本周周一
3select DATEADD(wk, DATEDIFF(wk,0,getdate()), 7) ; -- 下周周一
4SELECT
5 *
6FROM Student
7where DATEADD(year, year(getdate())-year(sage), sage) between
8DATEADD(wk, DATEDIFF(wk,0,getdate()), 0)
9and DATEADD(wk, DATEDIFF(wk,0,getdate()), 7)
sql50_47
1-- 同第四十七题
2select getdate();
3select DATEADD(wk, DATEDIFF(wk,0,getdate()), 0); -- 本周周一
4select DATEADD(wk, DATEDIFF(wk,0,getdate()), 7) ; -- 下周周一
5SELECT
6 *
7FROM Student
8where DATEADD(year, year(getdate())-year(sage), sage) between
9DATEADD(wk, DATEDIFF(wk,0,getdate()), 7)
10and DATEADD(wk, DATEDIFF(wk,0,getdate()), 14)
1-- 利用getdate() 获取当前时间, month()获得月份
2SELECT getdate();
3select
4 sid,
5 sname,
6 sage,
7 ssex
8from Student
9where month(sage) = month(getdate())
sql50_49
1-- 同第四十九题
2SELECT getdate();
3select
4 sid,
5 sname,
6 sage,
7 ssex
8from Student
9where month(sage) = month(getdate())+1
本文项目地址:
https://github.com/firewang/sql50
(喜欢的话,Star一下)
阅读原文,或者访问该链接可以在线观看(该系列将更新至GitHub,并且托管到read the docs)
https://sql50.readthedocs.io/zh_CN/latest/
参考网址: