Try   HackMD

596. Classes More Than 5 Students

(資訊來自於leetcode 596 Classes More Than 5 Students)

超過 5 名學生的班級

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

編寫一個 SQL 查詢來報告所有至少有五個學生的班級。

以任意順序返回結果表。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

解題方式:

select class from Courses group by class having count(*)>=5

解題解析:

要取得至少有5個學生的班級,這邊我們就會用到聚合函數,但是WHERE不能搭配聚合函數,因此我們這邊就需要用HAVING子句,使用在GROUP BY 後面。
(資訊來自於 HAVING 子句 (SQL HAVING Clause)