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
15 changes: 15 additions & 0 deletions 01-GamePlayAnalysis-II.sql
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 4 additions & 0 deletions 02-GamePlayAnalysis-III.sql
Original file line number Diff line number Diff line change
@@ -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;
11 changes: 11 additions & 0 deletions 03-ShortestDistanceInAPlane.sql
Original file line number Diff line number Diff line change
@@ -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);
11 changes: 11 additions & 0 deletions 04-CombineTwoTables.sql
Original file line number Diff line number Diff line change
@@ -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;
17 changes: 17 additions & 0 deletions 05-CustomersWithStrictlyIncreasingPurchases.sql
Original file line number Diff line number Diff line change
@@ -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;