sql題

下面和你們分享一個比較經典的場景,數據庫的面試題目,主要的表是學生、課程、成績、教師四張表,本示例的特色是有 模擬數據,加深理解和印象,答案主要 基於Oracle來實現

一、四張表分別爲:面試

Student(S#,Sname,Sage,Ssex) 學生表            S#:學號;Sname:學生姓名;Sage:學生年齡;Ssex:學生性別
Course(C#,Cname,T#) 課程表                    C#:課程編號;Cname:課程名字;T#:教師編號
SC(S#,C#,score) 成績表                        S#:學號;C#,課程編號;score:成績
Teacher(T#,Tname) 教師表                      T#:教師編號; Tname:教師名字

二、題目要求數據庫

一、查詢「111」課程比「112」課程成績高的全部學生的學號;
二、查詢平均成績大於60分的同窗的學號和平均成績;
三、查詢全部同窗的學號、姓名、選課數、總成績;
四、查詢姓「李」的老師的個數;
五、查詢沒學過'陳奕迅'老師課的同窗的學號、姓名;
六、查詢學過「001」而且也學過編號「002」課程的同窗的學號、姓名;
七、查詢學過「葉平」老師所教的全部課的同窗的學號、姓名;
八、查詢課程編號「002」的成績比課程編號「001」課程低的全部同窗的學號、姓名
九、查詢全部課程成績小於60分的同窗的學號、姓名
十、查詢沒有學全全部課的同窗的學號、姓名
十一、查詢至少有一門課與學號爲「1001」的同窗所學相同的同窗的學號和姓名
十二、查詢至少學過學號爲「001」同窗全部一門課的其餘同窗學號和姓名
1三、把「SC」表中「葉平」老師教的課的成績都更改成此課程的平均成績
1四、查詢和「1002」號的同窗學習的課程徹底相同的其餘同窗學號和姓名
1五、刪除學習'陳奕迅'老師課的SC表記錄
1六、向SC表中插入一些記錄,這些記錄要求符合如下條件:沒有上過編號「003」課程的同窗學號、2號課的平均成績
1七、按平均成績從高到低顯示全部學生的「語文1」、「生物2」、「化學1」三門的課程成績,按以下形式顯示
1八、查詢各科成績最高和最低的分:以以下形式顯示:課程ID,最高分,最低分
1九、按各科平均成績從低到高和及格率的百分數從高到低順序
20、查詢以下課程平均成績和及格率的百分數(用"1行"顯示)
2一、查詢不一樣老師所教不一樣課程平均分從高到低顯示
2二、查詢以下課程成績第 3 名到第 6 名的學生成績單:語文1(111),語文2(112),數學1 (113),數學2(114)
2三、統計列印各科成績,各分數段人數:課程ID,課程名稱,[100-85] 優秀人數,[85-70] 良好人數,[70-60] 通常人數,[ <60] 剛及格人數
2四、查詢學平生均成績及其名次 
2五、查詢各科成績前三名的記錄:(不考慮成績並列狀況)
2六、查詢每門課程被選修的學生數
2七、查詢出只選修了一門課程的所有學生的學號和姓名
2八、查詢男生、女生人數
2九、查詢名字中有'黑'的學生名單
30、查詢同名同性學生名單,並統計同名人數
3二、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列 3三、查詢平均成績大於85的全部學生的學號、姓名和平均成績 3四、查詢課程名稱爲'語文1',且分數低於60的學生姓名和分數 3五、查詢全部學生的選課狀況 3六、查詢任何一門課程成績在70分以上的姓名、課程名稱和分數 3七、查詢不及格的課程,並按課程號從大到小排列 3八、查詢課程編號爲003且課程成績在80分以上的學生的學號和姓名 3九、求選了課程的學生人數 40、查詢選修「葉平」老師所授課程的學生中,成績最高的學生姓名及其成績 4一、查詢各個課程及相應的選修人數 4二、查詢不一樣課程成績相同的學生的學號、課程號、學生成績 4三、查詢每門功成績最好的前兩名 4四、統計每門課程的學生選修人數(超過10人的課程才統計)。要求輸出課程號和選修人數,查詢結果按人數降序排列,查詢結果按人數降序排列,若人數相同,按課程號升序排列 4五、檢索至少選修兩門課程的學生學號 4六、查詢所有學生都選修的課程的課程號和課程名 4七、查詢沒學過'陳奕迅'老師講授的任一門課程的學生姓名 4八、查詢兩門以上不及格課程的同窗的學號及其平均成績 4九、檢索'114'課程分數小於60,按分數降序排列的同窗學號 50、刪除'2'同窗的'111'課程的成績


/*====================================*/
/*=========== 習題開始 =============*/
/*====================================*/
--一、查詢「111」課程比「112」課程成績高的全部學生的學號;
/* EXISTS 版本 */
SELECT t1.s#
FROM sc t1
WHERE t1.c# = 111 
  AND EXISTS (
               SELECT 1
               FROM sc t2
               WHERE t2.c# = 112
                 AND t1.s# = t2.s#  --要求同一個學生 因此有t1.s# = t2.s#
                 AND t1.score > t2.score
             );
/* 子查詢版本 */
SELECT a.s#
FROM (SELECT t1.s#,t1.score FROM sc t1 WHERE t1.c# = 111) a
INNER JOIN (SELECT t2.s#,t2.score FROM sc t2 WHERE t2.c# = 112) b
  ON a.s# = b.s#
WHERE a.score > b.score;

--二、查詢平均成績大於60分的同窗的學號和平均成績;
SELECT t1.s# AS ST_CODE,
       AVG(t1.score) AS CU_AVG
FROM sc t1
GROUP BY t1.s#
  HAVING AVG(t1.score) > 60
  
--三、查詢全部同窗的學號、姓名、選課數、總成績;
/* group by 的時候要注意選出來的字段是否是都是聚合函數或者分組的字段 */
SELECT t1.s# AS ST_DOCE,
       MIN(t1.sname) AS ST_NAME,
       COUNT(t2.c#)  AS CU_NUM,
       SUM(t2.score) AS CU_SUM
FROM STUDENT t1
LEFT JOIN SC t2
  ON t1.s# = t2.s#
GROUP BY t1.s#

--四、查詢姓「李」的老師的個數;
SELECT COUNT(t1.t#) AS 李_NUM
FROM teacher t1
WHERE t1.tname LIKE '李%'

--五、查詢沒學過'陳奕迅'老師課的同窗的學號、姓名;
/* 一對多關係的就會出現比較坑爹的狀況,記得用distinct而且要考慮是否是要作一個子查詢 */
/* 當正面來可能比較困難的時候就要反方面來進行 */
/* EXISTS版 */
SELECT t5.s# AS ST_CODE,
       t5.sname AS ST_NAME
FROM student t5
WHERE NOT EXISTS (
                   SELECT 1
                   FROM (
                          SELECT DISTINCT t1.s# AS ST_CODE,
                                          t1.sname AS ST_NAME,
                                          t4.tname
                          FROM student t1
                          LEFT JOIN sc t2
                            ON t1.s# = t1.s#
                          LEFT JOIN course t3
                            ON t2.c# = t3.c#
                          LEFT JOIN teacher t4
                            ON t3.t# = t4.t#
                          WHERE t4.tname = '陳奕迅'
                         )
                   WHERE s# = t5.s#
                 );
/* 子查詢版 */
SELECT t5.s# AS ST_CODE,
       t5.sname AS ST_NAME
FROM student t5
WHERE t5. EXISTS (
                   SELECT 1
                   FROM (
                          SELECT DISTINCT t1.s# AS ST_CODE,
                                          t1.sname AS ST_NAME,
                                          t4.tname
                          FROM student t1
                          LEFT JOIN sc t2
                            ON t1.s# = t1.s#
                          LEFT JOIN course t3
                            ON t2.c# = t3.c#
                          LEFT JOIN teacher t4
                            ON t3.t# = t4.t#
                          WHERE t4.tname = '陳奕迅'
                         )
                   WHERE s# = t5.s#
                 );
                 
--六、查詢學過「001」而且也學過編號「002」課程的同窗的學號、姓名;
--不能在where條件下面寫WHERE t2.c# = 111 and t2.c# = 112 由於是對應同一條記錄的
SELECT DISTINCT t1.s#,t1.sname
FROM student t1
LEFT JOIN sc t2
  ON t1.s# = t2.s#
WHERE t2.c# = 111

INTERSECT  --集合的交集

SELECT DISTINCT t1.s#,t1.sname
FROM student t1
LEFT JOIN sc t2
  ON t1.s# = t2.s#
WHERE t2.c# = 112

--七、查詢學過「葉平」老師所教的全部課的同窗的學號、姓名;
SELECT DISTINCT t1.s# AS ST_CODE,
                t1.sname AS ST_NAME,
FROM student t1
LEFT JOIN sc t2
  ON t1.s# = t1.s#
LEFT JOIN course t3
  ON t2.c# = t3.c#
LEFT JOIN teacher t4
  ON t3.t# = t4.t#
WHERE t4.tname = '李冰冰'

--八、查詢課程編號「002」的成績比課程編號「001」課程低的全部同窗的學號、姓名;
/* 用單純的join版 */
/* 用了子查詢 */
SELECT a.s#
FROM (SELECT t1.s#,t1.score FROM sc t1 WHERE t1.c# = 111) a
INNER JOIN (SELECT t2.s#,t2.score FROM sc t2 WHERE t2.c# = 112) b
  ON a.s# = b.s#
WHERE a.score < b.score

--九、查詢全部課程成績小於60分的同窗的學號、姓名;
SELECT T1.S#, T1.SNAME
FROM STUDENT T1
INNER JOIN SC T2 
  ON T1.S# = T2.S#
WHERE T2.C# < 60

--十、查詢沒有學全全部課的同窗的學號、姓名;
/*子查詢版*/
SELECT DISTINCT t1.s#,t1.sname
FROM student t1
INNER JOIN SC t2
  ON t1.s# = t2.s#
WHERE t2.c# IN (
                SELECT t3.c#
                FROM Course t3
               )
/*exists版*/               
SELECT DISTINCT t1.s#,t1.sname
FROM student t1
INNER JOIN SC t2
  ON t1.s# = t2.s#
WHERE EXISTS (
              SELECT 1
              FROM Course t3
              WHERE t3.c# = t2.c#
             )
             
--十一、查詢至少有一門課與學號爲「1001」的同窗所學相同的同窗的學號和姓名;
/* 子查詢 */
SELECT DISTINCT t1.s#,t1.sname
FROM student t1
INNER JOIN sc t2
  ON t1.s# = t2.s#
WHERE t2.c# IN (
                SELECT t4.c#
                FROM student t3
                INNER JOIN sc t4
                  ON t3.s# = t4.s#
                WHERE t3.s# = 1
               )
/* exists版 */
SELECT DISTINCT t1.s#,t1.sname
FROM student t1
INNER JOIN sc t2
  ON t1.s# = t2.s#
WHERE EXISTS  (
               SELECT 1
               FROM student t3
               INNER JOIN sc t4
                 ON t3.s# = t4.s#
               WHERE t4.c# = t2.c# 
                 AND t3.s# = 1              
              )
              
--十二、查詢至少學過學號爲「001」同窗全部一門課的其餘同窗學號和姓名;
/* 子查詢 */
SELECT DISTINCT t1.s#,t1.sname
FROM student t1
INNER JOIN sc t2
  ON t1.s# = t2.s#
  AND t1.s# <> 1
WHERE t2.c# IN (
                SELECT t4.c#
                FROM student t3
                INNER JOIN sc t4
                  ON t3.s# = t4.s#
                WHERE t3.s# = 1
               )
/* exists版 */
SELECT DISTINCT t1.s#,t1.sname
FROM student t1
INNER JOIN sc t2
  ON t1.s# = t2.s#
  AND t1.s# <> 1
WHERE EXISTS  (
               SELECT 1
               FROM student t3
               INNER JOIN sc t4
                 ON t3.s# = t4.s#
               WHERE t4.c# = t2.c# 
                 AND t3.s# = 1               
              )
              
--1三、把「SC」表中「葉平」老師教的課的成績都更改成此課程的平均成績;
UPDATE sc t4
SET t4.score = (
                WITH tmp AS 
                (
                  SELECT t1.c#,
                         AVG(t1.score) AS avg_score
                  FROM sc t1
                  INNER JOIN Course t2
                    ON t1.c# = t2.c#
                  INNER JOIN teacher t3
                    ON t2.t# = t3.t#
                  GROUP BY t1.c#
                )
                SELECT t5.avg_score 
                FROM tmp t5 
                WHERE t4.c# = t5.c#
               )
WHERE EXISTS (
               SELECT 1
               FROM sc t6
               INNER JOIN Course t7
                 ON t6.c# = t7.c#
               INNER JOIN teacher t8
                 ON t7.t# = t8.t#
                 AND t8.tname = '陳奕迅'
             )
             
1四、查詢和「1002」號的同窗學習的課程徹底相同的其餘同窗學號和姓名;
SELECT DISTINCT t1.s#,
                t1.sname
FROM student t1
INNER JOIN sc t2
  ON t1.s# = t2.s#
AND t2.c# = ALL  (
              SELECT t4.c#
              FROM student t3
              INNER JOIN sc t4
                ON t3.s# = t4.s#
              AND t4.s# = 7
             )
AND t1.s# <> 7

--1五、刪除學習'陳奕迅'老師課的SC表記錄;
DELETE FROM sc t1
WHERE EXISTS (
               SELECT 1
               FROM teacher t2
               INNER JOIN Course t3
                 ON t2.t# = t3.t#
               WHERE t3.c# = t1.c#
                 AND t2.tname = '陳奕迅'
)

1六、向SC表中插入一些記錄,這些記錄要求符合如下條件:沒有上過編號「003」課程的同窗學號、2號課的平均成績;
INSERT INTO sc(s#,c#,score)

SELECT min(t5.s#),112,AVG(t4.score)
FROM sc t4
INNER JOIN student t5
  ON t4.s# = t5.s#
WHERE t4.s# NOT IN (
                SELECT t2.s#
                FROM student t2
                INNER JOIN sc t3
                  ON t2.s# = t3.s#
                AND t3.c# = 113
                )
AND t4.c# = 112
GROUP BY t4.c# 

--1七、按平均成績從高到低顯示全部學生的「語文1」、「生物2」、「化學1」三門的課程成績,按以下形式顯示: 學生ID,,數據庫,企業管理,英語,有效課程數,有效平均分
/* 行轉列除了能夠用poivt外還能夠分別select出來,再和主表的字段關聯 */
SELECT t1.S# as 學生ID,
       (SELECT score FROM SC t2 WHERE t2.S#=t1.S# AND t2.C#=111) AS 語文1,
       (SELECT score FROM SC t3 WHERE t3.S#=t1.S# AND t3.C#=118) AS 生物2, 
       (SELECT score FROM SC t4 WHERE t4.S#=t1.S# AND t4.C#=121) AS 化學1, 
       COUNT(*) AS 有效課程數,
       AVG(t1.score) AS 平均成績 
FROM SC t1
GROUP BY t1.S# 
ORDER BY avg(t1.score)  

--1八、查詢各科成績最高和最低的分:以以下形式顯示:課程ID,最高分,最低分
SELECT t1.c#,
       MIN(t1.score) AS min_score,
       MAX(t1.score) AS max_score
FROM sc t1
GROUP BY t1.c# 

--1九、按各科平均成績從低到高和及格率的百分數從高到低順序
SELECT t1.c#,
       AVG(t1.score) AS avg_score,
       100 * SUM(
                 CASE 
                  WHEN nvl(t1.score,0)>=60 THEN 
                    1 
                  ELSE 
                    0 
                  END
                )/COUNT(*) AS pass_pct
FROM sc t1 
GROUP BY t1.c#
ORDER BY avg_score ASC,pass_pct DESC

--20、查詢以下課程平均成績和及格率的百分數(用"1行"顯示): 語文1(111),語文2(112),數學1(113),數學2(114)
SELECT SUM(CASE WHEN t1.c# =111 THEN score ELSE 0 END)/SUM(CASE t1.c# WHEN 111 THEN 1 ELSE 0 END)                      AS 語文1           ,
       100 * SUM(CASE WHEN t1.c# = 111 AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN t1.c# = 111 THEN 1 ELSE 0 END) AS 語文1及格百分比 ,
       SUM(CASE WHEN t1.c# = 112 THEN score ELSE 0 END)/SUM(CASE t1.c# WHEN 112 THEN 1 ELSE 0 END)                     AS 語文2           ,
       100 * SUM(CASE WHEN t1.c# = 112 AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN t1.c# = 112 THEN 1 ELSE 0 END) AS 語文2及格百分比 ,
       SUM(CASE WHEN t1.c# = 113 THEN score ELSE 0 END)/SUM(CASE t1.c# WHEN 113 THEN 1 ELSE 0 END)                     AS 數學1           ,
       100 * SUM(CASE WHEN t1.c# = 113 AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN t1.c# = 113 THEN 1 ELSE 0 END) AS 數學1及格百分比 ,
       SUM(CASE WHEN t1.c# = 114 THEN score ELSE 0 END)/SUM(CASE t1.c# WHEN 114 THEN 1 ELSE 0 END)                     AS 數學2           ,
       100 * SUM(CASE WHEN t1.c# = 114 AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN t1.c# = 114 THEN 1 ELSE 0 END) AS 數學2及格百分比 
FROM SC t1

--2一、查詢不一樣老師所教不一樣課程平均分從高到低顯示
SELECT t3.tname,
       t2.cname,
       AVG(t1.score) AS avg_score
FROM sc t1
INNER JOIN course t2
  ON t1.c# = t2.c#
INNER JOIN teacher t3
  ON t2.t# = t3.t#
GROUP BY t3.tname,t2.cname
ORDER BY avg_score DESC

--2二、查詢以下課程成績第 3 名到第 6 名的學生成績單:企業管理(111),馬克思(112),UML (113),數據庫(114) 
SELECT sub_query.cname,
       sub_query.rank      
FROM(
      SELECT t2.cname,
             t1.score,
             rank() over(PARTITION BY t1.c# ORDER BY t1.score DESC) AS rank  
      FROM sc t1
      INNER JOIN course t2
        ON t1.c# = t2.c#
      WHERE t1.c# IN (111,112,113,114)
    ) sub_query
WHERE sub_query.rank BETWEEN 3 AND 6;

--2三、統計列印各科成績,各分數段人數:課程ID,課程名稱,[100-85] 優秀人數,[85-70] 良好人數,[70-60] 通常人數,[ <60] 剛及格人數
SELECT t1.c#,
       t2.cname,
       SUM(CASE WHEN t1.score > 85 AND t1.score <= 100 THEN 1 ELSE 0 END) AS 優秀人數,
       SUM(CASE WHEN t1.score > 70 AND t1.score <= 85 THEN 1 ELSE 0 END)  AS 良好人數,
       SUM(CASE WHEN t1.score > 60 AND t1.score <= 70 THEN 1 ELSE 0 END)  AS 通常人數,
       SUM(CASE WHEN t1.score <= 60 THEN 1 ELSE 0 END)                    AS 剛及格人數
FROM sc t1
INNER JOIN course t2
  ON t1.c# = t2.c#
GROUP BY t1.c#,t2.cname

--2四、查詢學平生均成績及其名次 
SELECT t1.sname,
       t1.s#,
       AVG(t2.score) AS avg_score,
       dense_rank() over(ORDER BY AVG(t2.score) DESC) AS dense_rank
FROM student t1
INNER JOIN sc t2
  ON t1.s# = t2.s#
GROUP BY t1.sname,t1.s#

--2五、查詢各科成績前三名的記錄:(不考慮成績並列狀況)
SELECT sub_query.cname,
       sub_query.s#,
       sub_query.sname,
       sub_query.score,
       sub_query.rank
FROM ( 
       SELECT t3.cname,
              t1.s#,
              t1.sname,
              t2.score,
              rank() over(PARTITION BY t2.c# ORDER BY t2.score DESC) AS rank
       FROM student t1
       INNER JOIN sc t2
         ON t1.s# = t2.s#
       INNER JOIN course t3
         ON t2.c# = t3.c#
      ) sub_query
WHERE sub_query.rank < 4

--2六、查詢每門課程被選修的學生數 
SELECT t2.cname,
       COUNT(t1.s#) AS sum_stu
FROM sc t1
INNER JOIN course t2
  ON t1.c# = t2.c#
GROUP BY t2.cname

--2七、查詢出只選修了一門課程的所有學生的學號和姓名 
/* GROUP BY 語句中選擇出來的只有備份組的字段和聚合函數 */
SELECT t1.s#,
       t1.sname
FROM student t1
INNER JOIN sc t2
  ON t1.s# = t2.s#
GROUP BY t1.s#,t1.sname
HAVING COUNT(t2.c#) = 1

--2八、查詢男生、女生人數
SELECT t1.ssex,
       COUNT(t1.ssex) AS sex_num  
FROM student t1
GROUP BY t1.ssex

--2九、查詢名字中有'黑'的學生名單
SELECT t1.s#,
       t1.sname
FROM student t1
WHERE t1.sname LIKE '%黑%'

--30、查詢同名同性學生名單,並統計同名人數
/* 寫了個錯的 
   緣由:student表中有兩個小粉,on條件只是縣限定了sname相等,因此致使了2X2的笛卡爾乘積*/
SELECT t1.sname,
       COUNT(t1.s#) AS same_name_count
FROM student t1
INNER JOIN student t2
  ON t1.sname = t2.sname
GROUP BY t1.sname,t2.sname
HAVING COUNT(t1.sname) > 1
/* 正確的寫法 */
SELECT t1.sname,
       COUNT(t1.sname) AS same_name_count
FROM student t1
GROUP BY t1.sname
HAVING COUNT(t1.sname) > 1

--3二、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列
SELECT t2.cname,
       t2.c#,
       AVG(t1.score) AS avg_score       
FROM sc t1
INNER JOIN course t2
  ON t1.c# = t2.c#
GROUP BY t2.cname,t2.c#
ORDER BY avg_score ASC,t2.c# DESC

--3三、查詢平均成績大於85的全部學生的學號、姓名和平均成績
SELECT t1.s#,
       t1.sname,
       AVG(t2.score) AS avg_score
FROM student t1
INNER JOIN sc t2
  ON t1.s# = t2.s#
GROUP BY t1.s#,t1.sname
HAVING AVG(t2.score) > 85

--3四、查詢課程名稱爲'語文1',且分數低於60的學生姓名和分數
SELECT t1.sname,
       t2.score
FROM student t1
INNER JOIN sc t2
  ON t1.s# = t2.s#
INNER JOIN course t3
  ON t2.c# = t3.c#
WHERE t3.cname = '語文1'
  AND t2.score < 60
  
--3五、查詢全部學生的選課狀況;
/* 可能不是這樣寫,有更好的寫法 */
SELECT t1.s#,
       t1.sname,
       t3.cname
FROM student t1
INNER JOIN sc t2
  ON t1.s# = t2.s#
INNER JOIN course t3
  ON t2.c# = t3.c#
  
--3六、查詢任何一門課程成績在70分以上的姓名、課程名稱和分數;
SELECT t1.s#,
       t1.sname,
       t3.cname,
       t2.score
FROM student t1
INNER JOIN sc t2
  ON t1.s# = t2.s#
INNER JOIN course t3
  ON t2.c# = t3.c#
WHERE t2.score > 70

--3七、查詢不及格的課程,並按課程號從大到小排列
SELECT t1.c#,
       t2.cname,
       t1.score
FROM sc t1
INNER JOIN course t2
  ON t1.c# = t2.c#
WHERE t1.score < 60
ORDER BY t1.c# DESC

--3八、查詢課程編號爲003且課程成績在80分以上的學生的學號和姓名;
SELECT t1.s#,
       t1.sname,
       t2.score
FROM student t1
INNER JOIN sc t2
  ON t1.s# = t2.s#
INNER JOIN course t3
  ON t2.c# = t3.c#
WHERE t2.c# = 113
  AND t2.score > 80 
  
--3九、求選了課程的學生人數
SELECT SUM(1)
FROM (
      SELECT 1
      FROM sc t1
      GROUP BY t1.s#
      )
      
--40、查詢選修「葉平」老師所授課程的學生中,成績最高的學生姓名及其成績
SELECT t4.s#,
       t4.sname,
       t5.score,
       t6.cname
FROM student t4
INNER JOIN sc t5
  ON t4.s# = t5.s#
INNER JOIN course t6
  ON t5.c# = t6.c#
WHERE (t5.score,t6.cname) IN (
                              SELECT MAX(t1.score),
                                     t2.cname
                              FROM sc t1
                              INNER JOIN course t2
                                ON t1.c# = t2.c#
                              INNER JOIN teacher t3
                                ON t2.t# = t3.t#
                              WHERE t3.tname = '陳奕迅'
                                GROUP BY t2.cname
                             )
                             
--4一、查詢各個課程及相應的選修人數
SELECT t1.c#,
       t2.cname,
       COUNT(t1.s#)
FROM sc t1
INNER JOIN course t2
  ON t1.c# = t2.c#
GROUP BY t1.c#,t2.cname

--4二、查詢不一樣課程成績相同的學生的學號、課程號、學生成績
SELECT t1.s#,
       t1.c#,
       t1.score
FROM sc t1
INNER JOIN sc t2
  ON t1.score = t2.score
  AND t1.c# <> t2.c#
  AND t1.s# = t2.s#
  
--4三、查詢每門功成績最好的前兩名
SELECT sub_query.s#,
       sub_query.sname,
       sub_query.cname,
       sub_query.score,
       sub_query.rank
FROM (
      SELECT t1.s#,
             t3.sname,
             t2.cname,
             t1.score,
             dense_rank() over(PARTITION BY t1.c# ORDER BY t1.score DESC) AS rank
      FROM sc t1
      INNER JOIN course t2
        ON t1.c# = t2.c#
      INNER JOIN student t3
        ON t1.s# = t3.s#
      ) sub_query
WHERE sub_query.rank < 3

--4四、統計每門課程的學生選修人數(超過10人的課程才統計)。要求輸出課程號和選修人數,查詢結果按人數降序排列,查詢結果按人數降序排列,若人數相同,按課程號升序排列
SELECT t1.c#,
       COUNT(t1.s#) AS stu_num
FROM sc t1
GROUP BY t1.c#
  HAVING COUNT(t1.s#) > 10
ORDER BY stu_num DESC,t1.c# ASC

--4五、檢索至少選修兩門課程的學生學號
SELECT t1.s#
FROM sc t1
GROUP BY t1.s#
  HAVING COUNT(t1.c#) > 1
  
--4六、查詢所有學生都選修的課程的課程號和課程名
SELECT t1.c#,
       t2.cname,
       COUNT(t1.s#)
FROM sc t1
INNER JOIN course t2
  ON t1.c# = t2.c#
GROUP BY t1.c#,t2.cname
  HAVING COUNT(t1.s#) = (
                          SELECT COUNT(t3.s#)
                          FROM student t3
                        )
                        
--4七、查詢沒學過'陳奕迅'老師講授的任一門課程的學生姓名
SELECT t5.s#, 
       t5.sname
FROM student t5
WHERE t5.s# NOT IN (
                     SELECT DISTINCT t1.s#
                     FROM student t1
                     INNER JOIN sc t2
                       ON t1.s# = t2.s#
                     INNER JOIN course t3
                       ON t2.c# = t3.c#
                     INNER JOIN teacher t4
                       ON t3.t# = t4.t#
                     WHERE t4.tname = '陳奕迅'
                    )
                    
--4八、查詢兩門以上不及格課程的同窗的學號及其平均成績
/* 不能直接用下面的方式,由於有where條件過濾了一部分的成績
SELECT t1.s#,
       AVG(t1.score)
FROM sc t1
WHERE t1.score < 60
GROUP BY t1.s#
  HAVING COUNT(t1.c#) > 2 */                  
SELECT t2.s#,
       AVG(t2.score)
FROM sc t2
WHERE t2.s# IN (
                SELECT t1.s#
                FROM sc t1
                WHERE t1.score < 60
                GROUP BY t1.s#
                  HAVING COUNT(t1.c#) > 2
               )
GROUP BY t2.s#

--4九、檢索'114'課程分數小於60,按分數降序排列的同窗學號
SELECT t1.s#
FROM sc t1
WHERE t1.c# = 114
  AND t1.score < 60
ORDER BY t1.s# DESC

--50、刪除'2'同窗的'111'課程的成績
DELETE FROM sc t1
WHERE t1.s# = 2
  AND t1.c# = 111
相關文章
相關標籤/搜索