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
11 changes: 11 additions & 0 deletions Market_Analysis_II.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Write your MySQL query statement below

WITH CTE AS (
SELECT seller_id, item_id, RANK() OVER(PARTITION BY seller_id ORDER BY order_date) AS rnk FROM Orders
),

ACTE AS (
SELECT CTE.seller_id, Items.item_brand FROM CTE JOIN Items ON CTE.item_id = Items.item_id WHERE rnk = 2
)

SELECT Users.user_id AS seller_id, IF(Users.favorite_brand = ACTE.item_brand, 'yes', 'no') AS 2nd_item_fav_brand FROM Users LEFT JOIN ACTE ON Users.user_id = ACTE.seller_id;
18 changes: 18 additions & 0 deletions Tournament_Winners.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
WITH cte AS (
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
),
cte2 AS (
SELECT p.player_id, p.group_id, COALESCE(SUM(c.score), 0) AS total_score FROM Players p
LEFT JOIN cte c ON p.player_id = c.player_id
GROUP BY p.player_id, p.group_id
),
cte3 AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY group_id ORDER BY total_score DESC, player_id ASC
) AS rnk
FROM cte2
)
SELECT group_id, player_id
FROM cte3
WHERE rnk = 1;