
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.
💻 학생별 Grade 구한 후, Grade 8 아래학생의 이름은 마스킹 처리
(Grade 내림차순, 학생 이름 오름차순)
SELECT CASE WHEN a02.grade < 8 THEN NULL
ELSE a01.name END new_grade
,a02.grade
,a01.marks
FROM students a01
LEFT JOIN
(
SELECT *
FROM grades
)a02 ON a01.marks BETWEEN a02.min_mark AND a02.max_mark
ORDER BY a02.grade DESC, a01.name ASC
;
✍ LEFT JOIN 시 'BETWEEN AND' 문 사용
✍ CASE WHEN 문을 사용하여, 특정 조건에서 NULL 처리