[HackerRank] The Report(Non-Equi Join 활용)

생각하는 마리오네트·2021년 9월 20일
0

SQL

목록 보기
15/39

문제 설명

You are given two tables: Students and Grades. Students contains three columns ID, Name and Marks.

Students

ColumnType
IDInteger
NameString
MarksInteger

Grades

GradeMin_MarkMax_Mark
109
21019
32029
43039
54049
65059
76069
87079
98089
1090100

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.

Note

Print "NULL" as the name if the grade is less than 8.

Explanation

Consider the following table with the grades assigned to the students:

So, the following students got 8, 9 or 10 grades:

Maria (grade 10)
Jane (grade 9)
Julia (grade 9)
Scarlet (grade 8)

어려웠던점

  1. 중복되는 여러 조건이 따로 따로 언급되어서 처음에 당황을 했고, 노트에 하나하나 적으면서 중복되는 조건들을 정리하여 풀었다.
  2. Non-Equi Join 문제를 처음 마주쳐서 같은 것이 없는것끼리 어떻게 합쳐야할지 몰라서 당황했다.
  3. 조건문 활용이 아직 미숙하다는것을 알게되었다.

문제를 해결해 나간 과정

실제로 적어가면서 풀었던것을 깨끗하게 옮겨적지 않고 있는그대로를 캡쳐했다.
(나중에는 이땐 이랬었지....하기위함 ㅎㅎ)

정답

SELECT CASE WHEN G.Grade < 8 THEN NULL ELSE S.NAME END, G.Grade, S.Marks 
FROM STUDENTS S 
INNER JOIN GRADES G ON S.Marks BETWEEN G.Min_Mark AND G.Max_Mark
ORDER BY  G.GRADE DESC, S.NAME, S.MARKS

추가로 이후 학습해야할것
1. Non-Equi Join 와 Equi Join
2. 조건절 활용하기(IF/ CASE)

profile
문제를해결하는도구로서의"데이터"

0개의 댓글