🤔 문제 분석
1. Ketty gives Eve a task to generate a report containing three columns: Name, Grade and Mark.
2. Ketty doesn't want the NAMES of those students who received a grade lower than 8.
3. The report must be in descending order by grade -- i.e. higher grades are entered first.
If there is more than one student with the same grade (8-10) assigned to them, order those particular students by their name alphabetically.
4. Finally, if the grade is lower than 8, use "NULL" as their name and list them by their grades in descending order.
If there is more than one student with the same grade (1-7) assigned to them, order those particular students by their marks in ascending order.
💡 풀이
SELECT S.Name AS name, G.Grade AS grade, S.Marks AS marks
FROM Students AS S
INNER JOIN Grades AS G ON S.Marks BETWEEN G.Min_Mark AND G.Max_Mark AND G.Grade >= 8
UNION
SELECT NULL AS name, G.Grade AS grade, S.Marks AS marks
FROM Students AS S
INNER JOIN Grades AS G ON S.Marks BETWEEN G.Min_Mark AND G.Max_Mark
WHERE G.Grade < 8
ORDER BY Grade DESC, Name, Marks
UNION을 사용하면 먼저 나온 쿼리의 order절만 유효하고
두번째 쿼리의 order 절은 무효처리 된다.
'코딩테스트 > 해커랭크 SQL' 카테고리의 다른 글
[해커랭크/SQL] 39. Top Competitors (0) | 2023.03.17 |
---|---|
[해커랭크/SQL] 38. Contest Leaderboard (0) | 2023.03.17 |
[해커랭크/SQL] 36. Weather Observation Station 18 (0) | 2023.03.16 |
[해커랭크/SQL] 35. Binary Tree Nodes (0) | 2023.03.14 |
[해커랭크/SQL] 34. Occupations(피봇테이블, 윈도우함수) (0) | 2023.03.14 |
댓글