본문 바로가기
  • KEEP HUSTLE!
MYSQL 코테 준비

[HacKerRank SQL] The Report - MYSQL

by 하수군 2021. 2. 24.

Students 테이블
Grades 테이블

 

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.

 

 

아래의 조건으로 이름과 등급 그리고 점수 조회하기

- 전체적으로 등급 내림차순으로 정렬

- 8등급 이상이면 알파벳순으로 정렬

- 8등급 미만이면 이름 NULL로 표시 + 점수 오름차순으로 정렬

SELECT 
    IF (G.Grade >= 8, S.Name, NULL),
    G.Grade AS GD,
    S.Marks
FROM 
    Students AS S,
    Grades AS G 
WHERE S.Marks BETWEEN G.Min_Mark AND G.Max_Mark
ORDER BY 
    GD DESC,
    CASE 
        WHEN GD >= 8 THEN S.Name
        WHEN GD < 8 THEN S.Marks
    END ASC

댓글