Practice > SQL > Basic Join > The Report

Problem

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.
문제링크

Answer

SELECT CASE WHEN g.grade < 8 THEN NULL ELSE s.name END AS Name 
	-- SELECT에서도 CASE문을 쓰는 것에 익숙해져야함.
     , g.grade
     , s.marks
FROM students as s
    INNER JOIN grades as g ON s.marks between g.min_mark and g.max_mark 
    -- INNER JOIN에서 ON에 조건을 쓸 때 BETWEEN 을 쓸 수 있다.
ORDER BY g.grade DESC, s.name, s.marks
-- ORDER BY에서 디폴트 값은 ASC로 되기 때문에 굳이 쓸 필요 없음

0개의 댓글