hackerrank - The Report(틀림)

르네·2023년 10월 24일
0

SQL

목록 보기
46/63

문제

https://www.hackerrank.com/challenges/the-report/problem?isFullScreen=true

Ketty gives Eve a task to generate a report containing three columns: Name, Grade and Mark. Ketty doesn't want the NAMES of those students who received a grade lower than 8. 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. 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.

Write a query to help Eve.

풀이

SELECT CASE 
            WHEN grade >= 8 THEN s.name 
       END as name
     , g.grade
     , s.marks
FROM students AS s
    LEFT JOIN grades AS g ON s.marks between g.min_mark and g.max_mark
ORDER BY grade DESC
       , name
       , s.marks

배운점

  • INNER JOIN과 CASE문만 떠올려 계속 헤맸다. LEFT JOIN을 쓰면 풀 수 있는 문제
  • LEFT JOIN 문법에서 ON에 꼭 'A.abc = B.abc'일 필요 없었다. BETWEEN도 활용할 수 있었다! 이게 포인트!
profile
데이터분석 공부로그

0개의 댓글