From 9eff5fc071595e08221851ebb2453a94f49cd7af Mon Sep 17 00:00:00 2001 From: akanksha-vishwak Date: Mon, 2 Jun 2025 11:54:35 -0400 Subject: [PATCH 1/2] solution to problems 1, 2, 4 --- 01-GamePlayAnalysis-II.sql | 5 +++++ 02-GamePlayAnalysis-III.sql | 19 +++++++++++++++++++ 04-CombineTwoTables.sql | 6 ++++++ 3 files changed, 30 insertions(+) create mode 100644 01-GamePlayAnalysis-II.sql create mode 100644 02-GamePlayAnalysis-III.sql create mode 100644 04-CombineTwoTables.sql diff --git a/01-GamePlayAnalysis-II.sql b/01-GamePlayAnalysis-II.sql new file mode 100644 index 0000000..60cf009 --- /dev/null +++ b/01-GamePlayAnalysis-II.sql @@ -0,0 +1,5 @@ +-- Problem 1 : Game Play Analysis II (https://leetcode.com/problems/game-play-analysis-ii/) + +select player_id, device_id +from +(select player_id, device_id, min(event_date) from activity group by player_id) as a \ No newline at end of file diff --git a/02-GamePlayAnalysis-III.sql b/02-GamePlayAnalysis-III.sql new file mode 100644 index 0000000..f6b35a7 --- /dev/null +++ b/02-GamePlayAnalysis-III.sql @@ -0,0 +1,19 @@ +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 will not work if we do not have order by event_date because the sum will be +-- calculated for all rows in the partition, it will return each row with full sum (12 for player_id 1) + + +-- using subquery +select a1.player_id, a1.event_date, + (select sum(a2.games_played) + from activity a2 + where a2.player_id = a1.player_id + and a2.event_date <= a1.event_date) as games_played_so_far +from activity a1 + +-- subquery is not efficient, it will be executed for each row in the outer query +-- so it takes O(n^2) time complexity, it will take 1st row then goes to the subquery +-- and match the player_id and event_date and run the sum \ No newline at end of file diff --git a/04-CombineTwoTables.sql b/04-CombineTwoTables.sql new file mode 100644 index 0000000..7285b88 --- /dev/null +++ b/04-CombineTwoTables.sql @@ -0,0 +1,6 @@ +-- Problem 4 : Combine Two Tables (https://leetcode.com/problems/combine-two-tables/) + +select firstName, lastName, city, state +from person p +left join address a +on p.personId = a.personId \ No newline at end of file From 419fc07f11e727a2c64b95a1b9f9472d9c35bf45 Mon Sep 17 00:00:00 2001 From: akanksha-vishwak Date: Wed, 4 Jun 2025 00:55:40 -0400 Subject: [PATCH 2/2] solution to problem 3,5 --- 03-ShortestDistanceInAPlane.sql | 14 ++++++++++++++ 05-CustomerWithStrictlyIncreasingPurchases.sql | 14 ++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 03-ShortestDistanceInAPlane.sql create mode 100644 05-CustomerWithStrictlyIncreasingPurchases.sql diff --git a/03-ShortestDistanceInAPlane.sql b/03-ShortestDistanceInAPlane.sql new file mode 100644 index 0000000..1dd4d04 --- /dev/null +++ b/03-ShortestDistanceInAPlane.sql @@ -0,0 +1,14 @@ +-- Problem 3 : Shortest Distance in a Plane (https://leetcode.com/problems/shortest-distance-in-a-plane/) + +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.x = p2.x AND p1.y < p2.y)) +-- to my understanding: forward comparing, make p1x < p2x then include this +-- p1x = p2x, then compare y — include only if p1y < p2y + +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, p1.y) < (p2.x, p2.y) +-- doesn't run on MySQL?! \ No newline at end of file diff --git a/05-CustomerWithStrictlyIncreasingPurchases.sql b/05-CustomerWithStrictlyIncreasingPurchases.sql new file mode 100644 index 0000000..305c190 --- /dev/null +++ b/05-CustomerWithStrictlyIncreasingPurchases.sql @@ -0,0 +1,14 @@ +-- Problem 5 : Customers with Strictly Increasing Purchases (https://leetcode.com/problems/customers-with-strictly-increasing-purchases/) + +with cte as ( + select customer_id, year(order_date) as 'year', sum(price) as price + from orders + group by year, customer_id +) +select customer_id +from cte c1 +left join cte c2 on c1.customer_id=c2.customer_id +and c1.year+1 = c2.year +and c1.price < c2.price +group by c1.customer_id +having count(*) - count(c2.customer_id) = 1 \ No newline at end of file