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
5 changes: 5 additions & 0 deletions 01-GamePlayAnalysis-II.sql
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions 02-GamePlayAnalysis-III.sql
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions 03-ShortestDistanceInAPlane.sql
Original file line number Diff line number Diff line change
@@ -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?!
6 changes: 6 additions & 0 deletions 04-CombineTwoTables.sql
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions 05-CustomerWithStrictlyIncreasingPurchases.sql
Original file line number Diff line number Diff line change
@@ -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