문제 설명
You are given two tables: Students and Grades. Students contains three columns ID, Name and Marks.
Students
Column | Type |
---|---|
ID | Integer |
Name | String |
Marks | Integer |
Grades
Grade | Min_Mark | Max_Mark |
---|---|---|
1 | 0 | 9 |
2 | 10 | 19 |
3 | 20 | 29 |
4 | 30 | 39 |
5 | 40 | 49 |
6 | 50 | 59 |
7 | 60 | 69 |
8 | 70 | 79 |
9 | 80 | 89 |
10 | 90 | 100 |
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)
어려웠던점
문제를 해결해 나간 과정
실제로 적어가면서 풀었던것을 깨끗하게 옮겨적지 않고 있는그대로를 캡쳐했다.
(나중에는 이땐 이랬었지....하기위함 ㅎㅎ)
정답
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)