Skip to content

Commit db8f0d6

Browse files
committed
solve: questions faizanxmulla#13, #14
1 parent fccd989 commit db8f0d6

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
### Problem Statement
2+
3+
Write an SQL query to find all pairs of products that are purchased together by the same customer in the same order.
4+
5+
For each pair, return the product names and the frequency with which the pair was purchased together.
6+
7+
### Schema Setup
8+
9+
```sql
10+
CREATE TABLE orders (
11+
order_id INT,
12+
customer_id INT,
13+
product_id INT
14+
);
15+
16+
INSERT INTO orders (order_id, customer_id, product_id) VALUES
17+
(1, 1, 1),
18+
(2, 1, 2),
19+
(3, 1, 3),
20+
(4, 2, 1),
21+
(5, 2, 2);
22+
23+
24+
CREATE TABLE products (
25+
id INT PRIMARY KEY,
26+
name VARCHAR(50)
27+
);
28+
29+
INSERT INTO products (id, name) VALUES
30+
(1, 'A'),
31+
(2, 'B'),
32+
(3, 'C'),
33+
(4, 'D'),
34+
(5, 'E');
35+
```
36+
37+
### Expected Output
38+
39+
| pair | purchase_freq |
40+
|------|---------------|
41+
| A B | 2 |
42+
| A C | 1 |
43+
| B C | 1 |
44+
| A D | 1 |
45+
| B D | 1 |
46+
47+
48+
49+
### Solution Query
50+
51+
```sql
52+
SELECT CONCAT(pr1.name, ' ' , pr2.name) AS pair, COUNT(*) AS purchase_freq
53+
FROM orders o1 JOIN orders o2 ON o1.order_id = o2.order_id
54+
JOIN products pr1 ON pr1.id = o1.product_id
55+
JOIN products pr2 ON pr2.id = o2.product_id
56+
WHERE o1.product_id < o2.product_id
57+
GROUP BY 1
58+
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
### Problem Statement
2+
3+
Given the `users` and `events` tables, return the fraction of users, rounded to two decimal places, who accessed Amazon Music and upgraded to Prime membership within the first 30 days of signing up.
4+
5+
The output should return a single value representing the fraction of users who meet the above criteria.
6+
7+
8+
### Schema Setup
9+
10+
```sql
11+
CREATE TABLE users (
12+
user_id INT PRIMARY KEY,
13+
name VARCHAR(50),
14+
join_date DATE
15+
);
16+
17+
INSERT INTO users (user_id, name, join_date) VALUES
18+
(1, 'Jon', '2020-02-14'),
19+
(2, 'Jane', '2020-02-14'),
20+
(3, 'Jill', '2020-02-14'),
21+
(4, 'Josh', '2020-02-15'),
22+
(5, 'Jean', '2020-02-16'),
23+
(6, 'Justin', '2020-02-17'),
24+
(7, 'Jeremy', '2020-02-18');
25+
26+
CREATE TABLE events (
27+
user_id INT,
28+
type VARCHAR(50),
29+
access_date DATE
30+
);
31+
32+
INSERT INTO events (user_id, type, access_date) VALUES
33+
(1, 'Pay', '2020-03-01'),
34+
(2, 'MLack', '2020-03-02'),
35+
(2, 'P', '2020-03-12'),
36+
(3, 'Music', '2020-03-15'),
37+
(3, 'P', '2020-03-15'),
38+
(6, 'P', '2020-03-16'),
39+
(7, 'P', '2020-03-22');
40+
```
41+
42+
43+
### Expected Output
44+
45+
fraction |
46+
--|
47+
33.33 |
48+
49+
### Solution Query
50+
51+
```sql
52+
WITH events_cte AS (
53+
SELECT user_id,
54+
access_date,
55+
COUNT(*) FILTER(WHERE type='Music') as music_members,
56+
COUNT(*) FILTER(WHERE type='P') as prime_members
57+
FROM events
58+
GROUP BY 1, 2
59+
)
60+
SELECT 100.0 * ROUND(SUM(music_members) / SUM(prime_members), 2) as fraction
61+
FROM events_cte e JOIN users u USING(user_id)
62+
WHERE access_date <= join_date + INTERVAL '30 day'
63+
```

0 commit comments

Comments
 (0)