Skip to content

Commit 397cc28

Browse files
authored
Attempted 5 more today
1 parent 99665a8 commit 397cc28

5 files changed

+281
-0
lines changed

Easy/Average selling price.sql

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
-- Question 39
2+
-- Table: Prices
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | product_id | int |
8+
-- | start_date | date |
9+
-- | end_date | date |
10+
-- | price | int |
11+
-- +---------------+---------+
12+
-- (product_id, start_date, end_date) is the primary key for this table.
13+
-- Each row of this table indicates the price of the product_id in the period from start_date to end_date.
14+
-- For each product_id there will be no two overlapping periods. That means there will be no two intersecting periods for the same product_id.
15+
16+
17+
-- Table: UnitsSold
18+
19+
-- +---------------+---------+
20+
-- | Column Name | Type |
21+
-- +---------------+---------+
22+
-- | product_id | int |
23+
-- | purchase_date | date |
24+
-- | units | int |
25+
-- +---------------+---------+
26+
-- There is no primary key for this table, it may contain duplicates.
27+
-- Each row of this table indicates the date, units and product_id of each product sold.
28+
29+
30+
-- Write an SQL query to find the average selling price for each product.
31+
32+
-- average_price should be rounded to 2 decimal places.
33+
34+
-- The query result format is in the following example:
35+
36+
-- Prices table:
37+
-- +------------+------------+------------+--------+
38+
-- | product_id | start_date | end_date | price |
39+
-- +------------+------------+------------+--------+
40+
-- | 1 | 2019-02-17 | 2019-02-28 | 5 |
41+
-- | 1 | 2019-03-01 | 2019-03-22 | 20 |
42+
-- | 2 | 2019-02-01 | 2019-02-20 | 15 |
43+
-- | 2 | 2019-02-21 | 2019-03-31 | 30 |
44+
-- +------------+------------+------------+--------+
45+
46+
-- UnitsSold table:
47+
-- +------------+---------------+-------+
48+
-- | product_id | purchase_date | units |
49+
-- +------------+---------------+-------+
50+
-- | 1 | 2019-02-25 | 100 |
51+
-- | 1 | 2019-03-01 | 15 |
52+
-- | 2 | 2019-02-10 | 200 |
53+
-- | 2 | 2019-03-22 | 30 |
54+
-- +------------+---------------+-------+
55+
56+
-- Result table:
57+
-- +------------+---------------+
58+
-- | product_id | average_price |
59+
-- +------------+---------------+
60+
-- | 1 | 6.96 |
61+
-- | 2 | 16.96 |
62+
-- +------------+---------------+
63+
-- Average selling price = Total Price of Product / Number of products sold.
64+
-- Average selling price for product 1 = ((100 * 5) + (15 * 20)) / 115 = 6.96
65+
-- Average selling price for product 2 = ((200 * 15) + (30 * 30)) / 230 = 16.96
66+
67+
-- Solution
68+
Select d.product_id, round((sum(price*units)+0.00)/(sum(units)+0.00),2) as average_price
69+
from(
70+
Select *
71+
from prices p
72+
natural join
73+
unitssold u
74+
where u.purchase_date between p.start_date and p.end_date) d
75+
group by d.product_id
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
-- Question 37
2+
-- Several friends at a cinema ticket office would like to reserve consecutive available seats.
3+
-- Can you help to query all the consecutive available seats order by the seat_id using the following cinema table?
4+
-- | seat_id | free |
5+
-- |---------|------|
6+
-- | 1 | 1 |
7+
-- | 2 | 0 |
8+
-- | 3 | 1 |
9+
-- | 4 | 1 |
10+
-- | 5 | 1 |
11+
12+
13+
-- Your query should return the following result for the sample case above.
14+
15+
16+
-- | seat_id |
17+
-- |---------|
18+
-- | 3 |
19+
-- | 4 |
20+
-- | 5 |
21+
-- Note:
22+
-- The seat_id is an auto increment int, and free is bool ('1' means free, and '0' means occupied.).
23+
-- Consecutive available seats are more than 2(inclusive) seats consecutively available.
24+
25+
-- Solution
26+
Select seat_id
27+
from(
28+
select seat_id, free,
29+
lead(free,1) over() as next,
30+
lag(free,1) over() as prev
31+
from cinema) a
32+
where a.free=True and (next = True or prev=True)
33+
order by seat_id

Easy/Immediate food delivery.sql

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
-- Question 38
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+
-- Write an SQL query to find the percentage of immediate orders in the table, rounded to 2 decimal places.
19+
20+
-- The query result format is in the following example:
21+
22+
-- Delivery table:
23+
-- +-------------+-------------+------------+-----------------------------+
24+
-- | delivery_id | customer_id | order_date | customer_pref_delivery_date |
25+
-- +-------------+-------------+------------+-----------------------------+
26+
-- | 1 | 1 | 2019-08-01 | 2019-08-02 |
27+
-- | 2 | 5 | 2019-08-02 | 2019-08-02 |
28+
-- | 3 | 1 | 2019-08-11 | 2019-08-11 |
29+
-- | 4 | 3 | 2019-08-24 | 2019-08-26 |
30+
-- | 5 | 4 | 2019-08-21 | 2019-08-22 |
31+
-- | 6 | 2 | 2019-08-11 | 2019-08-13 |
32+
-- +-------------+-------------+------------+-----------------------------+
33+
34+
-- Result table:
35+
-- +----------------------+
36+
-- | immediate_percentage |
37+
-- +----------------------+
38+
-- | 33.33 |
39+
-- +----------------------+
40+
-- The orders with delivery id 2 and 3 are immediate while the others are scheduled.
41+
42+
-- Solution
43+
Select
44+
Round(avg(case when order_date=customer_pref_delivery_date then 1 else 0 end)*100,2) as immediate_percentage
45+
from delivery
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
-- Question 36
2+
-- Table: Departments
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | id | int |
8+
-- | name | varchar |
9+
-- +---------------+---------+
10+
-- id is the primary key of this table.
11+
-- The table has information about the id of each department of a university.
12+
13+
14+
-- Table: Students
15+
16+
-- +---------------+---------+
17+
-- | Column Name | Type |
18+
-- +---------------+---------+
19+
-- | id | int |
20+
-- | name | varchar |
21+
-- | department_id | int |
22+
-- +---------------+---------+
23+
-- id is the primary key of this table.
24+
-- The table has information about the id of each student at a university and the id of the department he/she studies at.
25+
26+
27+
-- Write an SQL query to find the id and the name of all students who are enrolled in departments that no longer exists.
28+
29+
-- Return the result table in any order.
30+
31+
-- The query result format is in the following example:
32+
33+
-- Departments table:
34+
-- +------+--------------------------+
35+
-- | id | name |
36+
-- +------+--------------------------+
37+
-- | 1 | Electrical Engineering |
38+
-- | 7 | Computer Engineering |
39+
-- | 13 | Bussiness Administration |
40+
-- +------+--------------------------+
41+
42+
-- Students table:
43+
-- +------+----------+---------------+
44+
-- | id | name | department_id |
45+
-- +------+----------+---------------+
46+
-- | 23 | Alice | 1 |
47+
-- | 1 | Bob | 7 |
48+
-- | 5 | Jennifer | 13 |
49+
-- | 2 | John | 14 |
50+
-- | 4 | Jasmine | 77 |
51+
-- | 3 | Steve | 74 |
52+
-- | 6 | Luis | 1 |
53+
-- | 8 | Jonathan | 7 |
54+
-- | 7 | Daiana | 33 |
55+
-- | 11 | Madelynn | 1 |
56+
-- +------+----------+---------------+
57+
58+
-- Result table:
59+
-- +------+----------+
60+
-- | id | name |
61+
-- +------+----------+
62+
-- | 2 | John |
63+
-- | 7 | Daiana |
64+
-- | 4 | Jasmine |
65+
-- | 3 | Steve |
66+
-- +------+----------+
67+
68+
-- John, Daiana, Steve and Jasmine are enrolled in departments 14, 33, 74 and 77 respectively.
69+
-- department 14, 33, 74 and 77 doesn't exist in the Departments table.
70+
71+
-- Solution
72+
Select s.id, s.name
73+
from students s left join
74+
departments d
75+
on s.department_id = d.id
76+
where d.name is null
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
-- Question 40
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 daily active user count for a period of 30 days ending 2019-07-27 inclusively. A user was active on some day if he/she made at least one activity on that day.
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+
-- | 4 | 3 | 2019-06-25 | open_session |
36+
-- | 4 | 3 | 2019-06-25 | end_session |
37+
-- +---------+------------+---------------+---------------+
38+
39+
-- Result table:
40+
-- +------------+--------------+
41+
-- | day | active_users |
42+
-- +------------+--------------+
43+
-- | 2019-07-20 | 2 |
44+
-- | 2019-07-21 | 2 |
45+
-- +------------+--------------+
46+
-- Note that we do not care about days with zero active users.
47+
48+
-- Solution
49+
Select activity_date as day, count(distinct user_id) as active_users
50+
from activity
51+
where activity_date > '2019-06-26' and activity_date < '2019-07-27'
52+
group by activity_date

0 commit comments

Comments
 (0)