計算各大學的 affiliation 數量
現在資料已經可以分析了,來對資料庫執行一些範例 SQL 查詢吧。你會用到已學過的觀念,例如依欄位分組與連接資料表。
在這題中,你要找出哪一所大學擁有最多的 affiliations(透過其教授)。為此,你需要 affiliations 與 professors 兩個資料表,因為後者也包含 university_id。
快速複習一下,JOIN 的基本結構如下:
SELECT table_a.column1, table_a.column2, table_b.column1, ...
FROM table_a
JOIN table_b
ON table_a.column = table_b.column
這會把 table_a 與 table_b 的資料結合,但只包含 table_a.column 等於 table_b.column 的列。
本練習屬於課程
SQL 關聯式資料庫入門
練習說明
- 計算每所大學的總 affiliation 數量。
- 依該計數做遞減排序。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
-- Count the total number of affiliations per university
SELECT ___(*), professors.university_id
FROM ___
JOIN ___
ON affiliations.professor_id = professors.id
-- Group by the university ids of professors
GROUP BY professors.___
___ ___ count DESC;