From ee80432c88dd74ebc7bbc288d4f0bad5a1ceabb5 Mon Sep 17 00:00:00 2001 From: Paneri Patel Date: Thu, 26 Jun 2025 16:16:16 -0400 Subject: [PATCH] Done Sql6 --- CombineTwoTables.sql | 7 +++++++ CustomersWithStrictlyIncreasingPurchases.sql | 15 +++++++++++++++ GamePlayAnalysisII.sql | 14 ++++++++++++++ GamePlayAnalysisIII.sql | 6 ++++++ ShortestDistanceInAPlane.sql | 7 +++++++ 5 files changed, 49 insertions(+) create mode 100644 CombineTwoTables.sql create mode 100644 CustomersWithStrictlyIncreasingPurchases.sql create mode 100644 GamePlayAnalysisII.sql create mode 100644 GamePlayAnalysisIII.sql create mode 100644 ShortestDistanceInAPlane.sql diff --git a/CombineTwoTables.sql b/CombineTwoTables.sql new file mode 100644 index 0000000..012c1c5 --- /dev/null +++ b/CombineTwoTables.sql @@ -0,0 +1,7 @@ +''' +4 Problem 4 : Combine Two Tables (https://leetcode.com/problems/combine-two-tables/) +''' + +SELECT firstName, lastName, city, state +FROM Person +LEFT JOIN Address ON Address.personId = Person.personId diff --git a/CustomersWithStrictlyIncreasingPurchases.sql b/CustomersWithStrictlyIncreasingPurchases.sql new file mode 100644 index 0000000..73ad283 --- /dev/null +++ b/CustomersWithStrictlyIncreasingPurchases.sql @@ -0,0 +1,15 @@ +''' +5 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 + ORDER BY customer_id, year) + +SELECT c1.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 diff --git a/GamePlayAnalysisII.sql b/GamePlayAnalysisII.sql new file mode 100644 index 0000000..345b7aa --- /dev/null +++ b/GamePlayAnalysisII.sql @@ -0,0 +1,14 @@ +''' +Sql6 + +1 Problem 1 : Game Play Analysis II (https://leetcode.com/problems/game-play-analysis-ii/) + +''' + +SELECT player_id, device_id +FROM Activity +WHERE (player_id, event_date) IN ( + SELECT player_id, MIN(event_date) + FROM Activity + GROUP BY player_id +) \ No newline at end of file diff --git a/GamePlayAnalysisIII.sql b/GamePlayAnalysisIII.sql new file mode 100644 index 0000000..782472f --- /dev/null +++ b/GamePlayAnalysisIII.sql @@ -0,0 +1,6 @@ +''' +2 Problem 2 : Game Play Analysis III (https://leetcode.com/problems/game-play-analysis-iii/) +''' + +SELECT player_id, event_date, SUM(games_played) OVER (PARTITION BY player_id ORDER BY event_date) AS 'games_played_so_far' +FROM Activity \ No newline at end of file diff --git a/ShortestDistanceInAPlane.sql b/ShortestDistanceInAPlane.sql new file mode 100644 index 0000000..c32e338 --- /dev/null +++ b/ShortestDistanceInAPlane.sql @@ -0,0 +1,7 @@ +''' +3 Problem 3 : Shortest Distance in a Plane (https://leetcode.com/problems/shortest-distance-in-a-plane/) +''' + +SELECT ROUND(SQRT(MIN(POWER(p1.x - p2.x, 2) + POWER(p1.y - p2.y, 2))), 2) AS shortest +FROM Point2D p1, Point2D p2 +WHERE (p1.x, p1.y) <> (p2.x, p2.y) \ No newline at end of file