Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions 01-MarketAnalysis-II.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- Problem 1: Market Analysis II (https://leetcode.com/problems/market-analysis-ii/)

with cte as(
select seller_id, item_id, item_brand from (
select seller_id, o.item_id as item_id, item_brand, order_date, rank() over (partition by seller_id order by order_date) as rnk
from orders o
join items i on o.item_id = i.item_id) as temp
where rnk = 2)


select user_id as seller_id,
(case
when item_brand = favorite_brand then 'yes'
else 'no'
end) as '2nd_item_fav_brand'
from users
left join cte on users.user_id = cte.seller_id







25 changes: 25 additions & 0 deletions 02-TournamentWinners.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- Problem 2: Tournament Winners: https://leetcode.com/problems/tournament-winners/description/


with cte as (
select player_id, sum(score) as score from (
select first_player as player_id , first_score as score
from matches
union all
select second_player as player_id , second_score as score
from matches
) as temp
group by player_id),
cte2 as(
select group_id, cte.player_id, score, rank() over (partition by group_id order by score desc, player_id asc) as rnk
from cte
join players on cte.player_id=players.player_id)

select group_id, player_id
from cte2
where rnk = 1