문제
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.
케티는 이브에게 Name, Grade, Mark 컬럼이 포함된 보고서 만드는 일을 맡겼습니다. 케티는 성적이 8 이하인 학생들 이름이 공개되기 원하지 않았습니다. 보고서는 성적순으로 내림차순 정렬 되어야 합니다. - 즉 높은 등급이 먼저 입력 되어야하죠. 만약 성적이 (8-10)인 학생이 여럿 있다면 그 학생들은 알파벳 수느로 정렬하세요. 마지막으로 성적이 8 보다 낮으면 이름을 NULL로 하고 성적순으로 내림차순 정력하세요. 반약 성적(1 - 7)이 같은 학생이 있다면 해당학생들을 점수 오름차순 하세요.
Students
Grades
SELECT IF(Grade < 8, NULL, NAME) , Grade, Marks
FROM Students S
JOIN Grades G ON S.Marks >= Min_Mark AND S.Marks <= Max_Mark
ORDER BY GRADE DESC, NAME
정답은 다만 쉬웠습니다. 다만 저는 JOIN이 이렇게 쓰일수도 있는지 몰랐어요. 보통 Primary 키를 중심으로 활용하는데 이런 연산자도 쓰일 줄 몰랐습니다. 문제는 쉬웠습니다. 다만 이러면 가시적이라 할 수는 없습니다. 정렬기준이 다양하기 때문인데요 그래서 쿼리르 조금더 업그레이드 했습니다.
SELECT
IF(Grade < 8, NULL, NAME) AS Name,
Grade,
Marks
FROM Students S
JOIN Grades G ON S.Marks >= Min_Mark AND S.Marks <= Max_Mark
ORDER BY
Grade DESC,
CASE
WHEN Grade >= 8 THEN NAME
ELSE NULL
END,
CASE
WHEN Grade < 8 THEN Marks
ELSE NULL
END
CASE WHEN을 정렬구문에 쓴 적은 처음이네요. 함수가 어떤 절에 쓰일지는 무궁무진 인거 같네요
'MYSQL > HakerRank_Medium' 카테고리의 다른 글
Ollivander's Inventory와 서브쿼리 (0) | 2025.04.18 |
---|---|
Top Competitors (0) | 2025.04.17 |
Weather Observation Station 20(중앙값 구하기) (0) | 2025.04.16 |
Weather Observation Station 19, 유클리드 거리 구하기 (0) | 2025.04.16 |
Weather Observation Station 18 (0) | 2025.04.15 |