1. 程式人生 > >sql經典題50例(學生表篇)(上)

sql經典題50例(學生表篇)(上)

這周開始徒手練習sql語句 對著上篇的sql學習路徑跟上一遍
現在上建表sql語句

  1. SHOW CREATE TABLE COURSE
  2. – 1.學生表
  3. Student(SNO,Sname,Sage,Ssex) – SNO 學生編號,Sname 學生姓名,Sage 出生年月,Ssex 學生性別
  4. – 2.課程表
  5. Course(CNO,Cname,TNO) – C# –課程編號,Cname 課程名稱,T# 教師編號
  6. – 3.教師表
  7. Teacher(TNO,Tname) – T# 教師編號,Tname 教師姓名
  8. – 4.成績表
  9. SC(SNO,CNO,score) – S# 學生編號,C# 課程編號,score 分數
  10. – 建立測試資料
  11. CREATE TABLE Student (SNO VARCHAR(10) NOT NULL,Sname VARCHAR(10) NOT NULL,Sage DATETIME,Ssex ENUM(‘男’,’女’));
  12. INSERT INTO Student VALUES(‘1001’ , ‘趙雷’ , ‘1996-01-01’ , ‘男’);
  13. INSERT INTO Student VALUES(‘1002’ , ‘錢電’ , ‘1997-12-21’ , ‘男’);
  14. INSERT INTO Student VALUES(‘1003’ , ‘孫風’ , ‘1996-05-20’ , ‘男’);
  15. INSERT INTO Student VALUES(‘1004’ , ‘李雲’ , ‘1995-08-06’ , ‘男’);
  16. INSERT INTO Student VALUES(‘1005’ , ‘周梅’ , ‘1996-12-01’ , ‘女’);
  17. INSERT INTO Student VALUES(‘1006’ , ‘吳蘭’ , ‘1995-03-01’ , ‘女’);
  18. INSERT INTO Student VALUES(‘1007’ , ‘鄭竹’ , ‘1994-07-01’ , ‘女’);
  19. INSERT INTO Student VALUES(‘1008’ , ‘王菊’ , ‘1996-01-20’ , ‘女’);
  20. COMMIT
  21. CREATE TABLE Course(CNO VARCHAR(10),Cname NVARCHAR(10),TNO VARCHAR(10))
  22. INSERT INTO Course VALUES(‘001’ , ‘企業管理’ , ‘02’);
  23. INSERT INTO Course VALUES(‘002’ , ‘馬克思’ , ‘01’);
  24. INSERT INTO Course VALUES(‘003’ , ‘OO&UML’ , ‘04’);
  25. INSERT INTO Course VALUES(‘004’ , ‘資料庫’ , ‘03’);
  26. INSERT INTO Course VALUES(‘005’ , ‘資料分析’ , ‘06’);
  27. INSERT INTO Course VALUES(‘006’ , ‘英語’ , ‘05’);
  28. INSERT INTO Course VALUES(‘007’ , ‘大物’ , ‘03’);
  29. INSERT INTO Course VALUES(‘008’ , ‘嵌入式’ , ‘05’);
  30. SELECT * FROM Course
  31. CREATE TABLE Teacher(TNO VARCHAR(10),Tname VARCHAR(10))
  32. INSERT INTO Teacher VALUES(‘01’ , ‘葉平’);
  33. INSERT INTO Teacher VALUES(‘02’ , ‘李正’);
  34. INSERT INTO Teacher VALUES(‘03’ , ‘馬冬梅’);
  35. INSERT INTO Teacher VALUES(‘04’ , ‘李想’);
  36. INSERT INTO Teacher VALUES(‘05’ , ‘李玲’);
  37. INSERT INTO Teacher VALUES(‘06’ , ‘尚加號’);
  38. CREATE TABLE SC(SNO VARCHAR(10),CNO VARCHAR(10),score DECIMAL(5,1))
  39. INSERT INTO SC VALUES(‘1001’ , ‘001’ , 80);
  40. INSERT INTO SC VALUES(‘1001’ , ‘002’ , 90);
  41. INSERT INTO SC VALUES(‘1001’ , ‘003’ , 79);
  42. INSERT INTO SC VALUES(‘1001’ , ‘004’ , 69);
  43. INSERT INTO SC VALUES(‘1001’ , ‘005’ , 89);
  44. INSERT INTO SC VALUES(‘1001’ , ‘006’ , 91);
  45. INSERT INTO SC VALUES(‘1002’ , ‘001’ , 73);
  46. INSERT INTO SC VALUES(‘1002’ , ‘002’ , 68);
  47. INSERT INTO SC VALUES(‘1002’ , ‘003’ , 88);
  48. INSERT INTO SC VALUES(‘1002’ , ‘004’ , 69);
  49. INSERT INTO SC VALUES(‘1003’ , ‘001’ , 85);
  50. INSERT INTO SC VALUES(‘1003’ , ‘002’ , 84);
  51. INSERT INTO SC VALUES(‘1003’ , ‘003’ , 95);
  52. INSERT INTO SC VALUES(‘1003’ , ‘005’ , 65);
  53. INSERT INTO SC VALUES(‘1004’ , ‘001’ , 59);
  54. INSERT INTO SC VALUES(‘1004’ , ‘002’ , 35);
  55. INSERT INTO SC VALUES(‘1004’ , ‘003’ , 24);
  56. INSERT INTO SC VALUES(‘1004’ , ‘006’ , 59);
  57. INSERT INTO SC VALUES(‘1005’ , ‘001’ , 76);
  58. INSERT INTO SC VALUES(‘1005’ , ‘002’ , 87);
  59. INSERT INTO SC VALUES(‘1005’ , ‘005’ , 69);
  60. INSERT INTO SC VALUES(‘1006’ , ‘001’ , 31);
  61. INSERT INTO SC VALUES(‘1006’ , ‘004’ , 72);
  62. INSERT INTO SC VALUES(‘1006’ , ‘003’ , 34);
  63. INSERT INTO SC VALUES(‘1007’ , ‘004’ , 93);
  64. INSERT INTO SC VALUES(‘1007’ , ‘002’ , 89);
  65. INSERT INTO SC VALUES(‘1007’ , ‘003’ , 98);
  66. INSERT INTO SC VALUES(‘1007’ , ‘001’ , 82);
  67. commit
  • 1.查詢‘001’課程比‘002’課程成績高的所有學生的學號
  1. SELECT * FROM (SELECT SCORE,SC.SNO AS SC_SNO FROM SC JOIN Student AS ST ON ST.SNO = SC.`SNO` WHERE CNO = ‘001’) AS A,
  2. (SELECT SCORE,SC.SNO AS SC_SNO FROM SC JOIN Student AS ST ON ST.SNO = SC.`SNO` WHERE CNO = ‘002’) AS B WHERE A.SC_SNO=B.SC_SNO AND A.SCORE > B.SCORE
  • 2.查詢平均成績大於60分同學的學號和平均成績
  1. SELECT SNO,AVG(SCORE) AS AVG_SC FROM SC GROUP BY SNO HAVING AVG_SC>60
  • 3.查詢所有同學的學號 姓名 選課數 總成績
SELECT * FROM SC
-- class number
SELECT SNO,COUNT(*) AS class_num,SUM(score) AS total_sum FROM SC GROUP BY SNO
-- grade 
SELECT DISTINCT
  SC.SNO,
  Sname,
  B.class_num,
  B.total_sum
FROM
  Student
  JOIN SC
    ON SC.SNO = Student.SNO
  JOIN
    (SELECT
      SNO,
      COUNT(*) AS class_num,
      SUM(score) AS total_sum
    FROM
      SC
    GROUP BY SNO) AS B
    ON B.SNO = Student.SNO
  • 4.查詢姓 李 老師的個數
SELECT * FROM Teacher
SELECT COUNT(*) FROM Teacher WHERE Tname LIKE '李%'
  • 5.查詢沒學過 葉平老師課的同學的學號、姓名
-- 要篩選目標 --學過老師的課的學號 姓名
-- 1001 02 03 04 05 07
SELECT SC.SNO FROM Course AS CO JOIN Teacher AS TA ON CO.TNO = TA.TNO JOIN SC ON SC.CNO = CO.CNO WHERE Tname = '葉平'
-- 邏輯不對  找對應的
SELECT  * FROM Student JOIN SC ON SC.SNO = Student.`SNO` WHERE SC.`CNO` != 002

SELECT DISTINCT
  Student.SNO,
  Sname
FROM
  Student
  JOIN SC
    ON SC.SNO = Student.`SNO`
WHERE SC.SNO NOT IN
  (SELECT
    SC.SNO
  FROM
    Course AS CO
    JOIN Teacher AS TA
      ON CO.TNO = TA.TNO
    JOIN SC
      ON SC.CNO = CO.CNO
  WHERE Tname = '葉平')
  • 6.查詢學過“001”並且也學過編號“002”課程的同學的學號、姓名
SELECT
  *
FROM
  Student
  JOIN SC
    ON SC.SNO = Student.SNO
WHERE CNO = '001'
  AND SC.SNO IN
  (SELECT
    SC.SNO
  FROM
    SC
  WHERE CNO = '002')
  • 7.查詢學過葉平老師所教的所有課的同學的學號和姓名
SELECT TNO FROM Teacher WHERE Tname = '葉平'
SELECT
  SC.`SNO`,
  ST.Sname
FROM
  SC
  JOIN Course AS CO
    ON CO.CNO = SC.CNO
  JOIN Teacher AS TA
    ON TA.TNO = CO.TNO
  JOIN Student AS ST
    ON ST.SNO = SC.SNO
WHERE TA.TNO =
  (SELECT
    TNO
  FROM
    Teacher
  WHERE Tname = '葉平')
  • 8.查詢課程編號“002”的成績比課程編號“001”課程低的所有同學的姓名、學號
SELECT * FROM SC WHERE CNO='002' 
SNO     CNO score
1001    002 90.0
1002    002 68.0
1003    002 84.0
1004    002 35.0
1005    002 87.0
1007    002 89.0
SELECT * FROM SC WHERE CNO='001' 
SNO     CNO score
1001    001 80.0
1002    001 73.0
1003    001 85.0
1004    001 59.0
1005    001 76.0
1006    001 31.0
1007    001 82.0
SELECT
  ST.Sname,
  SC.SNO
FROM
  SC
  JOIN Student AS ST
    ON ST.SNO = SC.SNO
WHERE CNO = '002'
  AND SCORE < ANY
  (SELECT
    SCORE
  FROM
    SC
  WHERE CNO = '001')
  • 9.查詢所有課程成績小於60分的同學的學號、姓名
SELECT DISTINCT ST.SNO,ST.SNAME FROM SC JOIN Student AS ST ON ST.SNO = SC.SNO WHERE SCORE < 60
  • 10.查詢沒有學全所有課的同學的學號、姓名
SELECT ST.SNO,ST.SNAME FROM SC JOIN Student AS ST ON ST.SNO = SC.SNO GROUP BY ST.SNO HAVING COUNT(SC.`CNO`) <
(SELECT COUNT(*) FROM Course)
  • 11.查詢至少有一門與學號為‘1001’同學所學相同的同學的學號和姓名
SELECT DISTINCT ST.SNO,ST.SNAME FROM Student AS ST JOIN SC ON SC.SNO = ST.SNO  WHERE CNO IN (
SELECT CNO FROM SC WHERE SC.`SNO` = '1001' )
  • 12.把“SCORE”表中葉平老師教的課的成績都更改為此課程的平均成績
SELECT AVG(SCORE) FROM SC JOIN Course AS CO ON CO.CNO = SC.CNO JOIN Teacher AS TA ON TA.TNO = CO.TNO
WHERE TA.TNAME = '葉平'
  • 13.查詢和“1002”同學學習的課程完全相同的其他同學學號和姓名
-- 分解成 數目和 CNO都相同
SELECT ST.SNAME,ST.SNO FROM SC JOIN Student AS ST ON SC.SNO = ST.SNO WHERE SC.CNO IN (
SELECT CNO FROM SC WHERE SNO = '1002' ) GROUP BY SNO HAVING COUNT(*) IN (SELECT COUNT(*) FROM SC WHERE SNO = '1002')
  • 14.刪除學習葉平老師可的SC表(做錯了)
-- ERROR
SELECT * FROM SC WHERE CNO = '002'
DELETE  FROM SC JOIN Course AS CO ON CO.CNO = SC.CNO JOIN Teacher AS TA ON TA.TNO = CO.TNO
WHERE TNAME = '葉平' 

SELECT * FROM SC,Course,Teacher WHERE Course.`CNO`=SC.`CNO` AND Course.`TNO` = Teacher.`TNO` AND Tname='葉平'
DELETE  SC FROM Course,Teacher WHERE Course.`CNO`=SC.`CNO` AND Course.`TNO` = Teacher.`TNO` AND Tname='葉平'
UNKNOWN TABLE 'SC' IN MULTI DELETE
-- 先建表  後刪表中資料
CREATE TABLE TMP AS SELECT DISTINCT SC.CNO FROM SC JOIN Course AS CO ON CO.CNO = SC.CNO JOIN Teacher AS TA ON TA.TNO = CO.TNO
WHERE TNAME = '葉平' 
-- SELECT * FROM TMP
DELETE FROM SC WHERE CNO =  (SELECT CNO FROM TMP)
  • 15.查詢不同老師所教不同課程平均分從高到低顯示
SELECT SC.`CNO` AS '課程班級號',CO.CNAME AS '課程名稱',AVG(SC.SCORE)AS '均分',TA.TNAME AS '教師姓名' FROM SC JOIN Course AS CO ON CO.CNO = SC.`CNO` JOIN Teacher AS TA ON TA.TNO = CO.TNO
GROUP BY SC.CNO ORDER BY AVG(SCORE) DESC
  • 16.向SC表插入一些記錄 符合以下要求:沒有上過編號’003’課程的同學學號 2號課程的平均成績
-- note: insert 和 select的巢狀使用
-- 上過003號課程的同學學號 -- 搞定沒上過的同學學號
SELECT DISTINCT SNO FROM SC WHERE SNO NOT IN ( 
SELECT DISTINCT SNO FROM SC WHERE CNO='003')
INSERT SC
SELECT
  SNO,
  '002',
  (SELECT
    AVG(SCORE)
  FROM
    SC
  WHERE CNO = '002'
  GROUP BY CNO)
FROM
  Student
WHERE SNO NOT IN
  (SELECT DISTINCT
    SNO
  FROM
    SC
  WHERE CNO = '003')
  • 17.按平均成績從高到低顯示所有學生的’資料庫‘,”企業管理”,”英語”三門課程成績 按如下形式顯示:
    學生ID,資料庫,英語,有效課程數,有效平均分
SELECT SNO,CNO,SCORE FROM SC WHERE CNO = '004'
SELECT SNO,CNO,SCORE FROM SC WHERE CNO = '001'
SELECT SNO,CNO,SCORE FROM SC WHERE CNO = '006'
-- 有難度題  總結在 查單個的時候 記得要和外面的表進行關聯 否則無法出來結果
SELECT
  SNO AS '學生ID',
  (SELECT
    SCORE
  FROM
    SC
  WHERE CNO = '004' AND SC.SNO = T.SNO) AS '資料庫',
  (SELECT
    SCORE
  FROM
    SC
  WHERE CNO = '001'  AND SC.SNO = T.SNO) AS ' 企業管理 ',
  (SELECT
    SCORE
  FROM
    SC
  WHERE CNO = '006'  AND SC.SNO = T.SNO) AS '英語',
  COUNT(*) AS ' 有效課程數 ',
  AVG(T.SCORE) AS ' 平均成績 '
FROM
  SC AS T
GROUP BY T.SNO
ORDER BY AVG(T.SCORE)
學生ID    資料庫 企業管理    英語    有效課程數     平均成績 
1006    72.0    31.0        \N      3       45.66667
1004    \N      59.0        59.0    3       47.33333
1005    \N      76.0        \N      2       72.50000
1002    69.0    73.0        \N      3       76.66667
1001    69.0    80.0        91.0    5       81.60000
1003    \N      85.0        \N      3       81.66667
1007    93.0    82.0        \N      3       91.00000
  • 18.查詢各科成績最高分和最低分 如下顯示:課程ID,最高分,最低分
 -- 先查最高分和最低分表 再連線起來即可
SELECT CNO,MAX(SCORE) FROM SC GROUP BY CNO
SELECT CNO,MIN(SCORE) FROM SC GROUP BY CNO

SELECT
  SC.CNO AS   '課程ID', 
  MAX_SC AS '最高分',
  MIN_SC AS  '最低分'
FROM
  SC
  JOIN
    (SELECT
      CNO,
      MAX(SCORE) AS MAX_SC
    FROM
      SC
    GROUP BY CNO) AS L
    ON L.CNO = SC.CNO
  JOIN
    (SELECT
      CNO,
      MIN(SCORE) AS MIN_SC
    FROM
      SC
    GROUP BY CNO) AS R
    ON R.CNO = SC.`CNO`
GROUP BY SC.CNO
  • 19.按各科平均成績從低到高和及格率的百分數從高到低排序
-- NVL(), IFNULL() 和 COALESCE() 函式也可以達到相同的結果。將NULL顯示為0
-- case when then else end用法舉例
-- 簡單Case函式  when
-- 及格率查詢 常用函式 case when
有難度題目
CASE sex 
         WHEN '1' THEN '男' 
         WHEN '2' THEN '女' 
ELSE '其他' END 
-- Case搜尋函式 
CASE WHEN sex = '1' THEN '男' 
         WHEN sex = '2' THEN '女' 
ELSE '其他' END 
-- -------------------------------
SELECT COUNT(*),CNO FROM SC GROUP BY CNO
SELECT COALESCE(AVG(SCORE),0) FROM SC GROUP BY CNO ORDER BY AVG(SCORE)
SELECT COUNT(*) FROM SC GROUP BY CNO
-- --------------------------------
SELECT
  CNO AS '科目編號',
  AVG(SCORE) AS '平均分',
  SUM(

    CASE

      WHEN IFNULL(SCORE, 0) >= 60
      THEN 1
      ELSE 0
    END
  ) / COUNT(*) AS '科目及格率'
FROM
  SC
GROUP BY CNO
ORDER BY SUM(

    CASE

      WHEN IFNULL(SCORE, 0) >= 60
      THEN 1
      ELSE 0
    END
  ) / COUNT(*) DESC-- 
科目編號    平均分 科目及格率
005   74.33333   1.0000
004   75.75000   1.0000
002   75.50000   0.8333
001   69.42857   0.7143
003   69.66667   0.6667
006   75.00000   0.5000
SELECT * FROM Course
CNO Cname           TNO
001 企業管理    02
002 馬克思         01
003 OO&UML      04
004 資料庫         03
005 資料分析    06
006 英語          05
  • 20.查詢如下課程平均成績和及格率的百分數
SELECT 
SUM(CASE WHEN SC.CNO = '001' THEN SC.SCORE ELSE 0 END)/SUM(CASE WHEN SC.CNO='001' THEN 1 ELSE 0 END) AS '企業管理平均成績',
SUM(CASE WHEN SC.CNO = '001' AND IFNULL(SC.SCORE,0)>=60 THEN 1 ELSE 0 END)/SUM(CASE WHEN SC.CNO='001' THEN 1 ELSE 0 END)||'%' AS '企業管理及格率',
SUM(CASE WHEN SC.CNO = '002' THEN SC.SCORE ELSE 0 END)/SUM(CASE WHEN SC.CNO='002' THEN 1 ELSE 0 END) AS '馬克思平均成績',
SUM(CASE WHEN SC.CNO = '002' AND IFNULL(SC.SCORE,0)>=60 THEN 1 ELSE 0 END)/SUM(CASE WHEN SC.CNO='002' THEN 1 ELSE 0 END)||'%' AS '馬克思及格率',
SUM(CASE WHEN SC.CNO = '003' THEN SC.SCORE ELSE 0 END)/SUM(CASE WHEN SC.CNO='003' THEN 1 ELSE 0 END) AS 'OO&UML平均成績',
SUM(CASE WHEN SC.CNO = '003' AND IFNULL(SC.SCORE,0)>=60 THEN 1 ELSE 0 END)/SUM(CASE WHEN SC.CNO='003' THEN 1 ELSE 0 END)||'%' AS 'OO&UML及格率',
SUM(CASE WHEN SC.CNO = '004' THEN SC.SCORE ELSE 0 END)/SUM(CASE WHEN SC.CNO='004' THEN 1 ELSE 0 END) AS '資料庫平均成績',
SUM(CASE WHEN SC.CNO = '004' AND IFNULL(SC.SCORE,0)>=60 THEN 1 ELSE 0 END)/SUM(CASE WHEN SC.CNO='004' THEN 1 ELSE 0 END)||'%' AS '資料庫及格率',
SUM(CASE WHEN SC.CNO = '005' THEN SC.SCORE ELSE 0 END)/SUM(CASE WHEN SC.CNO='005' THEN 1 ELSE 0 END) AS '資料分析平均成績',
SUM(CASE WHEN SC.CNO = '005' AND IFNULL(SC.SCORE,0)>=60 THEN 1 ELSE 0 END)/SUM(CASE WHEN SC.CNO='005' THEN 1 ELSE 0 END)||'%' AS '資料分析及格率',
SUM(CASE WHEN SC.CNO = '006' THEN SC.SCORE ELSE 0 END)/SUM(CASE WHEN SC.CNO='006' THEN 1 ELSE 0 END) AS '英語平均成績',
SUM(CASE WHEN SC.CNO = '006' AND IFNULL(SC.SCORE,0)>=60 THEN 1 ELSE 0 END)/SUM(CASE WHEN SC.CNO='006' THEN 1 ELSE 0 END)||'%' AS '英語及格率'
FROM SC

企業管理平均成績    企業管理及格率 馬克思平均成績 馬克思及格率  OO&UML平均成績  OO&UML及格率   資料庫平均成績 資料庫及格率  資料分析平均成績    資料分析及格率 英語平均成績  英語及格率
69.42857        0.7143      75.50000    0.8750      69.66667      0.6667    75.75000    1.0000      74.33333        1.0000      75.00000    0.5000
  • 21.查詢不同老師所教的不同課程平均分從高到低顯示
SELECT MAX(TA.TNO) AS '教師ID',MAX(TA.TNAME) AS '教師姓名',SC.CNO AS '課程ID',MAX(CO.CNAME) AS '課程名稱',IFnullAVG(SC.`score`) AS '平均成績'
FROM
  SC
  JOIN Course AS CO
    ON CO.CNO = SC.CNO
  JOIN Teacher AS TA
    ON TA.TNO = CO.TNO
    GROUP BY SC.`CNO`
     ORDER BY AVG(SC.`score`) DESC

  • 22.查詢如下課程第3名至第6名的學生成績單:企業管理(001) 馬克思(002) OO&UML(003) 資料庫(004) 資料分析(005) 英語(006)
-- 學生ID 學生姓名 企業管理 馬克思 OO&UML 資料庫 資料分析 英語 平均成績
SELECT * FROM SC WHERE CNO = '001' GROUP BY SNO ORDER BY SCORE DESC 
SELECT * FROM SC WHERE CNO = '002' GROUP BY SNO ORDER BY SCORE DESC 
SELECT * FROM SC WHERE CNO = '003' GROUP BY SNO ORDER BY SCORE DESC 
SELECT * FROM SC WHERE CNO = '004' GROUP BY SNO ORDER BY SCORE DESC 
SELECT * FROM SC WHERE CNO = '005' GROUP BY SNO ORDER BY SCORE DESC 
SELECT * FROM SC WHERE CNO = '006' GROUP BY SNO ORDER BY SCORE DESC 

SELECT
  SC.SNO AS '學生ID',
  ST.`Sname` AS '學生姓名',
  A_001.SCORE AS '企業管理',
  A_002.SCORE AS '馬克思',
  A_003.SCORE AS 'OO&UML',
  A_004.SCORE AS '資料庫',
  A_005.SCORE AS '資料分析',
  A_006.SCORE AS '英語',
  AVG(SC.SCORE) AS '平均分'
FROM
  SC
  JOIN Student AS ST
    ON ST.SNO = SC.SNO
  LEFT JOIN
    (SELECT
      *
    FROM
      SC
    WHERE CNO = '001'
    GROUP BY SNO
    ORDER BY SCORE DESC) AS A_001
    ON A_001.SNO = SC.SNO
  LEFT JOIN
    (SELECT
      *
    FROM
      SC
    WHERE CNO = '002'
    GROUP BY SNO
    ORDER BY SCORE DESC) AS A_002
    ON A_002.SNO = SC.SNO
  LEFT JOIN
    (SELECT
      *
    FROM
      SC
    WHERE CNO = '003'
    GROUP BY SNO
    ORDER BY SCORE DESC) AS A_003
    ON A_003.SNO = SC.SNO
  LEFT JOIN
    (SELECT
      *
    FROM
      SC
    WHERE CNO = '004'
    GROUP BY SNO
    ORDER BY SCORE DESC) AS A_004
    ON A_004.SNO = SC.SNO
  LEFT JOIN
    (SELECT
      *
    FROM
      SC
    WHERE CNO = '005'
    GROUP BY SNO
    ORDER BY SCORE DESC) AS A_005
    ON A_005.SNO = SC.SNO
  LEFT JOIN
    (SELECT
      *
    FROM
      SC
    WHERE CNO = '006'
    GROUP BY SNO
    ORDER BY SCORE DESC) AS A_006
    ON A_006.SNO = SC.SNO
GROUP BY SC.SNO
  • 23.統計列印各科成績,各分數段人數:課程ID,課程名稱[100-85][85-70][70-60][<60]
SELECT CNO,COUNT(*) FROM SC WHERE SCORE >= 85 GROUP BY CNO
SELECT CNO,COUNT(*) FROM SC WHERE SCORE < 85  AND SCORE >= 70 GROUP BY CNO
SELECT CNO,COUNT(*) FROM SC WHERE SCORE < 70  AND SCORE >= 60 GROUP BY CNO
SELECT CNO,COUNT(*) FROM SC WHERE SCORE < 60 GROUP BY CNO

SELECT  SC.`CNO`,CO.`Cname`,SC85.NUM AS '[100-85]',SC70.NUM AS '[85-70]',SC60.NUM AS '[70-60]',SCL60.NUM AS '[<60]' FROM SC JOIN Course AS CO ON CO.CNO = SC.CNO LEFT JOIN (SELECT CNO,COUNT(*) AS NUM FROM SC WHERE SCORE >= 85 GROUP BY CNO
) AS SC85 ON SC.CNO = SC85.CNO LEFT JOIN (SELECT CNO,COUNT(*)AS NUM FROM SC WHERE SCORE < 85  AND SCORE >= 70 GROUP BY CNO
) AS SC70 ON SC.CNO = SC70.CNO LEFT JOIN (SELECT CNO,COUNT(*)AS NUM  FROM SC WHERE SCORE < 70  AND SCORE >= 60 GROUP BY CNO
) AS SC60 ON SC.CNO = SC60.CNO LEFT JOIN (SELECT CNO,COUNT(*)AS NUM FROM SC WHERE SCORE < 60 GROUP BY CNO
) AS SCL60 ON SC.CNO = SCL60.CNO  GROUP BY SC.CNO

CNO Cname   [100-85]    [85-70] [70-60] [<60]
001 企業管理    1   4   \N  2
002 馬克思     3   1   1   1
003 OO&UML      3   1   \N  2
004