CodingSpace

[HackerRank/SQL] Basic Join - The Report 본문

HackerRank/SQL

[HackerRank/SQL] Basic Join - The Report

개발자_조이킴 2022. 6. 27. 23:39

Problem. Basic Join - The Report


Link.

https://www.hackerrank.com/challenges/the-report/problem

 

The Report | HackerRank

Write a query to generate a report containing three columns: Name, Grade and Mark.

www.hackerrank.com


Description.

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

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.

 

STUDENTS, GRADES 테이블을 사용하여 학생의 이름(NAME), 등급(GRADE), 점수(MARKS)를 조회하시오.

이때 8등급 보다 낮은 등급(1-7)을 가진 학생의 이름은 NULL로 처리하고, 등급이 높은 순으로 정렬하시오.

단 8등급 이상의 학생들의 경우 등급이 같은 경우 이름을 알파벳 순으로 정렬하고,

8등급 미만의 학생들의 경우 등급이 같은 경우 점수가 낮은순으로 정렬하시오.

 

Students 테이블
Grades 테이블


Key Point. 

IF,

 

JOIN,

 

BETWEEN,


My Answer. 

SELECT 
    IF(GRADE < 8, NULL, NAME), 
    G.Grade, 
    S.Marks
FROM Students S
JOIN Grades G
WHERE S.MARKS BETWEEN G.MIN_MARK AND G.MAX_MARK
ORDER BY GRADE DESC, NAME

References. 

 

Comments