You are given two tables: Students andGrades. Students contains three columnsID,NameandMarks.
Gradescontains the following data:
KettygivesEvea task to generate a report containing three columns:Name,GradeandMark.Kettydoesn't want the NAMES of those students who received a grade lower than8. 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.
Sample Input
Sample Output
Maria 10 99
Jane 9 81
Julia 9 88
Scarlet 8 78
NULL 7 63
NULL 7 68
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 got8,9or10grades:
Maria (grade 10)
Jane (grade 9)
Julia (grade 9)
Scarlet (grade 8)
🔉 문제 설명
8점보다 낮은 학생 제외
등급으로 내림차순으로 작성(등급이 높은 학생부터)
같은 학년을 받은 학생이 두 명 이상인 경우 알파벳순으로 정렬
마지막 등급이 8미만인 경우 "NULL"을 이름으로 사용하고 등급별로 내림차순으로 나열
같은 등급을 가진 학생이 둘 이상인 경우 점수별로 오름차순으로 정
❗답
SELECT
CASE
WHEN g.grade < 8 THEN null
ELSE s.name
END AS g,
g.grade, s.marks
FROM students s join grades g ON s.marks BETWEEN g.min_mark AND g.max_mark
ORDER BY g.grade DESC, s.name, s.marks
🤔풀이
FROM students s join grades g ON s.marks BETWEEN g.min_mark AND g.max_mark