- Today
- Total
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- 연관분석
- ERROR: install is not COMMAND nor fully qualified CLASSNAME.
- pre-course
- 프로그래머스
- MySQL
- gpt-api에러
- 코딩테스트
- 쉽게 배우는 데이터 통신과 컴퓨터 네트워크
- 쉽게 배우는 데이터 통신과 컴퓨터 네트워크 답지
- python
- 부스트캠프
- HackerRank
- 딥러닝 개요
- inner join
- 역전파
- 행렬
- NumPy
- map
- npm install -g yarn 에러
- pandas
- gpt-api
- 프로그래머스 SQL
- 부스트캠프ai
- 딥러닝 역사
- 깃
- yarn 설치 에러
- 컴퓨터통신
- sql
- 컴퓨터 통신
- TabNet
최말짱 블로그
[HackerRank] The Report(mysql풀이) 본문
나에게 꽤나 어려웠던 문제..
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


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 테이블이 있다.
Grades.Min_Mark < Students.marks < Grades.Max_Mark 범위 사이에 해당하는 grade를 찾아 ID, Marks와 함께 출력해주면 된다.
주의
1. grade가 8 이하인 경우 ID를 Null로 표시해준다.
2. 등급이 높은 순으로 정렬하고, 등급이 같을 경우 이름의 알파벳순으로(abcd..) 정렬한다.
<정답>
select IF(g.grade < 8, 'NULL', s.name), 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;
등급이 8이하일 때 name을 null을 출력해야 한다는 조건에서 case문을 써야하나 고민했었는데(할 순 있을 듯 BUT 길어짐), 단순하게 IF문을 이용하여 출력할 수 있었다.
알게된 것
1. select에 if문 쓸 수 있다. if(조건,True일때,False일때)
2. inner join을 할 때 꼭 같다(equl) 조건으로 동일 컬럼을 찾지 않아도 된다 ! 그냥 비교하는 용도로 inner join 해도 됨.
해당 문제도 students와 grades에 같은 컬럼이 있지 않았는데, inner join의 조건으로 s.marks between g.min_mark and g.max_mark를 쓰면서 학생이 해당하는 등급을 찾을 수 있었다.
3. order by 할 때
order by 첫 번째 order조건 order방법, 두번째 order조건, order 방법(asc는 생략가능) 으로 표시한다.
'HackerRank SQL' 카테고리의 다른 글
[프로그래머스]식품분류별 가장 비싼 식품의 정보 조회하기(MYSQL풀이) (0) | 2023.03.01 |
---|---|
[프로그래머스]조건에 맞는 도서와 저자 리스트 출력하기(mysql풀이) (0) | 2023.02.24 |
[프로그래머스]입양 시각 구하기(2)(mysql풀이) (0) | 2023.02.24 |
[HackerRank]Top Competitors(mysql풀이) (0) | 2022.07.12 |