Skip to content

Commit 0cef76c

Browse files
authored
Attempted 4 today
1 parent 70f55c1 commit 0cef76c

File tree

4 files changed

+218
-0
lines changed

4 files changed

+218
-0
lines changed

Easy/Delete duplicate emails.sql

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
-- Question 32
2+
-- Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id.
3+
4+
-- +----+------------------+
5+
-- | Id | Email |
6+
-- +----+------------------+
7+
8+
9+
10+
-- +----+------------------+
11+
-- Id is the primary key column for this table.
12+
-- For example, after running your query, the above Person table should have the following rows:
13+
14+
-- +----+------------------+
15+
-- | Id | Email |
16+
-- +----+------------------+
17+
18+
19+
-- +----+------------------+
20+
21+
-- Solution
22+
With t1 as
23+
(
24+
Select *,
25+
row_number() over(partition by email order by id) as rk
26+
from person
27+
)
28+
Delete from person
29+
where id in (Select t1.id from t1 where t1.rk>1)

Easy/Sales Analysis 2.sql

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
-- Question 33
2+
-- Table: Product
3+
4+
-- +--------------+---------+
5+
-- | Column Name | Type |
6+
-- +--------------+---------+
7+
-- | product_id | int |
8+
-- | product_name | varchar |
9+
-- | unit_price | int |
10+
-- +--------------+---------+
11+
-- product_id is the primary key of this table.
12+
-- Table: Sales
13+
14+
-- +-------------+---------+
15+
-- | Column Name | Type |
16+
-- +-------------+---------+
17+
-- | seller_id | int |
18+
-- | product_id | int |
19+
-- | buyer_id | int |
20+
-- | sale_date | date |
21+
-- | quantity | int |
22+
-- | price | int |
23+
-- +------ ------+---------+
24+
-- This table has no primary key, it can have repeated rows.
25+
-- product_id is a foreign key to Product table.
26+
27+
28+
-- Write an SQL query that reports the buyers who have bought S8 but not iPhone. Note that S8 and iPhone are products present in the Product table.
29+
30+
-- The query result format is in the following example:
31+
32+
-- Product table:
33+
-- +------------+--------------+------------+
34+
-- | product_id | product_name | unit_price |
35+
-- +------------+--------------+------------+
36+
-- | 1 | S8 | 1000 |
37+
-- | 2 | G4 | 800 |
38+
-- | 3 | iPhone | 1400 |
39+
-- +------------+--------------+------------+
40+
41+
-- Sales table:
42+
-- +-----------+------------+----------+------------+----------+-------+
43+
-- | seller_id | product_id | buyer_id | sale_date | quantity | price |
44+
-- +-----------+------------+----------+------------+----------+-------+
45+
-- | 1 | 1 | 1 | 2019-01-21 | 2 | 2000 |
46+
-- | 1 | 2 | 2 | 2019-02-17 | 1 | 800 |
47+
-- | 2 | 1 | 3 | 2019-06-02 | 1 | 800 |
48+
-- | 3 | 3 | 3 | 2019-05-13 | 2 | 2800 |
49+
-- +-----------+------------+----------+------------+----------+-------+
50+
51+
-- Result table:
52+
-- +-------------+
53+
-- | buyer_id |
54+
-- +-------------+
55+
-- | 1 |
56+
-- +-------------+
57+
-- The buyer with id 1 bought an S8 but didn't buy an iPhone. The buyer with id 3 bought both.
58+
59+
-- Solution
60+
Select distinct a.buyer_id
61+
from sales a join
62+
product b
63+
on a.product_id = b.product_id
64+
where a.buyer_id in
65+
(Select a.buyer_id from sales a join product b on a.product_id = b.product_id where b.product_name = 'S8')
66+
and
67+
a.buyer_id not in (Select a.buyer_id from sales a join product b on a.product_id = b.product_id where b.product_name = 'iPhone')

Easy/Sales Analysis 3.sql

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
-- Question 34
2+
-- Table: Product
3+
4+
-- +--------------+---------+
5+
-- | Column Name | Type |
6+
-- +--------------+---------+
7+
-- | product_id | int |
8+
-- | product_name | varchar |
9+
-- | unit_price | int |
10+
-- +--------------+---------+
11+
-- product_id is the primary key of this table.
12+
-- Table: Sales
13+
14+
-- +-------------+---------+
15+
-- | Column Name | Type |
16+
-- +-------------+---------+
17+
-- | seller_id | int |
18+
-- | product_id | int |
19+
-- | buyer_id | int |
20+
-- | sale_date | date |
21+
-- | quantity | int |
22+
-- | price | int |
23+
-- +------ ------+---------+
24+
-- This table has no primary key, it can have repeated rows.
25+
-- product_id is a foreign key to Product table.
26+
27+
28+
-- Write an SQL query that reports the products that were only sold in spring 2019. That is, between 2019-01-01 and 2019-03-31 inclusive.
29+
30+
-- The query result format is in the following example:
31+
32+
-- Product table:
33+
-- +------------+--------------+------------+
34+
-- | product_id | product_name | unit_price |
35+
-- +------------+--------------+------------+
36+
-- | 1 | S8 | 1000 |
37+
-- | 2 | G4 | 800 |
38+
-- | 3 | iPhone | 1400 |
39+
-- +------------+--------------+------------+
40+
41+
-- Sales table:
42+
-- +-----------+------------+----------+------------+----------+-------+
43+
-- | seller_id | product_id | buyer_id | sale_date | quantity | price |
44+
-- +-----------+------------+----------+------------+----------+-------+
45+
-- | 1 | 1 | 1 | 2019-01-21 | 2 | 2000 |
46+
-- | 1 | 2 | 2 | 2019-02-17 | 1 | 800 |
47+
-- | 2 | 2 | 3 | 2019-06-02 | 1 | 800 |
48+
-- | 3 | 3 | 4 | 2019-05-13 | 2 | 2800 |
49+
-- +-----------+------------+----------+------------+----------+-------+
50+
51+
-- Result table:
52+
-- +-------------+--------------+
53+
-- | product_id | product_name |
54+
-- +-------------+--------------+
55+
-- | 1 | S8 |
56+
-- +-------------+--------------+
57+
-- The product with id 1 was only sold in spring 2019 while the other two were sold after.
58+
59+
-- Solution
60+
select distinct a.product_id, product_name from sales a join product b on a.product_id = b.product_id where a.product_id
61+
in
62+
(select product_id from sales where sale_date >= '2019-01-01' and sale_date <= '2019-03-31')
63+
and
64+
a.product_id not in
65+
(select product_id from sales where sale_date > '2019-03-31' or sale_date < '2019-01-01')
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
-- Question 35
2+
-- Table: Activity
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | user_id | int |
8+
-- | session_id | int |
9+
-- | activity_date | date |
10+
-- | activity_type | enum |
11+
-- +---------------+---------+
12+
-- There is no primary key for this table, it may have duplicate rows.
13+
-- The activity_type column is an ENUM of type ('open_session', 'end_session', 'scroll_down', 'send_message').
14+
-- The table shows the user activities for a social media website.
15+
-- Note that each session belongs to exactly one user.
16+
17+
18+
-- Write an SQL query to find the average number of sessions per user for a period of 30 days ending 2019-07-27 inclusively, rounded to 2 decimal places. The sessions we want to count for a user are those with at least one activity in that time period.
19+
20+
-- The query result format is in the following example:
21+
22+
-- Activity table:
23+
-- +---------+------------+---------------+---------------+
24+
-- | user_id | session_id | activity_date | activity_type |
25+
-- +---------+------------+---------------+---------------+
26+
-- | 1 | 1 | 2019-07-20 | open_session |
27+
-- | 1 | 1 | 2019-07-20 | scroll_down |
28+
-- | 1 | 1 | 2019-07-20 | end_session |
29+
-- | 2 | 4 | 2019-07-20 | open_session |
30+
-- | 2 | 4 | 2019-07-21 | send_message |
31+
-- | 2 | 4 | 2019-07-21 | end_session |
32+
-- | 3 | 2 | 2019-07-21 | open_session |
33+
-- | 3 | 2 | 2019-07-21 | send_message |
34+
-- | 3 | 2 | 2019-07-21 | end_session |
35+
-- | 3 | 5 | 2019-07-21 | open_session |
36+
-- | 3 | 5 | 2019-07-21 | scroll_down |
37+
-- | 3 | 5 | 2019-07-21 | end_session |
38+
-- | 4 | 3 | 2019-06-25 | open_session |
39+
-- | 4 | 3 | 2019-06-25 | end_session |
40+
-- +---------+------------+---------------+---------------+
41+
42+
-- Result table:
43+
-- +---------------------------+
44+
-- | average_sessions_per_user |
45+
-- +---------------------------+
46+
-- | 1.33 |
47+
-- +---------------------------+
48+
-- User 1 and 2 each had 1 session in the past 30 days while user 3 had 2 sessions so the average is (1 + 1 + 2) / 3 = 1.33.
49+
50+
51+
-- Solution
52+
select ifnull(round(avg(a.num),2),0) as average_sessions_per_user
53+
from (
54+
select count(distinct session_id) as num
55+
from activity
56+
where activity_date between '2019-06-28' and '2019-07-27'
57+
group by user_id) a

0 commit comments

Comments
 (0)