Skip to content

Commit db07966

Browse files
authored
Add files via upload
1 parent 1b91bfd commit db07966

File tree

4 files changed

+246
-0
lines changed

4 files changed

+246
-0
lines changed

Medium/Article Views 2.sql

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
-- Question 81
2+
-- Table: Views
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | article_id | int |
8+
-- | author_id | int |
9+
-- | viewer_id | int |
10+
-- | view_date | date |
11+
-- +---------------+---------+
12+
-- There is no primary key for this table, it may have duplicate rows.
13+
-- Each row of this table indicates that some viewer viewed an article (written by some author) on some date.
14+
-- Note that equal author_id and viewer_id indicate the same person.
15+
16+
17+
-- Write an SQL query to find all the people who viewed more than one article on the same date, sorted in ascending order by their id.
18+
19+
-- The query result format is in the following example:
20+
21+
-- Views table:
22+
-- +------------+-----------+-----------+------------+
23+
-- | article_id | author_id | viewer_id | view_date |
24+
-- +------------+-----------+-----------+------------+
25+
-- | 1 | 3 | 5 | 2019-08-01 |
26+
-- | 3 | 4 | 5 | 2019-08-01 |
27+
-- | 1 | 3 | 6 | 2019-08-02 |
28+
-- | 2 | 7 | 7 | 2019-08-01 |
29+
-- | 2 | 7 | 6 | 2019-08-02 |
30+
-- | 4 | 7 | 1 | 2019-07-22 |
31+
-- | 3 | 4 | 4 | 2019-07-21 |
32+
-- | 3 | 4 | 4 | 2019-07-21 |
33+
-- +------------+-----------+-----------+------------+
34+
35+
-- Result table:
36+
-- +------+
37+
-- | id |
38+
-- +------+
39+
-- | 5 |
40+
-- | 6 |
41+
-- +------+
42+
43+
-- Solution
44+
select distinct viewer_id as id#, count(distinct article_id) as total
45+
from views
46+
group by viewer_id, view_date
47+
having count(distinct article_id)>1
48+
order by 1

Medium/Immediate Food Delivery 2.sql

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
-- Question 82
2+
-- Table: Delivery
3+
4+
-- +-----------------------------+---------+
5+
-- | Column Name | Type |
6+
-- +-----------------------------+---------+
7+
-- | delivery_id | int |
8+
-- | customer_id | int |
9+
-- | order_date | date |
10+
-- | customer_pref_delivery_date | date |
11+
-- +-----------------------------+---------+
12+
-- delivery_id is the primary key of this table.
13+
-- The table holds information about food delivery to customers that make orders at some date and specify a preferred delivery date (on the same order date or after it).
14+
15+
16+
-- If the preferred delivery date of the customer is the same as the order date then the order is called immediate otherwise it's called scheduled.
17+
18+
-- The first order of a customer is the order with the earliest order date that customer made. It is guaranteed that a customer has exactly one first order.
19+
20+
-- Write an SQL query to find the percentage of immediate orders in the first orders of all customers, rounded to 2 decimal places.
21+
22+
-- The query result format is in the following example:
23+
24+
-- Delivery table:
25+
-- +-------------+-------------+------------+-----------------------------+
26+
-- | delivery_id | customer_id | order_date | customer_pref_delivery_date |
27+
-- +-------------+-------------+------------+-----------------------------+
28+
-- | 1 | 1 | 2019-08-01 | 2019-08-02 |
29+
-- | 2 | 2 | 2019-08-02 | 2019-08-02 |
30+
-- | 3 | 1 | 2019-08-11 | 2019-08-12 |
31+
-- | 4 | 3 | 2019-08-24 | 2019-08-24 |
32+
-- | 5 | 3 | 2019-08-21 | 2019-08-22 |
33+
-- | 6 | 2 | 2019-08-11 | 2019-08-13 |
34+
-- | 7 | 4 | 2019-08-09 | 2019-08-09 |
35+
-- +-------------+-------------+------------+-----------------------------+
36+
37+
-- Result table:
38+
-- +----------------------+
39+
-- | immediate_percentage |
40+
-- +----------------------+
41+
-- | 50.00 |
42+
-- +----------------------+
43+
-- The customer id 1 has a first order with delivery id 1 and it is scheduled.
44+
-- The customer id 2 has a first order with delivery id 2 and it is immediate.
45+
-- The customer id 3 has a first order with delivery id 5 and it is scheduled.
46+
-- The customer id 4 has a first order with delivery id 7 and it is immediate.
47+
-- Hence, half the customers have immediate first orders.
48+
49+
-- Solution
50+
select
51+
round(avg(case when order_date = customer_pref_delivery_date then 1 else 0 end)*100,2) as
52+
immediate_percentage
53+
from
54+
(select *,
55+
rank() over(partition by customer_id order by order_date) as rk
56+
from delivery) a
57+
where a.rk=1

Medium/Monthly Transactions 1.sql

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
-- Question 83
2+
-- Table: Transactions
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | id | int |
8+
-- | country | varchar |
9+
-- | state | enum |
10+
-- | amount | int |
11+
-- | trans_date | date |
12+
-- +---------------+---------+
13+
-- id is the primary key of this table.
14+
-- The table has information about incoming transactions.
15+
-- The state column is an enum of type ["approved", "declined"].
16+
17+
18+
-- Write an SQL query to find for each month and country, the number of transactions and their total amount, the number of approved transactions and their total amount.
19+
20+
-- The query result format is in the following example:
21+
22+
-- Transactions table:
23+
-- +------+---------+----------+--------+------------+
24+
-- | id | country | state | amount | trans_date |
25+
-- +------+---------+----------+--------+------------+
26+
-- | 121 | US | approved | 1000 | 2018-12-18 |
27+
-- | 122 | US | declined | 2000 | 2018-12-19 |
28+
-- | 123 | US | approved | 2000 | 2019-01-01 |
29+
-- | 124 | DE | approved | 2000 | 2019-01-07 |
30+
-- +------+---------+----------+--------+------------+
31+
32+
-- Result table:
33+
-- +----------+---------+-------------+----------------+--------------------+-----------------------+
34+
-- | month | country | trans_count | approved_count | trans_total_amount | approved_total_amount |
35+
-- +----------+---------+-------------+----------------+--------------------+-----------------------+
36+
-- | 2018-12 | US | 2 | 1 | 3000 | 1000 |
37+
-- | 2019-01 | US | 1 | 1 | 2000 | 2000 |
38+
-- | 2019-01 | DE | 1 | 1 | 2000 | 2000 |
39+
-- +----------+---------+-------------+----------------+--------------------+-----------------------+
40+
41+
-- Solution
42+
with t1 as(
43+
select DATE_FORMAT(trans_date,'%Y-%m') as month, country, count(state) as trans_count, sum(amount) as trans_total_amount
44+
from transactions
45+
group by country, month(trans_date)),
46+
47+
t2 as (
48+
Select DATE_FORMAT(trans_date,'%Y-%m') as month, country, count(state) as approved_count, sum(amount) as approved_total_amount
49+
from transactions
50+
where state = 'approved'
51+
group by country, month(trans_date))
52+
53+
select t1.month, t1.country, coalesce(t1.trans_count,0) as trans_count, coalesce(t2.approved_count,0) as approved_count, coalesce(t1.trans_total_amount,0) as trans_total_amount, coalesce(t2.approved_total_amount,0) as approved_total_amount
54+
from t1 left join t2
55+
on t1.country = t2.country and t1.month = t2.month

Medium/Page Recommnedations.sql

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
-- Question 84
2+
-- Table: Friendship
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | user1_id | int |
8+
-- | user2_id | int |
9+
-- +---------------+---------+
10+
-- (user1_id, user2_id) is the primary key for this table.
11+
-- Each row of this table indicates that there is a friendship relation between user1_id and user2_id.
12+
13+
14+
-- Table: Likes
15+
16+
-- +-------------+---------+
17+
-- | Column Name | Type |
18+
-- +-------------+---------+
19+
-- | user_id | int |
20+
-- | page_id | int |
21+
-- +-------------+---------+
22+
-- (user_id, page_id) is the primary key for this table.
23+
-- Each row of this table indicates that user_id likes page_id.
24+
25+
26+
-- Write an SQL query to recommend pages to the user with user_id = 1 using the pages that your friends liked. It should not recommend pages you already liked.
27+
28+
-- Return result table in any order without duplicates.
29+
30+
-- The query result format is in the following example:
31+
32+
-- Friendship table:
33+
-- +----------+----------+
34+
-- | user1_id | user2_id |
35+
-- +----------+----------+
36+
-- | 1 | 2 |
37+
-- | 1 | 3 |
38+
-- | 1 | 4 |
39+
-- | 2 | 3 |
40+
-- | 2 | 4 |
41+
-- | 2 | 5 |
42+
-- | 6 | 1 |
43+
-- +----------+----------+
44+
45+
-- Likes table:
46+
-- +---------+---------+
47+
-- | user_id | page_id |
48+
-- +---------+---------+
49+
-- | 1 | 88 |
50+
-- | 2 | 23 |
51+
-- | 3 | 24 |
52+
-- | 4 | 56 |
53+
-- | 5 | 11 |
54+
-- | 6 | 33 |
55+
-- | 2 | 77 |
56+
-- | 3 | 77 |
57+
-- | 6 | 88 |
58+
-- +---------+---------+
59+
60+
-- Result table:
61+
-- +------------------+
62+
-- | recommended_page |
63+
-- +------------------+
64+
-- | 23 |
65+
-- | 24 |
66+
-- | 56 |
67+
-- | 33 |
68+
-- | 77 |
69+
-- +------------------+
70+
-- User one is friend with users 2, 3, 4 and 6.
71+
-- Suggested pages are 23 from user 2, 24 from user 3, 56 from user 3 and 33 from user 6.
72+
-- Page 77 is suggested from both user 2 and user 3.
73+
-- Page 88 is not suggested because user 1 already likes it.
74+
75+
-- Solution
76+
select distinct page_id as recommended_page
77+
from likes
78+
where user_id =
79+
any(select user2_id as id
80+
from friendship
81+
where user1_id = 1 or user2_id = 1 and user2_id !=1
82+
union all
83+
select user1_id
84+
from friendship
85+
where user2_id = 1)
86+
and page_id != all(select page_id from likes where user_id = 1)

0 commit comments

Comments
 (0)