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 Combine Two Tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

SELECT p.firstName,p.lastName,a.city,a.state
FROM
Person p LEFT JOIN Address a
ON p.personId = a.personId
14 changes: 14 additions & 0 deletions Customers with Strictly Increasing Purchases.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

WITH CTE AS (
SELECT customer_id, YEAR(order_date) AS 'year', SUM(price) AS price_m
FROM Orders GROUP BY year,customer_id
ORDER BY customer_id
)

-- SELECT * FROM CTE

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_m < c2.price_m)
GROUP BY c1.customer_id HAVING COUNT(*) - COUNT(c2.customer_id) =1
21 changes: 21 additions & 0 deletions Game Play Analysis II.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Write your MySQL query statement below
-- SELECT DISTINCT player_id, FIRST_VALUE(device_id) OVER (PARTITION BY player_id ORDER BY event_date) AS device_id FROM
-- Activity
-- method2
-- 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)

WITH CTE AS(
SELECT player_id, device_id, RANK() OVER(PARTITION BY player_id ORDER BY event_date) AS 'rnk'
FROM Activity
)

SELECT c.player_id , c.device_id FROM CTE c
WHERE c.rnk = '1'





8 changes: 8 additions & 0 deletions Game Play Analysis III.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

SELECT player_id, event_date, SUM(games_played) OVER(PARTITION BY player_id ORDER BY event_date) AS games_played_so_far
FROM Activity

-- SELECT a1.player_id, a1.event_date, (SELECT SUM(a2.games_played) FROM Activity a2 WHERE
-- a1.player_id = a2.player_id AND a1.event_date >= a2.event_date) AS games_played_so_far

-- FROM Activity a1
20 changes: 20 additions & 0 deletions Shortest Distance in a Plane.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

-- WITH CTE AS (
-- SELECT p1.x AS x1, p1.y AS y1 ,p2.x AS x2, p2.y AS y2, ROUND(SQRT( POW(p2.x-p1.x,2) + POW(p2.y-p1.y,2) ),2) AS 'dist'
-- FROM Point2D p1
-- CROSS JOIN
-- Point2D p2
-- )


-- SELECT MIN(dist) AS shortest
-- FROM CTE
-- WHERE x1!=x2 OR y1!=y2

SELECT MIN(ROUND(SQRT(POW(p2.x - p1.x , 2) + POW(p2.y - p1.y, 2)),2)) AS 'shortest'
FROM point2D p1
INNER JOIN point2D p2
ON
(p1.x <= p2.x AND p1.y < p2.y) OR
(p1.x <= p2.x AND p1.y > p2.y) OR
(p1.x > p2.x AND p1.y = p2.y);