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 @@
#Solution1
select p.firstname, p.lastname, a.city , a.state from person p left join address a on p.personid = a.personid

#Solution2
select p.firstname, p.lastname, a.city , a.state from address a right join person p on p.personid = a.personid
8 changes: 8 additions & 0 deletions Customers_with_Strictly_Increasing_Purchases.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
with cte as (
select customer_id, year(order_date) as 'year' , sum(price) as 'price' from orders
group by year , customer_id order by customer_id,year)

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 c2.price > c1.price
group by c1.customer_id having count(*) - count(c2.customer_id) = 1
20 changes: 20 additions & 0 deletions Game_Play_AnalysisII.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#Solution1
with cte as (
select player_id, device_id, min(event_date) as 'event_date' from activity
group by player_id )
select a.player_id, a.device_id from activity a
join cte c on a.player_id = c.player_id
and a.event_date = c.event_date;


#Solution2
select distinct a.player_id, first_value(a.device_id) over(partition by a.player_id order by a.event_date) as device_id from activity a

#Solution3
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);

#Solution4
with cte as ( select distinct 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;

#Solution5
select a1.player_id, a1.device_id from activity a1 where (a1.player_id, a1.event_date) in (select a2.player_id, min(a2.event_date) from activity a2 group by a2.player_id)
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 @@
#Solution1
select player_id, event_date, sum( games_played) over (partition by player_id order by event_date) as games_played_so_far from activity

#Solution2
select a1.player_id, a1.event_date, sum(a1.games_played) as games_played_so_far from activity a1 inner join activity a2 on a1.player_id = a2.player_id and a1.event_date <= a2.event_date group by a2.player_id,a2.event_date

#Solution3
select a1.player_id, a1.event_date, (select sum(a2.games_played) from activity a2 where a1.player_id = a2.player_id and a2.event_date <= a1.event_date) as 'games_played_so_far' from activity a1;
8 changes: 8 additions & 0 deletions Shortest_Distance_in_a_Plane.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#Solution1
select round(sqrt(min(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 or p1.y != p2.y

#Solution2
select round(sqrt(min(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