Skip to content

Commit 73513b0

Browse files
authored
Add files via upload
1 parent 25e79ee commit 73513b0

File tree

4 files changed

+245
-0
lines changed

4 files changed

+245
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
-- Question 68
2+
-- Table: Queue
3+
4+
-- +-------------+---------+
5+
-- | Column Name | Type |
6+
-- +-------------+---------+
7+
-- | person_id | int |
8+
-- | person_name | varchar |
9+
-- | weight | int |
10+
-- | turn | int |
11+
-- +-------------+---------+
12+
-- person_id is the primary key column for this table.
13+
-- This table has the information about all people waiting for an elevator.
14+
-- The person_id and turn columns will contain all numbers from 1 to n, where n is the number of rows in the table.
15+
16+
17+
-- The maximum weight the elevator can hold is 1000.
18+
19+
-- Write an SQL query to find the person_name of the last person who will fit in the elevator without exceeding the weight limit. It is guaranteed that the person who is first in the queue can fit in the elevator.
20+
21+
-- The query result format is in the following example:
22+
23+
-- Queue table
24+
-- +-----------+-------------------+--------+------+
25+
-- | person_id | person_name | weight | turn |
26+
-- +-----------+-------------------+--------+------+
27+
-- | 5 | George Washington | 250 | 1 |
28+
-- | 3 | John Adams | 350 | 2 |
29+
-- | 6 | Thomas Jefferson | 400 | 3 |
30+
-- | 2 | Will Johnliams | 200 | 4 |
31+
-- | 4 | Thomas Jefferson | 175 | 5 |
32+
-- | 1 | James Elephant | 500 | 6 |
33+
-- +-----------+-------------------+--------+------+
34+
35+
-- Result table
36+
-- +-------------------+
37+
-- | person_name |
38+
-- +-------------------+
39+
-- | Thomas Jefferson |
40+
-- +-------------------+
41+
42+
-- Queue table is ordered by turn in the example for simplicity.
43+
-- In the example George Washington(id 5), John Adams(id 3) and Thomas Jefferson(id 6) will enter the elevator as their weight sum is 250 + 350 + 400 = 1000.
44+
-- Thomas Jefferson(id 6) is the last person to fit in the elevator because he has the last turn in these three people.
45+
-- Solution
46+
With t1 as
47+
(
48+
select *,
49+
sum(weight) over(order by turn) as cum_weight
50+
from queue
51+
order by turn)
52+
53+
select t1.person_name
54+
from t1
55+
where turn = (select max(turn) from t1 where t1.cum_weight<=1000)
56+

Medium/Market Analysis 1.sql

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
-- Question 69
2+
-- Table: Users
3+
4+
-- +----------------+---------+
5+
-- | Column Name | Type |
6+
-- +----------------+---------+
7+
-- | user_id | int |
8+
-- | join_date | date |
9+
-- | favorite_brand | varchar |
10+
-- +----------------+---------+
11+
-- user_id is the primary key of this table.
12+
-- This table has the info of the users of an online shopping website where users can sell and buy items.
13+
-- Table: Orders
14+
15+
-- +---------------+---------+
16+
-- | Column Name | Type |
17+
-- +---------------+---------+
18+
-- | order_id | int |
19+
-- | order_date | date |
20+
-- | item_id | int |
21+
-- | buyer_id | int |
22+
-- | seller_id | int |
23+
-- +---------------+---------+
24+
-- order_id is the primary key of this table.
25+
-- item_id is a foreign key to the Items table.
26+
-- buyer_id and seller_id are foreign keys to the Users table.
27+
-- Table: Items
28+
29+
-- +---------------+---------+
30+
-- | Column Name | Type |
31+
-- +---------------+---------+
32+
-- | item_id | int |
33+
-- | item_brand | varchar |
34+
-- +---------------+---------+
35+
-- item_id is the primary key of this table.
36+
37+
38+
-- Write an SQL query to find for each user, the join date and the number of orders they made as a buyer in 2019.
39+
40+
-- The query result format is in the following example:
41+
42+
-- Users table:
43+
-- +---------+------------+----------------+
44+
-- | user_id | join_date | favorite_brand |
45+
-- +---------+------------+----------------+
46+
-- | 1 | 2018-01-01 | Lenovo |
47+
-- | 2 | 2018-02-09 | Samsung |
48+
-- | 3 | 2018-01-19 | LG |
49+
-- | 4 | 2018-05-21 | HP |
50+
-- +---------+------------+----------------+
51+
52+
-- Orders table:
53+
-- +----------+------------+---------+----------+-----------+
54+
-- | order_id | order_date | item_id | buyer_id | seller_id |
55+
-- +----------+------------+---------+----------+-----------+
56+
-- | 1 | 2019-08-01 | 4 | 1 | 2 |
57+
-- | 2 | 2018-08-02 | 2 | 1 | 3 |
58+
-- | 3 | 2019-08-03 | 3 | 2 | 3 |
59+
-- | 4 | 2018-08-04 | 1 | 4 | 2 |
60+
-- | 5 | 2018-08-04 | 1 | 3 | 4 |
61+
-- | 6 | 2019-08-05 | 2 | 2 | 4 |
62+
-- +----------+------------+---------+----------+-----------+
63+
64+
-- Items table:
65+
-- +---------+------------+
66+
-- | item_id | item_brand |
67+
-- +---------+------------+
68+
-- | 1 | Samsung |
69+
-- | 2 | Lenovo |
70+
-- | 3 | LG |
71+
-- | 4 | HP |
72+
-- +---------+------------+
73+
74+
-- Result table:
75+
-- +-----------+------------+----------------+
76+
-- | buyer_id | join_date | orders_in_2019 |
77+
-- +-----------+------------+----------------+
78+
-- | 1 | 2018-01-01 | 1 |
79+
-- | 2 | 2018-02-09 | 2 |
80+
-- | 3 | 2018-01-19 | 0 |
81+
-- | 4 | 2018-05-21 | 0 |
82+
-- +-----------+------------+----------------+
83+
84+
-- Solution
85+
select user_id as buyer_id, join_date, coalesce(a.orders_in_2019,0)
86+
from users
87+
left join
88+
(
89+
select buyer_id, coalesce(count(*), 0) as orders_in_2019
90+
from orders o
91+
join users u
92+
on u.user_id = o.buyer_id
93+
where extract('year'from order_date) = 2019
94+
group by buyer_id) a
95+
on users.user_id = a.buyer_id
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
-- Question 67
2+
-- Table: Products
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | product_id | int |
8+
-- | new_price | int |
9+
-- | change_date | date |
10+
-- +---------------+---------+
11+
-- (product_id, change_date) is the primary key of this table.
12+
-- Each row of this table indicates that the price of some product was changed to a new price at some date.
13+
14+
15+
-- Write an SQL query to find the prices of all products on 2019-08-16. Assume the price of all products before any change is 10.
16+
17+
-- The query result format is in the following example:
18+
19+
-- Products table:
20+
-- +------------+-----------+-------------+
21+
-- | product_id | new_price | change_date |
22+
-- +------------+-----------+-------------+
23+
-- | 1 | 20 | 2019-08-14 |
24+
-- | 2 | 50 | 2019-08-14 |
25+
-- | 1 | 30 | 2019-08-15 |
26+
-- | 1 | 35 | 2019-08-16 |
27+
-- | 2 | 65 | 2019-08-17 |
28+
-- | 3 | 20 | 2019-08-18 |
29+
-- +------------+-----------+-------------+
30+
31+
-- Result table:
32+
-- +------------+-------+
33+
-- | product_id | price |
34+
-- +------------+-------+
35+
-- | 2 | 50 |
36+
-- | 1 | 35 |
37+
-- | 3 | 10 |
38+
-- +------------+-------+
39+
40+
-- Solution
41+
with t1 as (
42+
select a.product_id, new_price
43+
from(
44+
Select product_id, max(change_date) as date
45+
from products
46+
where change_date<='2019-08-16'
47+
group by product_id) a
48+
join products p
49+
on a.product_id = p.product_id and a.date = p.change_date),
50+
51+
t2 as (
52+
select distinct product_id
53+
from products)
54+
55+
select t2.product_id, coalesce(new_price,10) as price
56+
from t2 left join t1
57+
on t2.product_id = t1.product_id
58+
order by price desc

Medium/Second degree follower.sql

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-- Question 70
2+
-- In facebook, there is a follow table with two columns: followee, follower.
3+
4+
-- Please write a sql query to get the amount of each follower’s follower if he/she has one.
5+
6+
-- For example:
7+
8+
-- +-------------+------------+
9+
-- | followee | follower |
10+
-- +-------------+------------+
11+
-- | A | B |
12+
-- | B | C |
13+
-- | B | D |
14+
-- | D | E |
15+
-- +-------------+------------+
16+
-- should output:
17+
-- +-------------+------------+
18+
-- | follower | num |
19+
-- +-------------+------------+
20+
-- | B | 2 |
21+
-- | D | 1 |
22+
-- +-------------+------------+
23+
-- Explaination:
24+
-- Both B and D exist in the follower list, when as a followee, B's follower is C and D, and D's follower is E. A does not exist in follower list.
25+
26+
27+
-- Note:
28+
-- Followee would not follow himself/herself in all cases.
29+
-- Please display the result in follower's alphabet order.
30+
31+
-- Solution
32+
select followee as follower, count(distinct(follower)) as num
33+
from follow
34+
where followee = any(select follower from follow)
35+
group by followee
36+
order by followee

0 commit comments

Comments
 (0)