데이터 리터러시를 위한 발자취

[HackerRank] Ollivander's Inventory 본문

데이터 분석/SQL

[HackerRank] Ollivander's Inventory

wosole 2023. 12. 26. 15:25

 

Ollivander's Inventory

문제

- print the id, age, coins_needed, power of wands.

- minimum number of gold galleons.

- non evil wand of high power and age.

- sorted in order of descending power. 


문제풀이

접근방식은 아래와 같이 정리하여 진행했습니다.

 

1. 테이블 조인

- Wands, Wands_Property 테이블 간 조인 수행

 

2. non evil 조건 조회

- WHERE절 기반 'is_evil' 이 '0'인 조건 조회

 

3. 셀프조인을 통해 coins_needed가 적은 조건 조회

- 2번 절차 중 AND 조건을 통해 coins_needed의 MIN 값 구하기

 

4. 결과는 아래 조건에 맞게 정렬

- power 기준으로 내림차순

- 동일한 power가 있을 경우, age 기준으로 내림차순


제출코드

# 제출코드
SELECT id, age, coins_needed, power
FROM Wands W
JOIN Wands_Property WP ON W.code = WP.code 
WHERE is_evil = 0 AND
    coins_needed = (
    SELECT MIN(W2.coins_needed)
    FROM Wands W2
    WHERE W2.power = W.power AND W2.code = W.code)
ORDER BY power DESC, age DESC

 

Ollivander's Inventory | HackerRank

Help pick out Ron's new wand.

www.hackerrank.com

Comments