(若是想要測試大數據可自行添加千萬級數據方法另外一篇文章有)html
測試表格數據庫
--1.學生表
Student(S#,Sname,Sage,Ssex) post
--S# 學生編號,Sname 學生姓名,Sage 出生年月,Ssex 學生性別學習
--2.課程表
Course(C#,Cname,T#) 測試
--C# --課程編號,Cname 課程名稱,T# 教師編號大數據
--3.教師表
Teacher(T#,Tname)ui
--T# 教師編號,Tname 教師姓名spa
--4.成績表
SC(S#,C#,score)code
--S# 學生編號,C# 課程編號,score 分數htm
建立測試數據
學生表 Student
create table Student(S# varchar(10),Sname nvarchar(10),Sage datetime,Ssex nvarchar(10))
insert into Student values('01' , N'趙雷' , '1990-01-01' , N'男')
insert into Student values('02' , N'錢電' , '1990-12-21' , N'男')
insert into Student values('03' , N'孫風' , '1990-05-20' , N'男')
insert into Student values('04' , N'李雲' , '1990-08-06' , N'男')
insert into Student values('05' , N'周梅' , '1991-12-01' , N'女')
insert into Student values('06' , N'吳蘭' , '1992-03-01' , N'女')
insert into Student values('07' , N'鄭竹' , '1989-07-01' , N'女')
insert into Student values('08' , N'王菊' , '1990-01-20' , N'女')
科目表 Course
create table Course(C# varchar(10),Cname nvarchar(10),T# varchar(10))
insert into Course values('01' , N'語文' , '02')
insert into Course values('02' , N'數學' , '01')
insert into Course values('03' , N'英語' , '03')
教師表 Teacher
create table Teacher(T# varchar(10),Tname nvarchar(10))
insert into Teacher values('01' , N'張三')
insert into Teacher values('02' , N'李四')
insert into Teacher values('03' , N'王五')
成績表 SC
create table SC(S# varchar(10),C# varchar(10),score decimal(18,1))
insert into SC values('01' , '01' , 80)
insert into SC values('01' , '02' , 90)
insert into SC values('01' , '03' , 99)
insert into SC values('02' , '01' , 70)
insert into SC values('02' , '02' , 60)
insert into SC values('02' , '03' , 80)
insert into SC values('03' , '01' , 80)
insert into SC values('03' , '02' , 80)
insert into SC values('03' , '03' , 80)
insert into SC values('04' , '01' , 50)
insert into SC values('04' , '02' , 30)
insert into SC values('04' , '03' , 20)
insert into SC values('05' , '01' , 76)
insert into SC values('05' , '02' , 87)
insert into SC values('06' , '01' , 31)
insert into SC values('06' , '03' , 34)
insert into SC values('07' , '02' , 89)
insert into SC values('07' , '03' , 98)
50道練習題
1. 查詢" 01 "課程比" 02 "課程成績高的學生的信息及課程分數
1.1 查詢同時存在" 01 "課程和" 02 "課程的狀況
1.2 查詢存在" 01 "課程但可能不存在" 02 "課程的狀況(不存在時顯示爲 null )
1.3 查詢不存在" 01 "課程但存在" 02 "課程的狀況
2. 查詢平均成績大於等於 60 分的同窗的學生編號和學生姓名和平均成績
3. 查詢在 SC 表存在成績的學生信息
4. 查詢全部同窗的學生編號、學生姓名、選課總數、全部課程的總成績(沒成績的顯示爲 null )
4.1 查有成績的學生信息
5. 查詢「李」姓老師的數量
6. 查詢學過「張三」老師授課的同窗的信息
7. 查詢沒有學全全部課程的同窗的信息
8. 查詢至少有一門課與學號爲" 01 "的同窗所學相同的同窗的信息
9. 查詢和" 01 "號的同窗學習的課程徹底相同的其餘同窗的信息
10. 查詢沒學過"張三"老師講授的任一門課程的學生姓名
11. 查詢兩門及其以上不及格課程的同窗的學號,姓名及其平均成績
12. 檢索" 01 "課程分數小於 60,按分數降序排列的學生信息
13. 按平均成績從高到低顯示全部學生的全部課程的成績以及平均成績
14. 查詢各科成績最高分、最低分和平均分:
以以下形式顯示:課程 ID,課程 name,最高分,最低分,平均分,及格率,中等率,優良率,優秀率
及格爲>=60,中等爲:70-80,優良爲:80-90,優秀爲:>=90
要求輸出課程號和選修人數,查詢結果按人數降序排列,若人數相同,按課程號升序排列
15. 按各科成績進行排序,並顯示排名, Score 重複時保留名次空缺
15.1 按各科成績進行排序,並顯示排名, Score 重複時合併名次
16. 查詢學生的總成績,並進行排名,總分重複時保留名次空缺
16.1 查詢學生的總成績,並進行排名,總分重複時不保留名次空缺
17. 統計各科成績各分數段人數:課程編號,課程名稱,[100-85],[85-70],[70-60],[60-0] 及所佔百分比
18. 查詢各科成績前三名的記錄
19. 查詢每門課程被選修的學生數
20. 查詢出只選修兩門課程的學生學號和姓名
21. 查詢男生、女生人數
22. 查詢名字中含有「風」字的學生信息
23. 查詢同名同性學生名單,並統計同名人數
24. 查詢 1990 年出生的學生名單
25. 查詢每門課程的平均成績,結果按平均成績降序排列,平均成績相同時,按課程編號升序排列
26. 查詢平均成績大於等於 85 的全部學生的學號、姓名和平均成績
27. 查詢課程名稱爲「數學」,且分數低於 60 的學生姓名和分數
28. 查詢全部學生的課程及分數狀況(存在學生沒成績,沒選課的狀況)
29. 查詢任何一門課程成績在 70 分以上的姓名、課程名稱和分數
30. 查詢不及格的課程
31. 查詢課程編號爲 01 且課程成績在 80 分以上的學生的學號和姓名
32. 求每門課程的學生人數
33. 成績不重複,查詢選修「張三」老師所授課程的學生中,成績最高的學生信息及其成績
34. 成績有重複的狀況下,查詢選修「張三」老師所授課程的學生中,成績最高的學生信息及其成績
35. 查詢不一樣課程成績相同的學生的學生編號、課程編號、學生成績
36. 查詢每門功成績最好的前兩名
37. 統計每門課程的學生選修人數(超過 5 人的課程才統計)。
38. 檢索至少選修兩門課程的學生學號
39. 查詢選修了所有課程的學生信息
40. 查詢各學生的年齡,只按年份來算
41. 按照出生日期來算,當前月日 < 出生年月的月日則,年齡減一
42. 查詢本週過生日的學生
43. 查詢下週過生日的學生
44. 查詢本月過生日的學生
45. 查詢下月過生日的學生
答案參考,親測,若有更優解答,有勞各位獻上,供你們學習參考(下邊格式有問題。我就不逐一改了。你們將就看吧)
-
select A.*,B.C#,B.score from (select * from SC where C#='01')A
-
left join(select * from SC where C#='02')B
-
-
-
-
select * from (select * from SC where C#='01')A
-
left join (select * from SC where C#='02')B on A.S#=B.S#
-
-
-
select * from (select * from SC where C#='01')A
-
left join (select * from SC where C#='02')B on A.S#=B.S#
-
-
select * from SC where C#='02'and S# not in(select S# from SC where C#='01')
-
-
select A.S#,B.Sname,A.dc from(select S#,AVG(score)dc from SC group by S#)A
-
left join Student B on A.S#=B.S# where A.dc>=60
-
-
select * from Student where S# in (select distinct S# from SC)
-
-
select B.S#,B.Sname,A.選課總數,A.總成績 from
-
(
select S#,COUNT(C#)選課總數,sum(score)總成績 from SC group by S#)A
-
right join Student B on A.S#=B.S#
-
-
select A.S#,B.Sname,A.選課總數,A.總成績 from
-
(
select S#,COUNT(C#)選課總數,sum(score)總成績 from SC group by S#)A
-
left join Student B on A.S#=B.S#
-
-
-
select COUNT(*)李姓老師數量 from Teacher where Tname like '李%'
-
-
-
-
where S# in(select distinct S# from SC
-
where C#=(select C# from Course
-
where T#=(select T# from Teacher where Tname='張三')))
-
-
-
select * from Student where S# in(select S# from SC group by S# having COUNT(C#)<3)
-
-
-
-
where S# in(select distinct S# from SC where C# in(select C# from SC where S#='01')
-
-
-
-
-
where S# in(select S# from SC where C# in(select distinct C# from SC where S#='01') and S#<>'01'
-
-
-
-
-
select Sname from Student
-
where S# not in(select S# from SC
-
where C# in(select C# from Course where T# in(select T# from Teacher where Tname='張三')
-
-
-
-
select A.S#,A.Sname,B.平均成績 from Student A right join
-
(
select S#,AVG(score)平均成績 from SC where score<60 group by S# having COUNT(score)>=2)B
-
-
-
select S#,score from SC where C#='01' and score<60 order by score desc
-
-
-
select S#,max(case C# when '01' then score else 0 end)'01',
-
max(case C# when '02' then score else 0 end)'02',
-
MAX(case C# when '03' then score else 0 end)'03',AVG(score)平均分 from SC
-
group by S# order by 平均分 desc
-
-
-
select distinct A.C#,Cname,最高分,最低分,平均分,及格率,中等率,優良率,優秀率 from SC A
-
left join Course on A.C#=Course.C#
-
left join (select C#,MAX(score)最高分,MIN(score)最低分,AVG(score)平均分 from SC group by C#)B on A.C#=B.C#
-
left join (select C#,(convert(decimal(5,2),(sum(case when score>=60 then 1 else 0 end)*1.00)/COUNT(*))*100)及格率 from SC group by C#)C on A.C#=C.C#
-
left join (select C#,(convert(decimal(5,2),(sum(case when score >=70 and score<80 then 1 else 0 end)*1.00)/COUNT(*))*100)中等率 from SC group by C#)D on A.C#=D.C#
-
left join (select C#,(convert(decimal(5,2),(sum(case when score >=80 and score<90 then 1 else 0 end)*1.00)/COUNT(*))*100)優良率 from SC group by C#)E on A.C#=E.C#
-
left join (select C#,(convert(decimal(5,2),(sum(case when score >=90 then 1 else 0 end)*1.00)/COUNT(*))*100)優秀率
-
from SC group by C#)F on A.C#=F.C#
-
-
-
-
-
select *,RANK()over(order by score desc)排名 from SC
-
-
-
select *,DENSE_RANK()over(order by score desc)排名 from SC
-
-
-
select *,RANK()over(order by 總成績 desc)排名 from(
-
select S#,SUM(score)總成績 from SC group by S#)A
-
-
-
select *,dense_rank()over(order by 總成績 desc)排名 from(
-
select S#,SUM(score)總成績 from SC group by S#)A
-
-
-
select distinct A.C#,B.Cname,C.[100-85],C.所佔百分比,D.[85-70],D.所佔百分比,E.[70-60],E.所佔百分比,F.[60-0],F.所佔百分比
-
-
left join Course B ON A.C#=B.C#
-
left join (select C#,sum(case when score>85 and score<=100 then 1 else null end)[100-85],
-
convert(decimal(5,2),(sum(case when score>85 and score<100 then 1 else null end))*1.00/COUNT(*))*100 所佔百分比 from SC group by C#)C on A.C#=C.C#
-
left join (select C#,sum(case when score>70 and score<=85 then 1 else null end)[85-70],
-
convert(decimal(5,2),(sum(case when score>70 and score<=85 then 1 else null end))*1.00/COUNT(*))*100 所佔百分比 from SC group by C#)D on A.C#=D.C#
-
left join (select C#,sum(case when score>60 and score<=70 then 1 else null end)[70-60],
-
convert(decimal(5,2),(sum(case when score>60 and score<=70 then 1 else null end))*1.00/COUNT(*))*100 所佔百分比 from SC group by C#)E on A.C#=E.C#
-
left join (select C#,sum(case when score>0 and score<=60 then 1 else null end)[60-0],
-
convert(decimal(5,2),(sum(case when score>0 and score<=60 then 1 else null end))*1.00/COUNT(*))*100 所佔百分比 from SC group by C#)F on A.C#=F.C#
-
-
-
select * from(select *,rank()over (partition by C# order by score desc)A from SC)B where B.A<=3
-
-
-
select a.S#,a.C#,a.score from SC a
-
left join SC b on a.C#=b.C# and a.score<b.score
-
group by a.S#,a.C#,a.score
-
-
order by a.C#,a.score desc
-
-
-
select * from SC a where (select COUNT(*)from SC where C#=a.C# and score>a.score)<3
-
order by a.C#,a.score desc
-
-
-
select C#,COUNT(S#)學生數 from SC group by C#
-
-
-
select S#,Sname from Student
-
where S# in(select S# from(select S#,COUNT(C#)課程數 from SC group by S#)A where A.課程數=2)
-
-
-
select Ssex,COUNT(Ssex)人數 from Student group by Ssex
-
-
-
select * from Student where Sname like '%風%'
-
-
-
select A.*,B.同名人數 from Student A
-
left join (select Sname,Ssex,COUNT(*)同名人數 from Student group by Sname,Ssex)B
-
on A.Sname=B.Sname and A.Ssex=B.Ssex
-
-
-
-
select * from Student where YEAR(Sage)=1990
-
-
-
select C#,AVG(score)平均成績 from SC group by C# order by 平均成績 desc,C#
-
-
-
select A.S#,A.Sname,B.平均成績 from Student A
-
left join (select S#,AVG(score)平均成績 from SC group by S#)B on A.S#=B.S#
-
-
-
-
select B.Sname,A.score from(select * from SC where score<60 and C#=(select C# from Course where Cname='數學'))A
-
left join Student B on A.S#=B.S#
-
-
-
select A.S#,B.C#,B.score from Student A left join SC B on A.S#=B.S#
-
-
-
select A.Sname,D.Cname,D.score from
-
(
select B.*,C.Cname from(select * from SC where score>70)B left join Course C on B.C#=C.C#)D
-
left join Student A on D.S#=A.S#
-
-
-
select * from SC where score<60
-
-
-
select A.S#,B.Sname from (select * from SC where score>80 and C#=01)A
-
left join Student B on A.S#=B.S#
-
-
-
select C#,COUNT(*)學生人數 from SC group by C#
-
-
-
-
where C#=(select C# from Course where T#=(select T# from Teacher where Tname='張三'))
-
-
-
-
select *from(select *,DENSE_RANK()over (order by score desc)A
-
-
where C#=(select C# from Course where T#=(select T# from Teacher where Tname='張三')))B
-
-
-
-
select C.S#,max(C.C#)C#,max(C.score)score from SC C
-
left join(select S#,avg(score)A from SC group by S#)B
-
-
-
-
having COUNT(0)=(select COUNT(0)from SC where S#=C.S#)
-
-
-
-
(
select *,ROW_NUMBER()over(partition by C# order by score desc)A from SC)B
-
-
-
-
select C#,COUNT(S#)選修人數 from SC
-
-
-
-
-
-
-
-
-
-
-
-
-
-
having count(C#)=(select distinct COUNT(0)a from Course)
-
-
-
select S#,datediff(yy,Sage,GETDATE())年齡 from Student
-
-
-
select *,(case when convert(int,'1'+substring(CONVERT(varchar(10),Sage,112),5,8))
-
<
convert(int,'1'+substring(CONVERT(varchar(10),GETDATE(),112
-
then datediff(yy,Sage,GETDATE())
-
else datediff(yy,Sage,GETDATE())-1
-
-
-
-
-
-
select *,(case when datename(wk,convert(datetime,(convert(varchar(10),year(GETDATE()))+substring(convert(varchar(10),Sage,112),5,8))))=DATENAME(WK,GETDATE())
-
-
-
-
-
-
select *,(case when datename(wk,convert(datetime,(convert(varchar(10),year(GETDATE()))+
-
substring(convert(varchar(10),Sage,112),5,8))))=DATENAME(WK,GETDATE())+1
-
-
-
-
-
select *,(case when month(convert(datetime,(convert(varchar(10),year(GETDATE()))+substring(convert(varchar(10),Sage,112),5,8))))=month(GETDATE())
-
-
-
-
-
select *,(case when month(convert(datetime,(convert(varchar(10),year(GETDATE()))+substring(convert(varchar(10),Sage,112),5,8))))=month(GETDATE())+1
-
-
-