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

 

Students

 

Grades contains the following data

 

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.

 

Students 테이블과 Grades 테이블을 활용하여 학생별 Marks를 Grade 등급 구간별로 구분한 후, 출력하면 되는 문제이다.

이 문제를 풀면서 join문에 따로 조건절을 넣어서 사용할 수 있다는 걸 처음 알았다..! 항상 공통적으로 가지고 있는 외래키 연결하는 용도로만 사용했어서.. 덕분에 지식 하나 습득했다!

 

-

 

 

풀이 코드

students의 marks 점수를 grades테이블을 활용해서 구간별로 구분해주면 된다.

s.marks between g.min_mark and g.max_mark < marks가 min_mark, max_mark 구간에 따라 매치되면서 본인과 맞는 grade를 부여받는다.

 

 

+ Recent posts