From 005efa70034bf379b3280a18d19eb702178dd07c Mon Sep 17 00:00:00 2001 From: Arushi Bhambri Date: Fri, 6 Jun 2025 15:48:38 -0700 Subject: [PATCH 1/2] Added Sql6 Q1-5 solutions --- Sql6_Q1_GamePlayAnalysis2_Join.sql | 21 ++++++++++++++++++++ Sql6_Q2_GamePlayAnalysis3_sumover.sql | 12 ++++++++++++ Sql6_Q3_ShortestDistance.sql | 0 Sql6_Q4_CombineTbles_leftjoin.sql | 14 ++++++++++++++ Sql6_Q5_Strly_Inc_Purch.sql | 28 +++++++++++++++++++++++++++ 5 files changed, 75 insertions(+) create mode 100644 Sql6_Q1_GamePlayAnalysis2_Join.sql create mode 100644 Sql6_Q2_GamePlayAnalysis3_sumover.sql create mode 100644 Sql6_Q3_ShortestDistance.sql create mode 100644 Sql6_Q4_CombineTbles_leftjoin.sql create mode 100644 Sql6_Q5_Strly_Inc_Purch.sql diff --git a/Sql6_Q1_GamePlayAnalysis2_Join.sql b/Sql6_Q1_GamePlayAnalysis2_Join.sql new file mode 100644 index 0000000..ef88f4e --- /dev/null +++ b/Sql6_Q1_GamePlayAnalysis2_Join.sql @@ -0,0 +1,21 @@ +with min_player_dev as +( + select + player_id + , min(event_date) as min_date + from + Activity + group by 1 +) +select + m.player_id + , a.device_id +from min_player_dev m join Activity a on m.player_id = a.player_id and m.min_date = a.event_date + +-- First, the query defines a Common Table Expression (CTE) named min_player_dev that selects the +-- earliest (MIN) event_date for each player_id from the Activity table, giving us the first day each player was active. +-- Next, the main query joins this CTE with the original Activity table on both player_id and event_date, +-- ensuring we only match records where the activity happened on the player’s first active date. +-- The final output lists each player_id along with the device_id they used on that first day of activity. +-- If a player used multiple devices on their first day, +-- the result will include multiple rows for that player—one for each device. \ No newline at end of file diff --git a/Sql6_Q2_GamePlayAnalysis3_sumover.sql b/Sql6_Q2_GamePlayAnalysis3_sumover.sql new file mode 100644 index 0000000..7d31d37 --- /dev/null +++ b/Sql6_Q2_GamePlayAnalysis3_sumover.sql @@ -0,0 +1,12 @@ +select + player_id + , event_date + , sum(games_played) over(partition by player_id order by event_date) as games_played_so_far +from Activity + +-- This query selects each row from the Activity table and calculates a running total of games played +-- by each player over time. For each row, it returns the player_id, the event_date, +-- and a new column games_played_so_far, which is computed using a window function. +-- The window function SUM(games_played) operates over a partition of rows grouped by player_id and +-- ordered by event_date, so for each player, it adds up the number of games played from their earliest activity +-- up to the current row's date. This creates a cumulative sum that grows with each new activity date per player. \ No newline at end of file diff --git a/Sql6_Q3_ShortestDistance.sql b/Sql6_Q3_ShortestDistance.sql new file mode 100644 index 0000000..e69de29 diff --git a/Sql6_Q4_CombineTbles_leftjoin.sql b/Sql6_Q4_CombineTbles_leftjoin.sql new file mode 100644 index 0000000..8f915ce --- /dev/null +++ b/Sql6_Q4_CombineTbles_leftjoin.sql @@ -0,0 +1,14 @@ +select + p.firstName + , p.lastName + , a.city + , a.state +from + Person p left join Address a on p.personId = a.personId + +-- This SQL query retrieves a list of people along with their associated city and state information, if available. +-- It performs a left join between the Person table (aliased as p) and the Address table (aliased as a) +-- using the common personId column. The LEFT JOIN ensures that all records from the Person table are included, +-- even if there is no matching record in the Address table. For those people who don’t have a corresponding address, +-- the city and state fields will appear as NULL in the output. The selected columns include the person’s +-- first name, last name, city, and state. \ No newline at end of file diff --git a/Sql6_Q5_Strly_Inc_Purch.sql b/Sql6_Q5_Strly_Inc_Purch.sql new file mode 100644 index 0000000..d609519 --- /dev/null +++ b/Sql6_Q5_Strly_Inc_Purch.sql @@ -0,0 +1,28 @@ +with total_amount_table as +( +select + customer_id + , year(order_date) as year_purch + , sum(price) as total_purchases +from + Orders +group by 1,2 +order by 1,2 +) +select + t1.customer_id +from + total_amount_table t1 left join total_amount_table t2 on + t1.customer_id = t2.customer_id + and t2.total_purchases > t1.total_purchases + and t2.year_purch = t1.year_purch + 1 +group by 1 +having count(*) - count(t2.customer_id) = 1 + +-- second test case failed because there can be a year where it first decreased then increased. so this +-- query captured the increasing part and displayed it. but we want strictly increasing. +-- if we simply did select distinct t1.customer_id and then all the conditions, then 1st case did qualify +-- but in case of increasijg decreasing increasing, we fail. so we need to think of something where we can count +-- every row has been increasing. for that if we check count(*) and count(t2.cutsomer_id) and check nulls, +-- we will be able to find if all the years have been strictly increasing or not. +-- then we also dont have to do distinct , we just have to group by and put count condition in having \ No newline at end of file From 83eccde8e315dd0ae5c74586c3f07e9b83bd1386 Mon Sep 17 00:00:00 2001 From: Arushi Bhambri Date: Mon, 21 Jul 2025 16:05:21 -0700 Subject: [PATCH 2/2] Added another solution for Q3 Shortest distance --- Sql6_Q3_2nd solution.sql | 13 +++++++++++++ Sql6_Q3_ShortestDistance.sql | 27 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 Sql6_Q3_2nd solution.sql diff --git a/Sql6_Q3_2nd solution.sql b/Sql6_Q3_2nd solution.sql new file mode 100644 index 0000000..164db16 --- /dev/null +++ b/Sql6_Q3_2nd solution.sql @@ -0,0 +1,13 @@ +with ranked_points as +( + select + x + , y + , row_number() over(order by x, y) as rownum + from + Point2D +) +select + min(round(sqrt(pow(p2.x - p1.x , 2) + pow(p2.y - p1.y, 2)),2)) as shortest +from + ranked_points p1 inner join ranked_points p2 on p1.rownum < p2.rownum \ No newline at end of file diff --git a/Sql6_Q3_ShortestDistance.sql b/Sql6_Q3_ShortestDistance.sql index e69de29..47ec8a8 100644 --- a/Sql6_Q3_ShortestDistance.sql +++ b/Sql6_Q3_ShortestDistance.sql @@ -0,0 +1,27 @@ +-- Write your MySQL query statement below +select + round(min(sqrt(pow(p1.x-p2.x, 2) + pow(p1.y-p2.y, 2))),2) as 'shortest' +from point2D p1 inner join point2D p2 on p1.x != p2.x or p1.y != p2.y + +-- WITH CTE AS ( +-- SELECT x, y, ROW_NUMBER() OVER (ORDER BY x, y) AS r +-- FROM Point2D +-- ) +-- SELECT +-- ROUND( +-- MIN( +-- SQRT( +-- POW(CTE1.x - CTE2.x, 2) + POW(CTE1.y - CTE2.y, 2) +-- ) +-- ), 2 +-- ) AS shortest +-- FROM +-- CTE AS CTE1, +-- CTE AS CTE2 +-- WHERE +-- CTE1.r < CTE2.r; + +-- Try -- +-- p1.x < p2.x OR (p1.x = p2.x AND p1.y > p2.y) this will work too +-- ON (p1.x, p1.y) < (p2.x, p2.y); +-- (p1.x!=p2.x OR p1.y!=p2.y) AND (p1.x != p2.y OR p1.y!=p2.x) \ No newline at end of file