https://leetcode.com/problems/percentage-of-users-attended-a-contest/?envType=study-plan-v2&envId=top-sql-50
🗒️SQL 코드 풀이
SELECT R.contest_id,
ROUND(COUNT(DISTINCT R.user_id)/(SELECT COUNT(DISTINCT user_id)FROM Users)*100 ,2) percentage
FROM
Register R
GROUP BY
contest_id
ORDER BY
percentage DESC, contest_id
1. 메인 테이블은 Register의 테이블이다.
2. Register 테이블을 contest_id로 그룹화를 해주고, 문제 조건에 맞게 ORDER BY를 해준다.
3. 스칼라 서브쿼리를 사용해서 User가 총 몇 명인지 파악 후 문제 요구 사항대로 식을 작성한다.
📌 문제 코멘트
문제 자체는 어렵지 않았고, 포스팅의 목적은 스칼라 서브쿼리 !!
- 스칼라 서브쿼리가 뭔지 몰랐다.
- 전체 User 수를 어떻게 구해야할지에 막혔다.
→ 이렇게 행의 개수를 파악해야 하구나 !
📚문제
'♟️ 알고리즘 > Leetcode' 카테고리의 다른 글
[MySQL][Leet Code] 1251. Average Selling Price (Easy) (0) | 2025.02.18 |
---|---|
[MySQL][Leet Code] 1934. Confirmation Rate (Medium) (0) | 2025.02.18 |
[MySQL][Leet Code] 1280. Students and Examinations (Easy) (0) | 2025.02.17 |
[MySQL][Leet Code] 197. Rising Temperature (Easy) (0) | 2025.02.17 |
[MySQL][Leet Code] 1581. Customer Who Visited but Did Not Make Any Transactions (Easy) (0) | 2025.02.17 |