Skip to content

Commit 9b23f08

Browse files
authored
Add files via upload
1 parent ebf5faa commit 9b23f08

File tree

50 files changed

+1246
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1246
-0
lines changed

00175-combine-two-tables.sql

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- simple LEFT JOIN
2+
3+
select p.firstName, p.lastName, a.city, a.state
4+
from Person p
5+
left join Address a
6+
using(personId)
7+
8+
9+
-- apple- 4
10+
-- bloomberg- 2
11+
-- amazon- 2
12+
-- microsoft- 2
13+
-- adobe- 3
14+
-- google- 3

00182-duplicate-emails.sql

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- use group by for aggregate
2+
3+
select email as Email
4+
from Person
5+
group by email
6+
having count(email) > 1
7+
8+
9+
-- amazon- 2
10+
-- uber- 2

00183-customers-who-never-order.sql

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- pick id from Orders, and do not select those ids
2+
3+
select name as Customers
4+
from Customers
5+
where id not in
6+
(select distinct customerId
7+
from Orders)
8+
9+
-- amazon- 3
10+
-- apple- 7
11+
-- bloomberg- 5
12+
-- adobe- 2

00184-department-highest-salary.sql

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
-- using subquery
2+
3+
select d.name as Department, e.name as Employee, e.salary
4+
from Department d join Employee e
5+
on d.id = e.departmentId
6+
where (e.departmentId, e.salary) in
7+
(select departmentId, max(salary)
8+
from Employee
9+
group by departmentId)
10+
11+
-----------------------------------------------------------------------------------------------------------------------------------------------------------
12+
13+
-- using window function
14+
15+
with CTE as
16+
(select departmentId, id, name, salary,
17+
dense_rank() over(partition by departmentId order by salary desc) as rnk
18+
from Employee)
19+
20+
select d.name as Department, e.name as Employee, e.salary
21+
from Department d join CTE e
22+
on d.id = e.departmentId
23+
where e.rnk = 1
24+
25+
26+
-- amazon- 2
27+
-- microsoft- 3
28+
-- apple- 2
29+
-- facebook- 2
30+
-- google- 2

00511-game-play-analysis-i.sql

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- simple aggregate function
2+
3+
select player_id, min(event_date) as first_login
4+
from Activity
5+
group by 1
6+
7+
-- adobe- 2
8+
-- amazon- 4
9+
-- bloomberg- 4
10+
-- gsn games- 1

00512-game-play-analysis-ii.sql

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
-- using subquery
2+
3+
select player_id, device_id
4+
from Activity
5+
where (player_id, event_date) in
6+
(select player_id, min(event_date)
7+
from Activity
8+
group by player_id)
9+
10+
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
11+
-- using window function
12+
13+
with CTE as
14+
(select player_id, device_id,
15+
row_number() over(partition by player_id order by event_date) as rn
16+
from Activity)
17+
18+
select player_id, device_id
19+
from CTE
20+
where rn = 1
21+
22+
-- gsn games- 1

00534-game-play-analysis-iii.sql

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- running total of games played
2+
-- sum() over()
3+
4+
select player_id, event_date,
5+
sum(games_played) over(partition by player_id order by event_date) as games_played_so_far
6+
from Activity
7+
8+
-- gsn games
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- the the cusotmer with maximum order count, order by, limit
2+
3+
select customer_number
4+
from Orders
5+
group by 1
6+
order by count(order_number) desc
7+
limit 1
8+
9+
10+
-- adobe- 2
11+
-- google- 3
12+
-- apple- 2
13+
-- uber- 2
14+
-- twitter- 1

00603-consecutive-available-seats.sql

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
-- lag and lead will give the rows above and below
2+
-- if free = 1 and either of lag_free or lead_free is 1, it means we have 2 consecutive free seats.
3+
-- just pick those rows
4+
5+
with CTE as(
6+
select seat_id, free,
7+
lag(free, 1) over(order by seat_id) as lag_free,
8+
lead(free, 1) over(order by seat_id) as lead_free
9+
from Cinema)
10+
11+
select seat_id
12+
from CTE
13+
where (free = 1 and lag_free = 1) or (free = 1 and lead_free = 1)
14+
order by 1
15+
16+
------------------------------------------------------------------------------------------------------------------------------------------------------------
17+
-- if seat = free AND seat + 1 or seat - 1 have free = 1, then pull that seat
18+
19+
select seat_id
20+
from Cinema
21+
where free = 1 and
22+
(seat_id - 1 in (select seat_id
23+
from Cinema
24+
where free = 1)
25+
or
26+
seat_id + 1 in (select seat_id
27+
from Cinema
28+
where free = 1))
29+
30+
31+
-- amazon- 4

00607-sales-person.sql

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
-- nested query
2+
-- select all salesPerson with company RED
3+
-- select all salesPerson from SalesPerson not in the above table
4+
5+
select sp.name
6+
from SalesPerson sp
7+
where sales_id not in
8+
(select o.sales_id
9+
from Orders o
10+
where o.com_id in
11+
(select c.com_id
12+
from Company c
13+
where c.name = 'RED'))
14+
15+
------------------------------------------------------------------------------------------------------------------------------------------------
16+
-- JOIN Company c and Ordered o
17+
-- pick all sales_id with company = 'RED'
18+
-- pick all salesPerson from SalesPerson not in temp table above
19+
20+
select sp.name
21+
from SalesPerson sp
22+
where sales_id not in
23+
(select o.sales_id
24+
from Orders o
25+
inner join Company c
26+
on c.com_id = o.com_id
27+
where c.name = 'RED')
28+
29+
30+
-- no companies listed

00608-tree-node.sql

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- 3 cases- use CASE WHEN
2+
3+
select id,
4+
(case when p_id is null then 'Root'
5+
when id in (select p_id from Tree) then 'Inner'
6+
else 'Leaf' end) as type
7+
from Tree
8+
9+
10+
-- twitter- 1

00613-shortest-distance-in-a-line.sql

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
-- cross joining all the points from 2 tables, except the ones where they are same
2+
-- find the min of absolute distance
3+
4+
select min(abs(a - b)) as shortest
5+
from
6+
(select p1.x as a, p2.x as b
7+
from Point p1 cross join Point p2
8+
where p1.x != p2.x) temp
9+
10+
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
11+
-- concise version of the above
12+
13+
select min(abs(p1.x - p2.x)) as shortest
14+
from Point p1 cross join Point p2
15+
where p1.x != p2.x
16+
17+
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
18+
-- pull min distance with a where condition
19+
20+
select min(p1.x - p2.x) as shortest
21+
from Point p1, Point p2
22+
where p1.x > p2.x
23+
24+
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
25+
-- sort the table, and do lag. Now diff between current and lag- because difference between the sorted will always be lesser than difference between the larger ones
26+
-- pull the min distance
27+
28+
with CTE as
29+
(select x - lag(x) over(order by x) as distance
30+
from Point)
31+
32+
select min(distance) as shortest from CTE
33+
34+
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
35+
-- picking the lowest distance, 1st row will always be null hence use offset
36+
37+
select x - lag(x) over(order by x) as shortest
38+
from Point
39+
order by 1 asc
40+
limit 1 offset 1
41+
42+
43+
-- no companies listed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- aggregate- group by 2 columns, count
2+
3+
select actor_id, director_id
4+
from ActorDirector
5+
group by 1, 2
6+
having count(*) >= 3
7+
8+
9+
-- amazon- 3

01077-project-employees-iii.sql

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- use dense_rank(), partition by project, order by years desc
2+
3+
with CTE as
4+
(select p.project_id, p.employee_id, e.experience_years,
5+
dense_rank() over(partition by p.project_id order by e.experience_years desc) as rnk
6+
from Project p
7+
left join Employee e
8+
on p.employee_id = e.employee_id)
9+
10+
select project_id, employee_id
11+
from CTE
12+
where rnk = 1
13+
14+
-- facebook- 1
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-- use RANK() and pull results where rank = 1
2+
3+
select student_id, course_id, grade
4+
from
5+
(select student_id, course_id, grade, dense_rank() over(partition by student_id order by grade desc, course_id asc) as rnk
6+
from Enrollments) temp1
7+
where rnk = 1
8+
order by 1
9+
10+
--------------------------------------------------------------------------------------------------------------------------------------------------------------
11+
-- nested
12+
-- first get id and highest grade, then get min course_id
13+
14+
select student_id, min(course_id) as course_id, grade
15+
from Enrollments
16+
where (student_id, grade) in
17+
(select student_id, max(grade) as grade
18+
from Enrollments
19+
group by student_id)
20+
group by student_id
21+
order by student_id
22+
23+
-- amazon- 2
24+
-- coursera- 1

01173-immediate-food-delivery-i.sql

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- simple condition in aggregate function- count immediate, divide by total rows in the table
2+
3+
select round(sum(order_date = customer_pref_delivery_date) / count(*) * 100, 2) as immediate_percentage
4+
from Delivery
5+
---------------------------------------------------------------------------------------------------------------------------------------------
6+
-- same as above but using case
7+
8+
select round(sum( case when order_date = customer_pref_delivery_date then 1 else 0 end) / count(*) * 100, 2) as immediate_percentage
9+
from Delivery
10+
11+
12+
-- doordash- 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-- union all- one for host(when that team was host)- count host score, 2nd for guest(when that team was guest)- count guest score
2+
-- calculate sum of points
3+
-- group by team_id
4+
5+
select t.team_id, t.team_name, coalesce(sum(u.points), 0) as num_points
6+
from Teams t
7+
left join
8+
(select match_id, host_team as team_id, (case when host_goals > guest_goals then 3
9+
when host_goals = guest_goals then 1
10+
else 0 end) as points
11+
from Matches
12+
union all
13+
select match_id, guest_team as team_id,
14+
(case when host_goals < guest_goals then 3
15+
when host_goals = guest_goals then 1
16+
else 0 end) as points
17+
from Matches) u
18+
on u.team_id = t.team_id
19+
group by team_id
20+
order by 3 desc, 1 asc
21+
22+
-------------------------------------------------------------------------------------------------------------------------
23+
-- without using 'UNION ALL'- only used JOIN
24+
25+
select t.team_id, t.team_name, coalesce(
26+
sum(case when t.team_id = m.host_team and m.host_goals > m.guest_goals then 3
27+
when t.team_id = m.guest_team and m.guest_goals > m.host_goals then 3
28+
when host_goals = guest_goals then 1 end), 0) as num_points
29+
from Teams t
30+
left join Matches m
31+
on m.host_team = t.team_id or m.guest_team = t.team_id
32+
group by team_id
33+
order by 3 desc, 1 asc
34+
35+
36+
-- wayfair- 1

01225-report-contiguous-dates.sql

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
-- cte1- unioned both tables and arranged in ascending order of dates
2+
-- cte2- calculated rn and rnk and diff between them
3+
-- final query- picked min date as start date, max date as end date, grouped by status and diff
4+
-- rank()- gave them ranking based on their status ascending order date
5+
-- row_number()- ordered by date asc
6+
-- diff between them will be consistent if they are contiguous, hence group by diff
7+
-- when the status changes, again diff between them will be contiguous hence group by status to get different records for same status
8+
9+
with cte1 as
10+
((select fail_date as event_date, 'failed' as status
11+
from Failed)
12+
union all
13+
(select success_date as event_date, 'succeeded' as status
14+
from Succeeded)),
15+
cte2 as
16+
(select *,
17+
row_number() over(order by event_date) as rn,
18+
dense_rank() over (partition by status order by event_date) as rnk,
19+
row_number() over(order by event_date) - dense_rank() over (partition by status order by event_date) as diff
20+
from cte1
21+
where event_date between '2019-01-01' and '2019-12-31'
22+
order by 1)
23+
24+
select status as period_state, min(event_date) as start_date, max(event_date) as end_date
25+
from cte2
26+
group by status, diff
27+
order by 2
28+
29+
-- facebook- 1
30+
31+
------------------------------------------------------------------------------------------------------------------------------------------------------------
32+
33+
-- o/p of cte2
34+
35+
| event_date | status | rn | rnk | diff |
36+
| ---------- | --------- | -- | --- | ---- |
37+
| 2019-01-01 | succeeded | 1 | 1 | 0 |
38+
| 2019-01-02 | succeeded | 2 | 2 | 0 |
39+
| 2019-01-03 | succeeded | 3 | 3 | 0 |
40+
| 2019-01-04 | failed | 4 | 1 | 3 |
41+
| 2019-01-05 | failed | 5 | 2 | 3 |
42+
| 2019-01-06 | succeeded | 6 | 4 | 2 |

0 commit comments

Comments
 (0)