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
21 changes: 21 additions & 0 deletions Sql6_Q1_GamePlayAnalysis2_Join.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
with min_player_dev as
(
select
player_id
, min(event_date) as min_date
from
Activity
group by 1
)
select
m.player_id
, a.device_id
from min_player_dev m join Activity a on m.player_id = a.player_id and m.min_date = a.event_date

-- First, the query defines a Common Table Expression (CTE) named min_player_dev that selects the
-- earliest (MIN) event_date for each player_id from the Activity table, giving us the first day each player was active.
-- Next, the main query joins this CTE with the original Activity table on both player_id and event_date,
-- ensuring we only match records where the activity happened on the player’s first active date.
-- The final output lists each player_id along with the device_id they used on that first day of activity.
-- If a player used multiple devices on their first day,
-- the result will include multiple rows for that player—one for each device.
12 changes: 12 additions & 0 deletions Sql6_Q2_GamePlayAnalysis3_sumover.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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 query selects each row from the Activity table and calculates a running total of games played
-- by each player over time. For each row, it returns the player_id, the event_date,
-- and a new column games_played_so_far, which is computed using a window function.
-- The window function SUM(games_played) operates over a partition of rows grouped by player_id and
-- ordered by event_date, so for each player, it adds up the number of games played from their earliest activity
-- up to the current row's date. This creates a cumulative sum that grows with each new activity date per player.
13 changes: 13 additions & 0 deletions Sql6_Q3_2nd solution.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
with ranked_points as
(
select
x
, y
, row_number() over(order by x, y) as rownum
from
Point2D
)
select
min(round(sqrt(pow(p2.x - p1.x , 2) + pow(p2.y - p1.y, 2)),2)) as shortest
from
ranked_points p1 inner join ranked_points p2 on p1.rownum < p2.rownum
27 changes: 27 additions & 0 deletions Sql6_Q3_ShortestDistance.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- Write your MySQL query statement below
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.y != p2.y

-- WITH CTE AS (
-- SELECT x, y, ROW_NUMBER() OVER (ORDER BY x, y) AS r
-- FROM Point2D
-- )
-- SELECT
-- ROUND(
-- MIN(
-- SQRT(
-- POW(CTE1.x - CTE2.x, 2) + POW(CTE1.y - CTE2.y, 2)
-- )
-- ), 2
-- ) AS shortest
-- FROM
-- CTE AS CTE1,
-- CTE AS CTE2
-- WHERE
-- CTE1.r < CTE2.r;

-- Try --
-- p1.x < p2.x OR (p1.x = p2.x AND p1.y > p2.y) this will work too
-- ON (p1.x, p1.y) < (p2.x, p2.y);
-- (p1.x!=p2.x OR p1.y!=p2.y) AND (p1.x != p2.y OR p1.y!=p2.x)
14 changes: 14 additions & 0 deletions Sql6_Q4_CombineTbles_leftjoin.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
select
p.firstName
, p.lastName
, a.city
, a.state
from
Person p left join Address a on p.personId = a.personId

-- This SQL query retrieves a list of people along with their associated city and state information, if available.
-- It performs a left join between the Person table (aliased as p) and the Address table (aliased as a)
-- using the common personId column. The LEFT JOIN ensures that all records from the Person table are included,
-- even if there is no matching record in the Address table. For those people who don’t have a corresponding address,
-- the city and state fields will appear as NULL in the output. The selected columns include the person’s
-- first name, last name, city, and state.
28 changes: 28 additions & 0 deletions Sql6_Q5_Strly_Inc_Purch.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
with total_amount_table as
(
select
customer_id
, year(order_date) as year_purch
, sum(price) as total_purchases
from
Orders
group by 1,2
order by 1,2
)
select
t1.customer_id
from
total_amount_table t1 left join total_amount_table t2 on
t1.customer_id = t2.customer_id
and t2.total_purchases > t1.total_purchases
and t2.year_purch = t1.year_purch + 1
group by 1
having count(*) - count(t2.customer_id) = 1

-- second test case failed because there can be a year where it first decreased then increased. so this
-- query captured the increasing part and displayed it. but we want strictly increasing.
-- if we simply did select distinct t1.customer_id and then all the conditions, then 1st case did qualify
-- but in case of increasijg decreasing increasing, we fail. so we need to think of something where we can count
-- every row has been increasing. for that if we check count(*) and count(t2.cutsomer_id) and check nulls,
-- we will be able to find if all the years have been strictly increasing or not.
-- then we also dont have to do distinct , we just have to group by and put count condition in having