Snoopy [해커랭크/SQL] 37. The Report
코딩테스트/해커랭크 SQL

[해커랭크/SQL] 37. The Report

Sooyoon Jeong 2023. 3. 16.

🤔 문제 분석

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 절은 무효처리 된다.

댓글