[HackerRank] Ollivander's Inventory

당당·2023년 7월 19일
0

HackerRank

목록 보기
16/27

https://www.hackerrank.com/challenges/harry-potter-and-wands/problem

📔문제

Harry Potter and his friends are at Ollivander's with Ron, finally replacing Charlie's old broken wand.

Hermione decides the best way to choose is by determining the minimum number of gold galleons needed to buy each non-evil wand of high power and age. Write a query to print the id, age, coins_needed, and power of the wands that Ron's interested in, sorted in order of descending power. If more than one wand has same power, sort the result in order of descending age.

The following tables contain data on the wands in Ollivander's inventory:

Wands: The id is the id of the wand, code is the code of the wand, coins_needed is the total number of gold galleons needed to buy the wand, and power denotes the quality of the wand (the higher the power, the better the wand is).

Wands_Property: The code is the code of the wand, age is the age of the wand, and is_evil denotes whether the wand is good for the dark arts. If the value of is_evil is 0, it means that the wand is not evil. The mapping between code and age is one-one, meaning that if there are two pairs, (code1,age1) and (code2,age2), then code1!=code2 and age1!=age2.


📝예시

Wands Table:

Wands_Property Table:

9 45 1647 10
12 17 9897 10
1 20 3688 8
15 40 6018 7
19 20 7651 6
11 40 7587 5
10 20 504 5
18 40 3312 3
20 17 5689 3
5 45 6020 2
14 40 5408 1


🧮분야

  • JOIN

📃SQL 코드

select id,age,coins_needed, power
from wands w, wands_property p
where w.code=p.code
and p.is_evil=0
and (w.code,coins_needed,power) in (select code, min(coins_needed),power
                                   from wands
                                   group by code, power)
order by power desc, age desc;

📰출력 결과


📂고찰

같은 코드와 파워최소금액으로 구매할 수 있는 것만 출력해야한다.

그러므로 그것을 그룹핑한 후, 코드랑 파워랑 필요코인이 해당 쿼리문과 같은 것을 출력하면 된다.

profile
MySQL DBA 신입

1개의 댓글

comment-user-thumbnail
2023년 7월 19일

많은 도움이 되었습니다, 감사합니다.

답글 달기