數據庫基礎,你必知必會的SQL語句練習40題-Part 1

1.1 本題目的表結構

Student(S#,Sname,Sage,Ssex) 學生表

Course(C#,Cname,T#) 課程表

SC(S#,C#,score) 成績表

Teacher(T#,Tname) 教師表

1.2 本題目的建表及測試數據

(1)建表:

 1 CREATE TABLE Student 
2 (
3 S# INT,
4 Sname nvarchar(32),
5 Sage INT,
6 Ssex nvarchar(8)
7 )
8
9 CREATE TABLE Course
10 (
11 C# INT,
12 Cname nvarchar(32),
13 T# INT
14 )
15
16 CREATE TABLE Sc
17 (
18 S# INT,
19 C# INT,
20 score INT
21 )
22
23 CREATE TABLE Teacher
24 (
25 T# INT,
26 Tname nvarchar(16)
27 )

注意:建表語句暫未考慮主鍵以及外鍵的問題;

(2)測試數據:

 1 insert into Student select 1,N'劉一',18,N'男' union all
2 select 2,N'錢二',19,N'女' union all
3 select 3,N'張三',17,N'男' union all
4 select 4,N'李四',18,N'女' union all
5 select 5,N'王五',17,N'男' union all
6 select 6,N'趙六',19,N'女'
7
8 insert into Teacher select 1,N'葉平' union all
9 select 2,N'賀高' union all
10 select 3,N'楊豔' union all
11 select 4,N'周磊'
12
13 insert into Course select 1,N'語文',1 union all
14 select 2,N'數學',2 union all
15 select 3,N'英語',3 union all
16 select 4,N'物理',4
17
18 insert into SC
19 select 1,1,56 union all
20 select 1,2,78 union all
21 select 1,3,67 union all
22 select 1,4,58 union all
23 select 2,1,79 union all
24 select 2,2,81 union all
25 select 2,3,92 union all
26 select 2,4,68 union all
27 select 3,1,91 union all
28 select 3,2,47 union all
29 select 3,3,88 union all
30 select 3,4,56 union all
31 select 4,2,88 union all
32 select 4,3,90 union all
33 select 4,4,93 union all
34 select 5,1,46 union all
35 select 5,3,78 union all
36 select 5,4,53 union all
37 select 6,1,35 union all
38 select 6,2,68 union all
39 select 6,4,71

1.3 開始實戰吧小宇宙

(1)查詢“001”課程比“002”課程成績高的所有學生的學號;

1 select a.S# from
2 (select S#,Score from SC where C#='001') a,
3 (select S#,Score from SC where C#='002') b
4 where a.S#=b.S# and a.Score>b.Score


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(2) 查詢平均成績大於60分的同學的學號和平均成績;

1 select S#,AVG(Score) as AvgScore 
2 from SC
3 group by S#
4 having AVG(Score)>60


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(3)查詢所有同學的學號、姓名、選課數、總成績;

1 select s.S#,s.Sname,COUNT(sc.C#) as CourseCount,SUM(sc.Score) as ScoreSum
2 from Student s left outer join SC sc
3 on s.S# = sc.S#
4 group by s.S#,s.Sname
5 order by s.S#


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(4)查詢姓“李”的老師的個數;

1 select COUNT(distinct Tname) as count
2 from Teacher
3 where Tname like '李%'


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(5)查詢沒學過“葉平”老師課的同學的學號、姓名;

1 select s.S#,s.Sname
2 from Student s
3 where s.S# not in
4 (
5 select distinct(sc.S#) from SC sc,Course c,Teacher t
6 where sc.C#=c.C# and c.T#=t.T# and t.Tname='葉平'
7 )


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(6)查詢學過“001”並且也學過編號“002”課程的同學的學號、姓名;

 1 --解法一:求交集
2 select s.S#,s.Sname
3 from Student s,SC sc
4 where s.S#=sc.S# and sc.C#='001'
5 intersect
6 select s.S#,s.Sname
7 from Student s,SC sc
8 where s.S#=sc.S# and sc.C#='002'
9 --解法二:使用exists
10 select s.S#,s.Sname
11 from Student s,SC sc
12 where s.S#=sc.S# and sc.C#='001' and exists
13 (
14 select * from SC sc2 where sc.S#=sc2.S# and sc2.C#='002'
15 )


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


PS:EXISTS用於檢查子查詢是否至少會返回一行數據,該子查詢實際上並不返回任何數據,而是返回值True或False。那麼,這裡我們來看一下in和exists的區別:①in 是把外表和內表作hash 連接,而exists是對外表作loop循環,每次loop循環再對內表進行查詢。②一直以來認為exists比in效率高的說法是不準確的。 -->如果查詢的兩個表大小相當,那麼用in和exists差別不大。 -->如果兩個表中一個較小,一個是大表,則子查詢表大的用exists,子查詢表小的用in。

(7)查詢學過“葉平”老師所教的所有課的同學的學號、姓名;

 1 select s.S#,s.Sname 
2 from Student s
3 where s.S# in
4 (
5 select sc.S#
6 from SC sc,Course c,Teacher t
7 where c.C#=sc.C# and c.T#=t.T# and t.Tname='葉平'
8 group by sc.S#
9 having COUNT(sc.C#)=
10 (
11 select COUNT(c1.C#)
12 from Course c1,Teacher t1
13 where c1.T#=t1.T# and t1.Tname='葉平'
14 )
15 )

①測試數據教師表中,葉平老師只有一門課

數據庫基礎,你必知必會的SQL語句練習40題-Part 1


②修改測試數據教師表,將T#=2的TName也改為葉平,葉平就有兩門主講課程了

數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(8)查詢課程編號“002”的成績比課程編號“001”課程低的所有同學的學號、姓名;

1 select s.S#,s.Sname 
2 from Student s,
3 (select sc1.S#,sc1.Score from SC sc1 where sc1.C#='002') a,
4 (select sc2.S#,sc2.Score from SC sc2 where sc2.C#='001') b
5 where s.S#=a.S# and s.S#=b.S# and a.S#=b.S# and a.Score


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(9)查詢有課程成績小於60分的同學的學號、姓名;

1 select s.S#,s.Sname 
2 from Student s
3 where s.S# in
4 (
5 select distinct(sc.S#) from SC sc
6 where s.S#=sc.S# and sc.Score<60
7 )


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(10)查詢沒有學全所有課的同學的學號、姓名;

 1 select s.S#,s.Sname
2 from Student s
3 where s.S# not in
4 (
5 select sc.S# from SC sc
6 group by sc.S#
7 having COUNT(distinct sc.C#)=
8 (
9 select COUNT(distinct c.C#) from Course c
10 )
11 )


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(11)查詢至少有一門課與學號為“001”的同學所學相同的同學的學號和姓名;

1 select distinct(s.S#),s.Sname
2 from Student s,SC sc
3 where s.S#=sc.S# and sc.C# in
4 (
5 select distinct(sc2.C#) from SC sc2
6 where sc2.S#='001'
7 )
8 order by s.S# asc


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(12)查詢至少學過學號為“001”同學所有一門課的其他同學學號和姓名;(感覺跟11題有重疊)

1 select distinct(s.S#),s.Sname 
2 from Student s,SC sc
3 where s.S#=sc.S# and s.S#!='001' and sc.C# in
4 (
5 select distinct(sc2.C#) from SC sc2
6 where sc2.S#='001'
7 )
8 order by s.S# asc


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(13)把“SC”表中“葉平”老師教的課的成績都更改為此課程的平均成績;

 1 update SC set Score=
2 (
3 select AVG(score) from SC sc,Course c,Teacher t
4 where sc.C#=c.C# and c.T#=t.T# and t.Tname='葉平'
5 )
6 where C# in
7 (
8 select distinct(sc.C#) from SC sc,Course c,Teacher t
9 where sc.C#=c.C# and c.T#=t.T# and t.Tname='葉平'
10 )


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(14)查詢和“002”號的同學學習的課程完全相同的其他同學學號和姓名;

select s.S#,s.Sname 
from Student s
where s.S#!='002' and s.S# in
(
select distinct(S#) from SC
where C# in (select C# from SC where S#='002')
group by S#
having COUNT(distinct C#)=
(
select COUNT(distinct C#) from SC
where S#='002'
)
)


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(15)刪除學習“葉平”老師課的SC表記錄;

delete from SC where C# in
(
select c.C# from Course c,Teacher t
where c.T#=t.T# and t.Tname='葉平'
)


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(16)向SC表中插入一些記錄,這些記錄要求符合以下條件:①沒有上過編號“002”課程的同學學號;②插入“002”號課程的平均成績;

1 insert into SC
2 select s.S#,'002',(select AVG(score) from SC where C#='002')
3 from Student s
4 where s.S# not in (select distinct(S#) from SC where C#='002')


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(17)按平均成績從低到高顯示所有學生的“語文”、“數學”、“英語”三門的課程成績,按如下形式顯示: 學生ID,語文,數學,英語,有效課程數,有效平均分;

1 select t.S# as '學生ID',
2 (select Score from SC where S#=t.S# and C#='002') as '語文',
3 (select Score from SC where S#=t.S# and C#='003') as '數學',
4 (select Score from SC where S#=t.S# and C#='004') as '英語',
5 COUNT(t.C#) as '有效課程數',
6 AVG(t.Score) as '有效平均分'
7 from SC t
8 group by t.S#
9 order by AVG(t.Score)


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(18)查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分;

1 select sc.C# as '課程ID',MAX(Score) as '最高分',MIN(Score) as '最低分' 
2 from SC sc
3 group by sc.C#


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(19)按各科平均成績從低到高和及格率的百分數從高到低順序;

1 select sc.C#,c.Cname,ISNULL(AVG(sc.Score),0) as 'AvgScore',
2 100 * SUM(CASE WHEN ISNULL(sc.Score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) as 'Percent(%)'
3 from SC sc,Course c
4 where sc.C#=c.C#
5 group by sc.C#,c.Cname
6 order by [Percent(%)] desc


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


PS:此題難點在於如何求及格率的百分比,我們可以通過判斷每一行的Score是否大於等於60分的人數除以該課程的人數獲得及格率,然後統一乘以100便得到百分比。這裡使用了聚合函數SUM(PassedCounts)/COUNT(AllCounts)得到及格率(小於1的概率),最後乘以100獲得百分比。核心是這裡的PassedCounts(及格人數)的計算,這裡使用了CASE WHEN *** THEN *** ELSE *** END的語句,靈活地對Score進行了判斷並賦值(1和0)進行計算。

另外,這裡[Percent(%)]可以使用100 * SUM(CASE WHEN ISNULL(sc.Score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*)替代。

(20)查詢如下課程平均成績和及格率的百分數(備註:需要在1行內顯示): 企業管理(002),OO&UML (003),數據庫(004)

1 select 
2 SUM(CASE WHEN C#='002' THEN Score ELSE 0 END)/SUM(CASE C# WHEN '002' THEN 1 ELSE 0 END) as '企業管理平均分',
3 100 * SUM(CASE WHEN C#='002' and Score>=60 THEN 1 ELSE 0 END)/SUM(CASE C# WHEN '002' THEN 1 ELSE 0 END) as '企業管理及格百分比',
4 SUM(CASE WHEN C#='003' THEN Score ELSE 0 END)/SUM(CASE C# WHEN '003' THEN 1 ELSE 0 END) as 'OO&UML平均分',
5 100 * SUM(CASE WHEN C#='003' and Score>=60 THEN 1 ELSE 0 END)/SUM(CASE C# WHEN '003' THEN 1 ELSE 0 END) as 'OO&UML及格百分比',
6 SUM(CASE WHEN C#='004' THEN Score ELSE 0 END)/SUM(CASE C# WHEN '004' THEN 1 ELSE 0 END) as '數據庫平均分',
7 100 * SUM(CASE WHEN C#='004' and Score>=60 THEN 1 ELSE 0 END)/SUM(CASE C# WHEN '004' THEN 1 ELSE 0 END) as '數據庫及格百分比'
8 from SC


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


PS:這裡出現了兩種格式的CASE WHEN語句,但其實這兩種方式,可以實現相同的功能。簡單case函數(例如上面的:CASE C# WHEN '002' THEN 1 ELSE 0 END)的寫法相對比較簡潔,但是和case搜索函數(例如上面的:CASE WHEN C#='002' THEN Score ELSE 0 END)相比,功能方面會有些限制,比如寫判定式。

(21)查詢不同老師所教不同課程平均分從高到低顯示;

1 select c.C#,MAX(c.Cname) as 'Cname',MAX(t.T#) as 'T#',MAX(t.Tname) as 'Tname',
2 AVG(sc.Score) as 'AvgScroe'
3 from SC sc,Course c,Teacher t
4 where sc.C#=c.C# and c.T#=t.T#
5 group by c.C#
6 order by AvgScroe desc


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


PS:可能有園友會對上題中的很多個MAX(列名)有疑惑,這裡我們再來看下Group By語句。這裡需要注意的一點就是,在select指定的字段要麼就要包含在Group By語句的後面,作為分組的依據;要麼就要被包含在聚合函數中。因此,上題中我們需要查詢課程名,教師名等信息,但又不是分組的依據(分組依據應該是課程號),因此就用MAX()這個聚合函數包裹起來。

(22)查詢如下課程成績第 3 名到第 6 名的學生成績單:企業管理(001),馬克思(002),UML (003),數據庫(004)

[學生ID],[學生姓名],企業管理,馬克思,UML,數據庫,平均成績

此題沒有看明白,跳過。

(23)統計列印各科成績,各分數段人數:課程ID,課程名稱,[100-85],[85-70],[70-60],[ <60]

1 select sc.C#,MAX(c.Cname) as 'CourseName',
2 SUM(CASE WHEN sc.Score BETWEEN 85 and 100 THEN 1 ELSE 0 END) as '[85-100]',
3 SUM(CASE WHEN sc.Score BETWEEN 70 and 85 THEN 1 ELSE 0 END) as '[70-85]',
4 SUM(CASE WHEN sc.Score BETWEEN 60 and 70 THEN 1 ELSE 0 END) as '[60-70]',
5 SUM(CASE WHEN sc.Score BETWEEN 0 and 60 THEN 1 ELSE 0 END) as '[<60]'
6 from SC sc,Course c
7 where sc.C#=c.C#

8 group by sc.C#


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(24)查詢學生平均成績及其名次;

 1 select s.S#,s.Sname,T2.AvgScore,
2 (select COUNT(AvgScore) from
3 (select S#,AVG(Score) as 'AvgScore' from SC group by S#) as T1
4 where T2.AvgScore 5 from
6 (select S#,AVG(Score) as 'AvgScore' from SC
7 group by S#) as T2,

8 Student s
9 where s.S#=T2.S#
10 order by AvgScore desc


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


PS:本題目中的名次計算是一個難點,換個思路,我們如何計算當前學生的平均成績的排名可以通過計算有多少個成績小於當前學生的成績再加上1即可得到,如上題目中的:(select COUNT(AvgScore) from (select S#,AVG(Score) as 'AvgScore' from SC group by S#) as T1 where T2.AvgScore

(25)查詢各科成績前三名的記錄:(不考慮成績並列情況)

1 select sc.C#,c.Cname,sc.S#,s.Sname,sc.Score 
2 from Student s,SC sc,Course c
3 where sc.C#=c.C# and sc.S#=s.S# and sc.Score in
4 (
5 select top 3 Score from SC sc2
6 where sc.C#=sc2.C#
7 Order by Score desc
8 )
9 order by sc.C#,sc.Score desc


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(26)查詢每門課程被選修的學生數;

1 select sc.C#,MAX(c.Cname) as 'CName',COUNT(distinct sc.S#) as 'StudentCount'
2 from SC sc,Course c
3 where sc.C#=c.C#
4 group by sc.C#


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(27)查詢出只選修了一門課程的全部學生的學號和姓名;

1 select s.S#,s.Sname 
2 from Student s
3 where s.S# in
4 (
5 select sc.S# from SC sc
6 group by sc.S#
7 having COUNT(distinct sc.C#)=1
8 )

這裡SC表中沒有一個只選了一門課程的學生,可以將語句改為:having COUNT(distinct sc.C#)=2,便可得到以下結果:

數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(28)查詢男生、女生的人數;

1 select COUNT(S#) as 'BoysCount' from Student s where s.Ssex='男'
2 select COUNT(S#) as 'GirlsCount' from Student s where s.Ssex='女'


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(29)查詢姓“張”的學生名單;

1 select s.S#,s.Sname 
2 from Student s
3 where s.Sname like '張%'


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(30)查詢同名同姓學生名單,並統計同名人數;

1 select s.Sname,COUNT(Sname) as 'SameCount'
2 from Student s
3 group by s.Sname
4 having COUNT(Sname)>1

這裡Student表中並沒有兩個同名同姓的學生信息,因此我們插入一條:{7,錢二,20,女},再執行上面的SQL語句可得以下結果:

數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(31)查詢1981年出生的學生名單(注:Student表中Sage列的類型是datetime) ;

這個題目很怪,明明設計數據庫時Sage是int型,這裡又是datetime型,我暈死了。故只給出參考答案,我無法執行看結果。

1 select Sname,CONVERT(char (11),DATEPART(year,Sage)) as Age 
2 from Student
3 where CONVERT(char(11),DATEPART(year,Sage))='1981';

(32)查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列;

1 select sc.C#,AVG(sc.Score) as 'AvgScore' 
2 from SC sc
3 group by sc.C#
4 order by AvgScore asc,C# desc


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(33)查詢平均成績大於85的所有學生的學號、姓名和平均成績;

1 select sc.S#,s.Sname,AVG(sc.Score) as 'AvgScore' 
2 from Student s,SC sc
3 where s.S#=sc.S#
4 group by sc.S#,s.Sname
5 having AVG(sc.Score)>85


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(34)查詢課程名稱為“數學”,且分數低於60的學生姓名和分數;

1 select s.Sname,sc.Score from Student s,SC sc,Course c
2 where s.S#=sc.S# and sc.C#=c.C# and c.Cname='數學' and sc.Score<60


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(35)查詢所有學生的選課情況;

1 select s.S#,s.Sname,c.C#,c.Cname from Student s,SC sc,Course c
2 where s.S#=sc.S# and c.C#=sc.C#
3 order by c.C#,s.S#


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(36)查詢任何一門課程成績在70分以上的姓名、課程名稱和分數;

1 select distinct s.S#,s.Sname,c.Cname,sc.Score 
2 from Student s,SC sc,Course c
3 where s.S#=sc.S# and sc.C#=c.C# and sc.Score>=70


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(37)查詢不及格的課程,並按課程號從大到小排列;

1 select distinct sc.C#,c.Cname from SC sc,Course c
2 where sc.C#=c.C# and sc.Score<60
3 order by sc.C# desc


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(38)查詢課程編號為003且課程成績在80分以上的學生的學號和姓名;

1 select sc.S#,s.Sname from Student s,SC sc
2 where s.S#=sc.S# and sc.C#='003' and sc.Score>=80


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(39)求選了課程的學生人數(超簡單的一題)

1 select COUNT(distinct S#) as 'StuCount' from SC

(40)查詢選修“楊豔”老師所授課程的學生中,成績最高的學生姓名及其成績;

1 select s.S#,s.Sname,sc.Score 
2 from Student s,SC sc,Course c,Teacher t
3 where s.S#=sc.S# and sc.C#=c.C# and c.T#=t.T# and t.Tname='楊豔'
4 and sc.Score =
5 (
6 select MAX(sc2.Score) from SC sc2
7 where sc.C#=sc2.C#
8 )


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(41)查詢各個課程及相應的選修人數;

1 select sc.C#,c.Cname,COUNT(distinct S#) as 'StuCount' from SC sc,Course c
2 where sc.C#=c.C#
3 group by sc.C#,c.Cname


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(42)查詢不同課程但成績相同的學生的學號、課程號、學生成績;

1 select distinct sc1.S#,sc1.C#,sc1.Score from SC sc1,SC sc2
2 where sc1.C#!=sc2.C# and sc1.Score=sc2.Score
3 order by sc1.Score asc


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(43)查詢每門課程成績最好的前兩名;

1 select sc.C#,c.Cname,sc.S#,s.Sname,sc.Score from Student s,SC sc,Course c
2 where s.S#=sc.S# and sc.C#=c.C# and sc.Score in
3 (
4 select top 2 sc2.Score from SC sc2
5 where sc2.C#=sc.C#
6 order by sc2.Score desc
7 )
8 order by sc.C#


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(44)統計每門課程的學生選修人數(超過10人的課程才統計)。要求輸出課程號和選修人數,查詢結果按人數降序排列,查詢結果按人數降序排列,若人數相同,按課程號升序排列

1 select sc.C#,COUNT(distinct S#) as 'StuCount' from SC sc
2 group by sc.C#
3 having COUNT(distinct S#)>=10
4 order by StuCount desc,sc.C# asc

因為SC表中並沒有超過10人的課程,因此本查詢結果為空。

(45)檢索至少選修兩門課程的學生學號;

1 select distinct sc.S# from SC sc
2 group by sc.S#
3 having COUNT(sc.C#)>=2

SC表中所有的數據都選了超過兩門課,因此結果是所有的學號;

(46)查詢全部學生都選修的課程的課程號和課程名;

1 select sc.C#,c.Cname from SC sc,Course c
2 where sc.C#=c.C#
3 group by sc.C#,c.Cname
4 having COUNT(sc.S#)=(select COUNT(distinct s.S#) from Student s)

SC表中插入一條數據{7,2,80},再執行一下上面的SQL語句可以得到下面的結果:

數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(47)查詢沒學過“葉平”老師講授的任一門課程的學生姓名;

1 select s.Sname from Student s where s.S# not in 
2 (
3 select sc.S# from SC sc,Course c,Teacher t
4 where sc.C#=c.C# and c.T#=t.T# and t.Tname='楊豔'
5 )


數據庫基礎,你必知必會的SQL語句練習40題-Part 1


(48)查詢兩門以上不及格課程的同學的學號及其平均成績;

1 select sc.S#,AVG(ISNULL(sc.Score,0)) as 'AvgScore' from SC sc
2 where sc.S# in
3 (
4 select sc2.S# from SC sc2
5 where sc2.Score<60
6 group by sc2.S#
7 having COUNT(sc2.C#)>2
8 )
9 group by sc.S#

因SC表中木有兩門以上不及格的屌絲,因此查詢結果為空;

(49)檢索“004”課程分數小於60,按分數降序排列的同學學號;(很簡單的一題)

1 select sc.S# from SC sc
2 where sc.C#='004' and sc.Score<60
3 order by sc.Score desc

查詢結果:S#為1,3,5

(50)刪除“002”同學的“001”課程的成績;(很簡單的一題)

delete from SC where S#='002' and C#='001'

二、練習總結

本篇是從Cat Qi的原文《SQL面試題(學生表-教師表-課程表-選課表)》中摘抄的,前半部分難度較大,後半部分難度減小,經過我一題一題的練習,也還是得到了很大的鍛鍊。下一篇Part 2,將針對另外兩個類型的題目,這一類題目也是非常常見的題目,大概會有15個題目左右。


分享到:


相關文章: