diff --git a/01-GamePlayAnalysis-II.sql b/01-GamePlayAnalysis-II.sql new file mode 100644 index 0000000..3308d84 --- /dev/null +++ b/01-GamePlayAnalysis-II.sql @@ -0,0 +1,15 @@ +-- Problem 1 - Game Play Analysis II (https://leetcode.com/problems/game-play-analysis-ii/) +-- Approach 1 - Using Subquery +SELECT a.player_id, a.device_id +FROM Activity a +WHERE a.event_date IN +(SELECT MIN(b.event_date) FROM Activity b +WHERE a.player_id = b.player_id); +-- Approach 2 - Using CTE +WITH CTE AS ( + SELECT player_id, device_id, RANK() OVER(PARTITION BY player_id ORDER BY event_date) AS 'rnk' + FROM Activity +) +SELECT player_id, device_id +FROM CTE +WHERE rnk = 1; \ No newline at end of file diff --git a/02-GamePlayAnalysis-III.sql b/02-GamePlayAnalysis-III.sql new file mode 100644 index 0000000..72f34e9 --- /dev/null +++ b/02-GamePlayAnalysis-III.sql @@ -0,0 +1,4 @@ +-- Problem 2 - Game Play Analysis III (https://leetcode.com/problems/game-play-analysis-iii/) +-- Using Running Sum By Partitioning By player_id and ordering by event_date +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/03-ShortestDistanceInAPlane.sql b/03-ShortestDistanceInAPlane.sql new file mode 100644 index 0000000..dace008 --- /dev/null +++ b/03-ShortestDistanceInAPlane.sql @@ -0,0 +1,11 @@ +-- Problem 3 - Shortest Distance in a Plane (https://leetcode.com/problems/shortest-distance-in-a-plane/) +-- Approach 1 - Calculating Shortest Distance Without Eliminating Duplicate Calculations +SELECT ROUND(MIN(SQRT(POW(p2.x - p1.x, 2) + POW(p2.y - p1.y, 2))), 2) AS 'shortest' +FROM Point2D p1 +INNER JOIN Point2D p2 +WHERE p1.x != p2.x OR p1.y != p2.y; +-- Approach 2 - Calculating Shortest Distance By Eliminating Duplicate Calculations +SELECT ROUND(MIN(SQRT(POW(p2.x - p1.x, 2) + POW(p2.y - p1.y, 2))), 2) AS 'shortest' +FROM Point2D p1 +INNER JOIN Point2D p2 +WHERE (p1.x, p1.y) < (p2.x, p2.y); \ No newline at end of file diff --git a/04-CombineTwoTables.sql b/04-CombineTwoTables.sql new file mode 100644 index 0000000..0995e97 --- /dev/null +++ b/04-CombineTwoTables.sql @@ -0,0 +1,11 @@ +-- Problem 4 - Combine Two Tables (https://leetcode.com/problems/combine-two-tables/) +-- Approach 1 - Using LEFT JOIN +SELECT p.firstName, p.lastName, a.city, a.state +FROM Person p +LEFT JOIN Address a +ON p.personId = a.personId; +-- Approach 2 - Using RIGHT JOIN +SELECT p.firstName, p.lastName, a.city, a.state +FROM Address a +RIGHT JOIN Person p +ON p.personId = a.personId; \ No newline at end of file diff --git a/05-CustomersWithStrictlyIncreasingPurchases.sql b/05-CustomersWithStrictlyIncreasingPurchases.sql new file mode 100644 index 0000000..f851ce8 --- /dev/null +++ b/05-CustomersWithStrictlyIncreasingPurchases.sql @@ -0,0 +1,17 @@ +-- Problem 5 - Customers with Strictly Increasing Purchases (https://leetcode.com/problems/customers-with-strictly-increasing-purchases/) +-- Calculating total purchases on a yearly basis for each customer +WITH yearlyincrease AS( + SELECT customer_id, YEAR(order_date) AS orderyear, SUM(price) AS ordersum + FROM Orders + GROUP BY customer_id, orderyear + ORDER BY customer_id, orderyear +) +-- Selecting the customer_id where total purchases are strictly increasing in a year by joining the yearlyincrease cte with one anothe by left join where ordersum of first table is less than second and orderyear of second table is one greater than first +SELECT y1.customer_id +FROM yearlyincrease y1 +LEFT JOIN yearlyincrease y2 +ON y1.customer_id = y2.customer_id +AND y1.orderyear + 1 = y2.orderyear +AND y1.ordersum < y2.ordersum +GROUP BY y1.customer_id +HAVING COUNT(*) - COUNT(y2.customer_id) = 1; \ No newline at end of file