1. 程式人生 > >LeetCode.596. 超過5名學生的課

LeetCode.596. 超過5名學生的課

在這裡插入圖片描述

思路1:
即按課程分組,由於有重複資料,按學生計數時需要使用distinct.

SELECT CLASS class
FROM COURSES
GROUP BY CLASS
HAVING COUNT(DISTINCT STUDENT) >= 5

分析1:

此程式碼是我最初的思路,也是LeetCode記錄的最優解。但是在最近增加了一個極大的測試用例導致超時。

思路2:

二次選擇。是第一種思路的一種改寫。

程式碼2:

select class 
from (
    select class,count(distinct student) as num
    from courses
    group by class
) as c
where num>=5;