Skip to content

Commit a11f304

Browse files
authored
Add files via upload
1 parent 84ec9b4 commit a11f304

File tree

4 files changed

+287
-0
lines changed

4 files changed

+287
-0
lines changed

Medium/Active Users.sql

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
--Question 94
2+
-- Table Accounts:
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | id | int |
8+
-- | name | varchar |
9+
-- +---------------+---------+
10+
-- the id is the primary key for this table.
11+
-- This table contains the account id and the user name of each account.
12+
13+
14+
-- Table Logins:
15+
16+
-- +---------------+---------+
17+
-- | Column Name | Type |
18+
-- +---------------+---------+
19+
-- | id | int |
20+
-- | login_date | date |
21+
-- +---------------+---------+
22+
-- There is no primary key for this table, it may contain duplicates.
23+
-- This table contains the account id of the user who logged in and the login date. A user may log in multiple times in the day.
24+
25+
26+
-- Write an SQL query to find the id and the name of active users.
27+
28+
-- Active users are those who logged in to their accounts for 5 or more consecutive days.
29+
30+
-- Return the result table ordered by the id.
31+
32+
-- The query result format is in the following example:
33+
34+
-- Accounts table:
35+
-- +----+----------+
36+
-- | id | name |
37+
-- +----+----------+
38+
-- | 1 | Winston |
39+
-- | 7 | Jonathan |
40+
-- +----+----------+
41+
42+
-- Logins table:
43+
-- +----+------------+
44+
-- | id | login_date |
45+
-- +----+------------+
46+
-- | 7 | 2020-05-30 |
47+
-- | 1 | 2020-05-30 |
48+
-- | 7 | 2020-05-31 |
49+
-- | 7 | 2020-06-01 |
50+
-- | 7 | 2020-06-02 |
51+
-- | 7 | 2020-06-02 |
52+
-- | 7 | 2020-06-03 |
53+
-- | 1 | 2020-06-07 |
54+
-- | 7 | 2020-06-10 |
55+
-- +----+------------+
56+
57+
-- Result table:
58+
-- +----+----------+
59+
-- | id | name |
60+
-- +----+----------+
61+
-- | 7 | Jonathan |
62+
-- +----+----------+
63+
-- User Winston with id = 1 logged in 2 times only in 2 different days, so, Winston is not an active user.
64+
-- User Jonathan with id = 7 logged in 7 times in 6 different days, five of them were consecutive days, so, Jonathan is an active user.
65+
66+
-- Solution
67+
with t1 as (
68+
select id,login_date,
69+
lead(login_date,4) over(partition by id order by login_date) date_5
70+
from (select distinct * from Logins) b
71+
)
72+
73+
select distinct a.id, a.name from t1
74+
inner join accounts a
75+
on t1.id = a.id
76+
where datediff(t1.date_5,login_date) = 4
77+
order by id
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
-- Question 93
2+
-- Table: Customer
3+
4+
-- +-------------+---------+
5+
-- | Column Name | Type |
6+
-- +-------------+---------+
7+
-- | customer_id | int |
8+
-- | product_key | int |
9+
-- +-------------+---------+
10+
-- product_key is a foreign key to Product table.
11+
-- Table: Product
12+
13+
-- +-------------+---------+
14+
-- | Column Name | Type |
15+
-- +-------------+---------+
16+
-- | product_key | int |
17+
-- +-------------+---------+
18+
-- product_key is the primary key column for this table.
19+
20+
21+
-- Write an SQL query for a report that provides the customer ids from the Customer table that bought all the products in the Product table.
22+
23+
-- For example:
24+
25+
-- Customer table:
26+
-- +-------------+-------------+
27+
-- | customer_id | product_key |
28+
-- +-------------+-------------+
29+
-- | 1 | 5 |
30+
-- | 2 | 6 |
31+
-- | 3 | 5 |
32+
-- | 3 | 6 |
33+
-- | 1 | 6 |
34+
-- +-------------+-------------+
35+
36+
-- Product table:
37+
-- +-------------+
38+
-- | product_key |
39+
-- +-------------+
40+
-- | 5 |
41+
-- | 6 |
42+
-- +-------------+
43+
44+
-- Result table:
45+
-- +-------------+
46+
-- | customer_id |
47+
-- +-------------+
48+
-- | 1 |
49+
-- | 3 |
50+
-- +-------------+
51+
-- The customers who bought all the products (5 and 6) are customers with id 1 and 3.
52+
53+
-- Solution
54+
select customer_id
55+
from customer
56+
group by customer_id
57+
having count(distinct product_key) = (select COUNT(distinct product_key) from product)

Medium/Monthly Transaction 2.sql

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
-- Question 95
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+
-- Table: Chargebacks
17+
18+
-- +----------------+---------+
19+
-- | Column Name | Type |
20+
-- +----------------+---------+
21+
-- | trans_id | int |
22+
-- | charge_date | date |
23+
-- +----------------+---------+
24+
-- Chargebacks contains basic information regarding incoming chargebacks from some transactions placed in Transactions table.
25+
-- trans_id is a foreign key to the id column of Transactions table.
26+
-- Each chargeback corresponds to a transaction made previously even if they were not approved.
27+
28+
29+
-- Write an SQL query to find for each month and country, the number of approved transactions and their total amount, the number of chargebacks and their total amount.
30+
31+
-- Note: In your query, given the month and country, ignore rows with all zeros.
32+
33+
-- The query result format is in the following example:
34+
35+
-- Transactions table:
36+
-- +------+---------+----------+--------+------------+
37+
-- | id | country | state | amount | trans_date |
38+
-- +------+---------+----------+--------+------------+
39+
-- | 101 | US | approved | 1000 | 2019-05-18 |
40+
-- | 102 | US | declined | 2000 | 2019-05-19 |
41+
-- | 103 | US | approved | 3000 | 2019-06-10 |
42+
-- | 104 | US | approved | 4000 | 2019-06-13 |
43+
-- | 105 | US | approved | 5000 | 2019-06-15 |
44+
-- +------+---------+----------+--------+------------+
45+
46+
-- Chargebacks table:
47+
-- +------------+------------+
48+
-- | trans_id | trans_date |
49+
-- +------------+------------+
50+
-- | 102 | 2019-05-29 |
51+
-- | 101 | 2019-06-30 |
52+
-- | 105 | 2019-09-18 |
53+
-- +------------+------------+
54+
55+
-- Result table:
56+
-- +----------+---------+----------------+-----------------+-------------------+--------------------+
57+
-- | month | country | approved_count | approved_amount | chargeback_count | chargeback_amount |
58+
-- +----------+---------+----------------+-----------------+-------------------+--------------------+
59+
-- | 2019-05 | US | 1 | 1000 | 1 | 2000 |
60+
-- | 2019-06 | US | 3 | 12000 | 1 | 1000 |
61+
-- | 2019-09 | US | 0 | 0 | 1 | 5000 |
62+
-- +----------+---------+----------------+-----------------+-------------------+--------------------+
63+
64+
-- Solution
65+
with t1 as
66+
(select country, extract('month' from trans_date), state, count(*) as approved_count, sum(amount) as approved_amount
67+
from transactions
68+
where state = 'approved'
69+
group by 1, 2, 3),
70+
t2 as(
71+
select t.country, extract('month' from c.trans_date), sum(amount) as chargeback_amount, count(*) as chargeback_count
72+
from chargebacks c left join transactions t
73+
on trans_id = id
74+
group by t.country, extract('month' from c.trans_date)),
75+
76+
t3 as(
77+
select t2.date_part, t2.country, coalesce(approved_count,0) as approved_count, coalesce(approved_amount,0) as approved_amount, coalesce(chargeback_count,0) as chargeback_count, coalesce(chargeback_amount,0) as chargeback_amount
78+
from t2 left join t1
79+
on t2.date_part = t1.date_part and t2.country = t1.country),
80+
81+
t4 as(
82+
select t1.date_part, t1.country, coalesce(approved_count,0) as approved_count, coalesce(approved_amount,0) as approved_amount, coalesce(chargeback_count,0) as chargeback_count, coalesce(chargeback_amount,0) as chargeback_amount
83+
from t2 right join t1
84+
on t2.date_part = t1.date_part and t2.country = t1.country)
85+
86+
select *
87+
from t3
88+
union
89+
select *
90+
from t4

Medium/New users daily count.sql

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
-- Question 92
2+
-- Table: Traffic
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | user_id | int |
8+
-- | activity | enum |
9+
-- | activity_date | date |
10+
-- +---------------+---------+
11+
-- There is no primary key for this table, it may have duplicate rows.
12+
-- The activity column is an ENUM type of ('login', 'logout', 'jobs', 'groups', 'homepage').
13+
14+
15+
-- Write an SQL query that reports for every date within at most 90 days from today,
16+
-- the number of users that logged in for the first time on that date. Assume today is 2019-06-30.
17+
18+
-- The query result format is in the following example:
19+
20+
-- Traffic table:
21+
-- +---------+----------+---------------+
22+
-- | user_id | activity | activity_date |
23+
-- +---------+----------+---------------+
24+
-- | 1 | login | 2019-05-01 |
25+
-- | 1 | homepage | 2019-05-01 |
26+
-- | 1 | logout | 2019-05-01 |
27+
-- | 2 | login | 2019-06-21 |
28+
-- | 2 | logout | 2019-06-21 |
29+
-- | 3 | login | 2019-01-01 |
30+
-- | 3 | jobs | 2019-01-01 |
31+
-- | 3 | logout | 2019-01-01 |
32+
-- | 4 | login | 2019-06-21 |
33+
-- | 4 | groups | 2019-06-21 |
34+
-- | 4 | logout | 2019-06-21 |
35+
-- | 5 | login | 2019-03-01 |
36+
-- | 5 | logout | 2019-03-01 |
37+
-- | 5 | login | 2019-06-21 |
38+
-- | 5 | logout | 2019-06-21 |
39+
-- +---------+----------+---------------+
40+
41+
-- Result table:
42+
-- +------------+-------------+
43+
-- | login_date | user_count |
44+
-- +------------+-------------+
45+
-- | 2019-05-01 | 1 |
46+
-- | 2019-06-21 | 2 |
47+
-- +------------+-------------+
48+
-- Note that we only care about dates with non zero user count.
49+
-- The user with id 5 first logged in on 2019-03-01 so he's not counted on 2019-06-21.
50+
51+
-- Solution
52+
with t1 as
53+
(
54+
select user_id, min(activity_date) as login_date
55+
from Traffic
56+
where activity = 'login'
57+
group by user_id
58+
)
59+
60+
select login_date, count(distinct user_id) as user_count
61+
from t1
62+
where login_date between '2019-04-01' and '2019-06-30'
63+
group by login_date

0 commit comments

Comments
 (0)