最近兩年的工作沒有寫過多少SQL,感覺水平下降十分嚴重,網上找了50道練習題學習和復習
原文地址:50道SQL練習題及答案與詳細分析
1.0數據表介紹
--1.學生表
Student(SId,Sname,Sage,Ssex)
--SId 學生編號,Sname 學生姓名,Sage 出生年月,Ssex 學生性別
--2.課程表
Course(CId,Cname,TId)
--CId 課程編號,Cname 課程名稱,TId 教師編號
--3.教師表
Teacher(TId,Tname)
--TId 教師編號,Tname 教師姓名
--4.成績表
SC(SId,CId,score)
--SId 學生編號,CId 課程編號,score 分數
2.0 數據表創建
學生表Student
create table Student(SId varchar(10),Sname varchar(10),Sage datetime,Ssex varchar(10));
insert into Student values('01' , '趙雷' , '1990-01-01' , '男');
insert into Student values('02' , '錢電' , '1990-12-21' , '男');
insert into Student values('03' , '孫風' , '1990-12-20' , '男');
insert into Student values('04' , '李雲' , '1990-12-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吳蘭' , '1992-01-01' , '女');
insert into Student values('07' , '鄭竹' , '1989-01-01' , '女');
insert into Student values('09' , '張三' , '2017-12-20' , '女');
insert into Student values('10' , '李四' , '2017-12-25' , '女');
insert into Student values('11' , '李四' , '2012-06-06' , '女');
insert into Student values('12' , '趙六' , '2013-06-13' , '女');
insert into Student values('13' , '孫七' , '2014-06-01' , '女');
科目表Course
create table Course(CId varchar(10),Cname nvarchar(10),TId varchar(10));
insert into Course values('01' , '語文' , '02');
insert into Course values('02' , '數學' , '01');
insert into Course values('03' , '英語' , '03');
教師表Teacher
create table Teacher(TId varchar(10),Tname varchar(10));
insert into Teacher values('01' , '張三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
成績表SC
create table SC(SId varchar(10),CId 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);
3.0 練習題目
- 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.查詢下月過生日的學生
4.0答案和分析(如有更好解法,歡迎留言交流)
use DataBaseStudy
--1.查詢" 01 "課程比" 02 "課程成績高的學生的信息及課程分數
--因為需要全部的學生信息,則需要在sc表中得到符合條件的SId后與student表進行join,可以left join 也可以 right join
select * from (
select t1.sid as sid, t1.score as class1,t2.score as class2 from
(select *from sc where sc.cid='01') as t1 ,
(select * from sc where sc.cid='02') as t2
where t1.sid=t2.sid and t1.score>t2.score
) as r
left join student
on r.sid=student.sid
select * from Student RIGHT JOIN (
select t1.SId, class1, class2 from
(select SId, score as class1 from sc where sc.CId = '01')as t1,
(select SId, score as class2 from sc where sc.CId = '02')as t2
where t1.SId = t2.SId AND t1.class1 > t2.class2
)r
on Student.SId = r.SId;
--join (inner join)屬於等值連接,只返回兩個表中聯結字段相等的行,select * from t1,t2 where...這種屬於隱式表連接,逐漸被拋棄
select * from
(select SId, score as class1 from sc where sc.CId = '01')as t1
join
(select SId, score as class2 from sc where sc.CId = '02')as t2
on t1.SId=t2.SId
--1.2 查詢存在" 01 "課程但可能不存在" 02 "課程的情況(不存在時顯示為 null )
select * from
(select * from sc where sc.CId = '01') as t1
left join
(select * from sc where sc.CId = '02') as t2
on t1.SId = t2.SId
--1.3 查詢不存在" 01 "課程但存在" 02 "課程的情況
--考察子查詢
select * from sc where sc.CId = '02'
and sc.[SId] not in
(select sid from sc where sc.cid='01')
--2 查詢平均成績大於等於 60 分的同學的學生編號和學生姓名和平均成績
--考察group by 分組和常用聚合函數
select * from
(select sid,AVG(score) as AVGsource from sc
where score>60
group by sid) as t1
left join student
on t1.SId=Student.SId
--3 查詢在 SC 表存在成績的學生信息
select * from
(select SId from sc
group by SId) as t1
left join Student
on t1.SId=Student.SId
select distinct Student.* from Student
right join sc
on Student.SId=sc.SId
--下面這種寫法相當於innerjoin,屬於隱式的表連接,已經被逐漸拋棄
select DISTINCT student.*
from student,sc
where student.SId=sc.SId
--4 查詢所有同學的學生編號、學生姓名、選課總數、所有課程的總成績(沒成績的顯示為 null )
select * from
(select SId,COUNT(CId) as CCount,SUM(score)as SumScore from SC
group by SId ) as t1
right join student
on t1.SId=Student.SId
--1 查有成績的學生信息
select * from Student right join
(select Sid from sc
group by Sid) as t1
on t1.SId=Student.SId
--5 查詢「李」姓老師的數量
select count(*) from Teacher
where Tname like '李%'
--6,查詢學過「張三」老師授課的同學的信息
select count(*) from sc,Student
select count (*) from sc
inner join Student
on sc.SId=Student.SId
--7查詢沒有學全所有課程的同學的信息
select * from Student where SId not in
(
select SId from sc
group by
SId
having COUNT(CId)= (select count(CId) from Course)
)
--8 查詢至少有一門課與學號為" 01 "的同學所學相同的同學的信息
select * from Student right join
(select sid from SC
where CId in (select cid from sc where SId='01') and SId !='01'
group by sid) as t1
on Student.SId=t1.SId
--9查詢和" 01 "號的同學學習的課程 完全相同的其他同學的信息
--不會
--10查詢沒學過"張三"老師講授的任一門課程的學生姓名
select * from sc where sid=6
select * from Course where tid='01'
select * from Student where sid not in
(select sid from sc where cid in (select cid from Course where TId='01'))
select * from student
where student.sid not in(
select sc.sid from sc,course,teacher
where
sc.cid = course.cid
and course.tid = teacher.tid
and teacher.tname= '張三'
)
--11 查詢兩門及其以上不及格課程的同學的學號,姓名及其平均成績
select * from sc where score<=60
select Student.Sname,t1.SId,t1.avgScore,t1.num from Student
right join
(
select sid,count(score)as num,AVG(score)as avgScore from sc
where score<60
group by SId
having count(score)>=2) as t1
on t1.SId=Student.SId
--12 檢索" 01 "課程分數小於 60,按分數降序排列的學生信息
select * from Student right join
(
select * from sc where cid='01' and score<=60
)as t1
on Student.sid=t1.sid
order by score desc
select student.*, sc.score from student, sc
where student.sid = sc.sid
and sc.score < 60
and cid = '01'
ORDER BY sc.score DESC;
--13 按平均成績從高到低顯示所有學生的所有課程的成績以及平均成績
select * from sc
left join (
select sid,avg(score) as avscore from sc
group by sid
)r
on sc.sid = r.sid
order by avscore desc;
select sid,Count(*) from sc
group by sid
select * from Course
--下面是帶行轉列的優化版本,pivot函數用法稍微有些復雜。
select Student.Sname,t3.* from Student right join
(select sid, [01]as'語文',[02] as '數學',[03] as '英語',avgScore
from
(
select sc.*,t1.avgScore from sc right join
(
select sid,avg(score) as avgScore from sc
group by sid ) as t1
on sc.SId=t1.SId
)as t2
pivot
(
sum(score) for cid in([01],[02],[03])
)tbl) as t3
on Student.SId=t3.SId
order by avgScore desc
--14 查詢各科成績最高分、最低分和平均分:
--以如下形式顯示:課程 ID,課程 name,最高分,最低分,平均分,及格率,中等率,優良率,優秀率
--及格為>=60,中等為:70-80,優良為:80-90,優秀為:>=90
--要求輸出課程號和選修人數,查詢結果按人數降序排列,若人數相同,按課程號升序排列
--sqlserver百分比除法,全是整型結果也是整形,需要進行類型轉換
select cid
,max(score) as 最高分
,min(score) as 最低分
,avg(score) as 平均分
,count(*) as 選修人數
,sum(case when sc.score>=60 then 1 else 0 end)/cast(count(*) as float )as 及格率
from SC
group by CId
order by 選修人數 desc,CId asc
select
sc.CId ,
max(sc.score)as 最高分,
min(sc.score)as 最低分,
AVG(sc.score)as 平均分,
count(*)as 選修人數,
sum(case when sc.score>=60 then 1 else 0 end )/count(*)as 及格率,
sum(case when sc.score>=70 and sc.score<80 then 1 else 0 end )/count(*)as 中等率,
sum(case when sc.score>=80 and sc.score<90 then 1 else 0 end )/count(*)as 優良率,
sum(case when sc.score>=90 then 1 else 0 end )/count(*)as 優秀率
from sc
GROUP BY sc.CId
ORDER BY count(*)DESC, sc.CId ASC
--15 按各科成績進行排序,並顯示排名, Score 重復時保留名次空缺
-- left join ,right join,如果副表有多條數據對應主表,那查詢出來就是多條數據
select a.cid, a.sid, a.score, count(b.score)+1 as rank
from sc as a
left join sc as b
on a.score<b.score and a.cid = b.cid
group by a.cid, a.sid,a.score
order by a.cid, rank ASC;
select sc.CId,sc.SId,sc.score,count(b.score)+1 from sc
left join sc as b
on sc.score<b.score and sc.CId=b.CId
group by sc.cid,sc.sid,sc.score
order by sc.CId,score desc
--未聚合前的結果集,
select * from sc
left join sc as b
on sc.score<b.score and sc.CId=b.CId
order by sc.CId
--使用排序函數會更加簡潔
select sid,cid,score,rank() over(partition by cid order by score desc) as ranking from sc;
--16 查詢學生的總成績,並進行排名,總分重復時保留名次空缺
--考察sql四大排名函數的用法,row_number,rank,dense_rank,ntile,
with t1 as (
select sid,sum(score) as sumScore from sc
group by sid)
select dense_rank() over(order by sumScore desc)as num,* from t1
--17 統計各科成績各分數段人數:課程編號,課程名稱,[100-85],[85-70],[70-60],[60-0] 及所占百分比
select Course.Cname,t1.* from course right join
(select CId,count(score) as 課程人數
,sum(case when score>=85 then 1 else 0 end)*100/CAST( count(score) as float) as [100-85]
,sum(case when score>=70 and score<85 then 1 else 0 end)*100/CAST( count(score) as float) as [85-70]
,sum(case when score>=60 and score<70 then 1 else 0 end)*100/CAST( count(score) as float) as [70-60]
,sum(case when score<60 then 1 else 0 end)*100/CAST( count(score) as float) as [60-0]
from sc
group by CId) as t1
on Course.CId=t1.CId
--18 查詢各科成績前三名的記錄
select * from (
select sid,cid,score, RANK() over(partition by cid order by score desc)as rankNum from SC) as t1
where t1.rankNum<=3
--19查詢每門課程被選修的學生數
select cid,count(SId)as snum from sc
group by cid
--20查詢出只選修兩門課程的學生學號和姓名
select Student.Sname,t1.* from Student right join
(
select sid from sc
group by sid
having count(cid)=2) as t1
on Student.SId=t1.SId
--21查詢男生、女生人數
select Ssex,count(sid) as num from Student group by Ssex
--22查詢名字中含有「風」字的學生信息
select * from Student where Sname like '%風%'
--23 查詢同名同性學生名單,並統計同名人數
select Sname,count(*) as 人數 from Student
group by Sname
having count(*)>1
--24 查詢 1990 年出生的學生名單
--考察時間函數的熟悉程度
select * from Student where Sage>='1990-01-01' and Sage<='1990-12-31'
--25查詢每門課程的平均成績,結果按平均成績降序排列,平均成績相同時,按課程編號升序排列
select cid,avg(score)as avgScore from sc
group by cid
order by avgScore desc,CId asc
--26查詢平均成績大於等於 85 的所有學生的學號、姓名和平均成績
select Student.Sname,t1.* from Student
right join
(
select sid,avg(score)as avgScore from sc
group by sid
having avg(score)>=85) as t1
on Student.SId=t1.SId
order by avgScore desc
--27查詢課程名稱為「數學」,且分數低於 60 的學生姓名和分數
select Student.Sname,t1.score from Student right join
(
select * from sc where cid=(select cid from Course where cname='數學') and score<60) as t1
on Student.sid=t1.sid
--28查詢所有學生的課程及分數情況(存在學生沒成績,沒選課的情況)
select * from Student
left join sc
on Student.SId=sc.SId
--29查詢任何一門課程成績在 70 分以上的姓名、課程名稱和分數
select Student.Sname,Course.Cname,sc.score from Student,sc,Course
where Student.SId=sc.SId and sc.CId=Course.CId and sc.score>70
select Student.Sname,Course.Cname,sc.score from Student
join sc on Student.SId=sc.SId
join Course on sc.CId=Course.CId
where sc.score>70
--30查詢不及格的課程
select * from sc where score<60
--31查詢課程編號為 01 且課程成績在 80 分以上的學生的學號和姓名
select Student.SId,Student.Sname,sc.score from Student join sc
on Student.SId=sc.SId
where sc.CId='01' and score>80
--32求每門課程的學生人數
select cid,count(*)as num from sc
group by CId
--33成績不重復,查詢選修「張三」老師所授課程的學生中,成績最高的學生信息及其成績
select top(1)* from sc where sc.CId=
(
select cid from Course join Teacher
on Course.TId=Teacher.TId
where Teacher.Tname='李四')
order by score desc
--34 成績有重復的情況下,查詢選修「張三」老師所授課程的學生中,成績最高的學生信息及其成績
select * from (
select *,rank() over(partition by cid order by score desc) as 名次 from sc
where cid in (
select CId from Course
join Teacher
on Course.TId=Teacher.TId
where Teacher.Tname='張三'
)) as t1
where t1.名次=1
--35 查詢不同課程成績相同的學生的學生編號、課程編號、學生成績
--思路-》查詢的結果集中,不同的結果中不同字段的對比,可以用join連接,去重用group by去掉類似[1,3]和[3,1]這樣的重復數據
select t1.SId,t1.CId,t1.score,t2.score from sc as t1
join sc as t2
on t1.SId=t2.SId
and t1.CId!=t2.CId
and t1.score=t2.score
group by t1.SId,t1.CId,t1.score,t2.score
order by t1.SId
--36查詢每門功成績最好的前兩名
select cid,sid,名次 from
(select sid,cid,rank() over(partition by cid order by score desc)as 名次 from sc) as t1
where t1.名次<3
--37統計每門課程的學生選修人數(超過 5 人的課程才統計)。
select cid,count(*) as num from sc
group by cid
having count(*)>5
--38檢索至少選修兩門課程的學生學號
select sid,count(cid) as num from sc
group by sid
having count(cid)>=2
--39查詢選修了全部課程的學生信息
select Student.* from Student
right join
(
select sid,count(cid) as num from sc
group by sid
having count(cid)=3) as t1
on Student.SId=t1.SId
--40查詢各學生的年齡,只按年份來算
日期函數不太熟悉,后五題還沒做
