Skip to content

Commit 56bf69d

Browse files
authored
Add files via upload
1 parent 18ddc43 commit 56bf69d

26 files changed

+487
-0
lines changed

Easy/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

Easy/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
+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

Easy/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

Easy/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
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
+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

Easy/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
+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
+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

Easy/01303-find-the-team-size.sql

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- if we do not use order by in over(), we do not get running total, just normal aggregate for all rows within that partition
2+
3+
select employee_id, count(*) over(partition by team_id) as team_size
4+
from Employee
5+
order by 1
6+
7+
-- amazon- 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- subquery- use WHERE
2+
3+
select id, name
4+
from Students
5+
where department_id not in
6+
(select id
7+
from Departments)
8+
9+
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
10+
11+
-- join- use WHERE id is null
12+
13+
select s.id, s.name
14+
from Students s
15+
left join Departments d
16+
on d.id = s.department_id
17+
where d.id is null
18+
19+
20+
-- amazon- 1

Easy/01407-top-travellers.sql

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- using ifnull around sum()- can also use coalesce
2+
3+
select u.name, ifnull(sum(r.distance), 0) as travelled_distance
4+
from Users u
5+
left join Rides r
6+
on u.id = r.user_id
7+
group by u.id
8+
order by 2 desc, 1 asc
9+
10+
11+
-- point72- 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- use DISTINCT because we want distinct titles
2+
-- use where condition for filter
3+
4+
select distinct title
5+
from Content c join TVProgram t
6+
on c.content_id = t.content_id
7+
where c.Kids_content = 'Y' and c.content_type = 'Movies' and t.program_date like '2020-06%'
8+
9+
10+
-- amazon- 1
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
-- we need customer ids from 2 separate tables using 'and' condition
2+
-- 1st table- get sum of expenditures of all customers in June 2020, filter by customers whose sum >= 100
3+
-- 2nd table- get sum of expenditures of all customers in July 2020, filter by customers whose sum >= 100
4+
-- pull all customers who are in table1 AND table 2
5+
6+
select c.customer_id, c.name
7+
from Customers
8+
where customer_id in
9+
(select customer_id
10+
from Orders o
11+
join Product p
12+
on o.product_id = p.product_id
13+
where left(order_date, 7) = '2020-06'
14+
group by customer_id
15+
having sum(quantity*price) >= 100)
16+
and customer_id in
17+
(select customer_id, sum(quantity*price)
18+
from Orders o
19+
join Product p
20+
on o.product_id = p.product_id
21+
where left(order_date, 7) = '2020-07'
22+
group by customer_id
23+
having sum(quantity*price) >= 100)
24+
25+
---------------------------------------------------------------------------------------------------------------------
26+
27+
-- create a temp table- join all tables
28+
-- create 2 additional columns- expenditure in June and in July- CASE, AGGREGATE
29+
-- in the main query, pull customer ids where expenditure in both columns are >= 100
30+
31+
with CTE as(select c.customer_id, c.name,
32+
sum(case when left(o.order_date, 7) = '2020-06' then p.price*o.quantity else 0 end) june_spent,
33+
sum(case when left(o.order_date, 7) = '2020-07' then p.price*o.quantity else 0 end) july_spent
34+
from Customers c
35+
join Orders o
36+
on c.customer_id = o.customer_id
37+
join Product p
38+
on p.product_id = o.product_id
39+
group by 1)
40+
41+
select customer_id, name
42+
from CTE
43+
where june_spent >= 100 and july_spent >= 100
44+
45+
46+
-- amazon- 1

Easy/01571-warehouse-manager.sql

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-- multiple units, width, length, height, then calculate sum() of those
2+
3+
select w.name as warehouse_name,
4+
sum(units * Width * Length * Height) as volume
5+
from Warehouse w
6+
left join Products p
7+
on w.product_id = p.product_id
8+
group by 1
9+
10+
-------------------------------------------------------------------------------------------------------------
11+
-- breaking down the above one
12+
13+
with CTE as (
14+
select product_id, (Width * Length * Height) as size
15+
from Products)
16+
17+
select name as warehouse_name, sum(units * size) as volume
18+
from Warehouse w
19+
left join CTE c
20+
on c.product_id = w.product_id
21+
group by name
22+
23+
-- amazon- 1
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- simple aggregate with JOIN and HAVING
2+
3+
select u.name, sum(t.amount) as balance
4+
from Users u
5+
left join Transactions t
6+
on u.account = t.account
7+
group by u.account
8+
having sum(t.amount) > 10000
9+
10+
-- uber- 2

Easy/01607-sellers-with-no-sales.sql

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
-- select sellers from Orders table
2+
-- then select Sellers from Sellers table who are not in temp
3+
4+
select seller_name
5+
from Seller s
6+
where seller_id not in
7+
(select seller_id
8+
from Orders
9+
where sale_date like '2020%')
10+
order by seller_name
11+
12+
--------------------------------------------------------------------------------------------------------------------------------------------
13+
-- using JOIN with conditions
14+
15+
select s.seller_name
16+
from Seller s
17+
left join Orders o
18+
on o.seller_id = s.seller_id and sale_date like '2020%'
19+
where o.seller_id is null
20+
order by seller_name
21+
22+
-- no companies listed
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- basic aggregate function- count distinct, group by 2 variables
2+
3+
select date_id, make_name,
4+
count(distinct lead_id) unique_leads,
5+
count(distinct partner_id) unique_partners
6+
from DailySales
7+
group by date_id, make_name
8+
9+
10+
-- no companies listed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- simple aggregation- we need total sum of the diff between out and in time
2+
3+
select event_day as day, emp_id, sum(out_time-in_time) as total_time
4+
from Employees
5+
group by 1, 2
6+
7+
8+
-- adobe- 2
9+
-- amazon- 1

0 commit comments

Comments
 (0)