走向面試之數據庫基礎:1、你必知必會的SQL語句練習-Part 1

本文是在Cat Qi的參考原帖的基礎之上經本人一題一題練習後編輯而成,非原創,僅潤色而已。另外,本文所列題目的解法並不是只有一種,本文只是給出比較普通的一種而已,也但願各位園友可以自由發揮。html

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   )
View Code

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

  (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
View Code

1.3 開始實戰吧小宇宙

  (1)查詢「001」課程比「002」課程成績高的全部學生的學號;oop

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

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

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

  (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#

  (4)查詢姓「李」的老師的個數;spa

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

  (5)查詢沒學過「葉平」老師課的同窗的學號、姓名;.net

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 )

  (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 )

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 )            

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

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

  (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<b.Score

  (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 )

  (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 )

  (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

  (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

  (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 )

  (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'
    )
)

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

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

  (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')

  (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)

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

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

  (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

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

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

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#

  (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<T1.AvgScore)+1 as 'Rank'
 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

PS:本題目中的名次計算是一個難點,換個思路,咱們如何計算當前學生的平均成績的排名能夠經過計算有多少個成績小於當前學生的成績再加上1便可獲得,如上題目中的:

(select COUNT(AvgScore) from
(select S#,AVG(Score) as 'AvgScore' from SC group by S#) as T1
where T2.AvgScore<T1.AvgScore)+1 as 'Rank'

  (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

  (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#

  (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,即可獲得如下結果:

  (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=''

  (29)查詢姓「張」的學生名單;

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

  (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語句可得如下結果:

  (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

  (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

  (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

  (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#

  (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

  (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

  (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

  (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 )

  (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

  (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

  (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#

  (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語句能夠獲得下面的結果:

  (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 )

  (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'

2、練習總結

  本篇是從Cat Qi的原文《SQL面試題(學生表-教師表-課程表-選課表)》中摘抄的,前半部分難度較大,後半部分難度減少,通過我一題一題的練習,也仍是獲得了很大的鍛鍊。下一篇Part 2,將針對另外兩個類型的題目,我暫時取名爲「書到用時方恨少:圖書-讀者-借閱」類題目,這一類題目也是很是常見的題目,大概會有15個題目左右。

參考原帖

  (1)Cat Qi,《SQL面試題(學生表-教師表-課程表-選課表)》:http://www.cnblogs.com/qixuejia/p/3637735.html

  (2)CSDN,《找些不錯的SQL面試題》討論帖,http://bbs.csdn.net/topics/280002741

 

相關文章
相關標籤/搜索