sql语句练习(二):Demand
生活随笔
收集整理的這篇文章主要介紹了
sql语句练习(二):Demand
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
成績表t_score——學生編號(stu_id),課程編號(lesson_id),學生成績(score)
學生表t_student——學生編號(stu_id),學生姓名(stu_name),性別(gender),年齡(age),班級編號(class_id)
課程表t_lesson——課程編號(lesson_id),課程名稱(lesson_name)
以下為數據庫demand中的三張表,其結構和內容(部分)如下:
1. 查詢學生沒有參加考試的課程。(顯示形式:學生、姓名、班級、課程)
知識點:NOT EXISTS關鍵字、內部聯結
思路:理想情況下,所有學生參加了所有考試(笛卡爾積);現實情況是,部分學生參加了部分考試。使用NOT EXISTS關鍵字求差集。
2. 查找每門課程的前3名。(顯示形式:課程、第一名(姓名+分數) 、第二名(姓名+分數))
知識點:窗口函數中的序號函數(RANK())、Concat()拼接函數、自聯結
# ① 按課程對成績排名 mysql> SELECT stu_name, lesson_name, score, RANK() OVER (PARTITION BY a.lesson_id ORDER BY score DESC) score_order-> FROM t_score a-> INNER JOIN t_stu_profile b ON a.stu_id = b.stu_id-> INNER JOIN t_lesson c ON a.lesson_id = c.lesson_id; +----------+-------------+-------+-------------+ | stu_name | lesson_name | score | score_order | +----------+-------------+-------+-------------+ | 張北 | 語文 | 99 | 1 | | 錢南 | 語文 | 98 | 2 | | 郭東 | 語文 | 90 | 3 | | 李西 | 語文 | 84 | 4 | | 郭東 | 數據 | 86 | 1 | | 李西 | 數據 | 77 | 2 | | 張北 | 數據 | 76 | 3 | | 郭東 | 英語 | 89 | 1 | | 張北 | 英語 | 85 | 2 | | 李西 | 英語 | 69 | 3 | | 張北 | 物理 | 78 | 1 | | 李西 | 物理 | 75 | 2 | | 錢南 | 化學 | 98 | 1 | +----------+-------------+-------+-------------+# ② 自聯結 mysql> SELECT t1.lesson_name '課程',-> CONCAT(t1.stu_name, '+', t1.score) AS '第一名',-> CONCAT(t2.stu_name, '+', t2.score) AS '第二名',-> CONCAT(t3.stu_name, '+', t3.score) AS '第三名'-> FROM-> (SELECT stu_name, lesson_name, score, RANK() OVER (PARTITION BY a.lesson_id ORDER BY score DESC) score_order FROM t_score a INNER JOIN t_stu_profile b ON a.stu_id = b.stu_id INNER JOIN t_lesson c ON a.lesson_id = c.lesson_id)t1,-> (SELECT stu_name, lesson_name, score, RANK() OVER (PARTITION BY a.lesson_id ORDER BY score DESC) score_order FROM t_score a INNER JOIN t_stu_profile b ON a.stu_id = b.stu_id INNER JOIN t_lesson c ON a.lesson_id = c.lesson_id)t2,-> (SELECT stu_name, lesson_name, score, RANK() OVER (PARTITION BY a.lesson_id ORDER BY score DESC) score_order FROM t_score a INNER JOIN t_stu_profile b ON a.stu_id = b.stu_id INNER JOIN t_lesson c ON a.lesson_id = c.lesson_id)t3-> WHERE t1.score_order = 1 AND t2.score_order = 2 AND t3.score_order = 3-> AND t1.lesson_name = t2.lesson_name AND t2.lesson_name = t3.lesson_name; +--------+-----------+-----------+-----------+ | 課程 | 第一名 | 第二名 | 第三名 | +--------+-----------+-----------+-----------+ | 語文 | 張北+99 | 錢南+98 | 郭東+90 | | 數據 | 郭東+86 | 李西+77 | 張北+76 | | 英語 | 郭東+89 | 張北+85 | 李西+69 | +--------+-----------+-----------+-----------+3. 檢索0611班所有男生的成績。(顯示形式:姓名、語文、數學、英語、物理、化學、總分)
知識點:內部聯結、CASE WHEN 條件 THEN... ELSE... END、SUM()函數、GROUP BY子句
# ① 篩選符合條件的學生信息(0611班、男生) mysql> SELECT stu_name, lesson_name, score-> FROM t_score a-> INNER JOIN t_stu_profile b ON a.stu_id = b.stu_id-> INNER JOIN t_lesson c ON a.lesson_id = c.lesson_id-> WHERE class_id = '0611' AND gender = 'M'; +----------+-------------+-------+ | stu_name | lesson_name | score | +----------+-------------+-------+ | 錢南 | 語文 | 98 | | 錢南 | 化學 | 98 | +----------+-------------+-------+# ② 測試語句(CASE) mysql> SELECT stu_name,-> (CASE WHEN lesson_name = '語文' THEN score ELSE 0 END) chinese,-> (CASE WHEN lesson_name = '數學' THEN score ELSE 0 END) math,-> (CASE WHEN lesson_name = '英語' THEN score ELSE 0 END) english,-> (CASE WHEN lesson_name = '物理' THEN score ELSE 0 END) physics,-> (CASE WHEN lesson_name = '化學' THEN score ELSE 0 END) chemistry-> FROM (-> SELECT stu_name, lesson_name, score-> FROM t_score a-> INNER JOIN t_stu_profile b ON a.stu_id = b.stu_id-> INNER JOIN t_lesson c ON a.lesson_id = c.lesson_id-> WHERE class_id = '0611' AND gender = 'M')t; +----------+---------+------+---------+---------+-----------+ | stu_name | chinese | math | english | physics | chemistry | +----------+---------+------+---------+---------+-----------+ | 錢南 | 98 | 0 | 0 | 0 | 0 | | 錢南 | 0 | 0 | 0 | 0 | 98 | +----------+---------+------+---------+---------+-----------+# 最終代碼 mysql> SELECT stu_name '姓名',-> SUM(CASE WHEN lesson_name = '語文' THEN score ELSE 0 END) '語文',-> SUM(CASE WHEN lesson_name = '數學' THEN score ELSE 0 END) '數學',-> SUM(CASE WHEN lesson_name = '英語' THEN score ELSE 0 END) '英語',-> SUM(CASE WHEN lesson_name = '物理' THEN score ELSE 0 END) '物理',-> SUM(CASE WHEN lesson_name = '化學' THEN score ELSE 0 END) '化學'-> FROM (-> SELECT stu_name, lesson_name, score-> FROM t_score a-> INNER JOIN t_stu_profile b ON a.stu_id = b.stu_id-> INNER JOIN t_lesson c ON a.lesson_id = c.lesson_id-> WHERE class_id = '0611' AND gender = 'M')t-> GROUP BY stu_name-> ; +--------+--------+--------+--------+--------+--------+ | 姓名 | 語文 | 數學 | 英語 | 物理 | 化學 | +--------+--------+--------+--------+--------+--------+ | 錢南 | 98 | 0 | 0 | 0 | 98 | +--------+--------+--------+--------+--------+--------+總結
以上是生活随笔為你收集整理的sql语句练习(二):Demand的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sql语句练习(一)
- 下一篇: 属性总结(一):marker