HackerRank SQL #4

1. Ollivander’s Inventory

1.1 SQL Server

with join_tables as (
    select
        id,
        age,
        coins_needed,
        power
    from wands
    inner join wands_property
        on wands_property.code = wands.code
    where is_evil = 0
),

agg_price as (
    select
        id,
        age,
        power,
        MIN(coins_needed) as min_price
 from join_tables
 group by id, power, age
),

dedup_step as (
    select
        id,
        age,
        min_price,
        power,
        row_number() over(
            partition by age, power order by min_price asc
        ) as rownumber
 from agg_price
)

select
    id,
    age,
    min_price,
    power
from dedup_step
where rownumber = 1
order by power desc, age desc