Skip to content

Commit c7974cb

Browse files
authored
Add files via upload
1 parent cc7b23f commit c7974cb

7 files changed

+530
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
-- Question 107
2+
-- The Numbers table keeps the value of number and its frequency.
3+
4+
-- +----------+-------------+
5+
-- | Number | Frequency |
6+
-- +----------+-------------|
7+
-- | 0 | 7 |
8+
-- | 1 | 1 |
9+
-- | 2 | 3 |
10+
-- | 3 | 1 |
11+
-- +----------+-------------+
12+
-- In this table, the numbers are 0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 3, so the median is (0 + 0) / 2 = 0.
13+
14+
-- +--------+
15+
-- | median |
16+
-- +--------|
17+
-- | 0.0000 |
18+
-- +--------+
19+
-- Write a query to find the median of all numbers and name the result as median.
20+
21+
-- Solution
22+
with t1 as(
23+
select *,
24+
sum(frequency) over(order by number) as cum_sum, (sum(frequency) over())/2 as middle
25+
from numbers)
26+
27+
select avg(number) as median
28+
from t1
29+
where middle between (cum_sum - frequency) and cum_sum
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
-- Question 106
2+
-- Table: Student
3+
4+
-- +---------------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------------+---------+
7+
-- | student_id | int |
8+
-- | student_name | varchar |
9+
-- +---------------------+---------+
10+
-- student_id is the primary key for this table.
11+
-- student_name is the name of the student.
12+
13+
14+
-- Table: Exam
15+
16+
-- +---------------+---------+
17+
-- | Column Name | Type |
18+
-- +---------------+---------+
19+
-- | exam_id | int |
20+
-- | student_id | int |
21+
-- | score | int |
22+
-- +---------------+---------+
23+
-- (exam_id, student_id) is the primary key for this table.
24+
-- Student with student_id got score points in exam with id exam_id.
25+
26+
27+
-- A "quite" student is the one who took at least one exam and didn't score neither the high score nor the low score.
28+
29+
-- Write an SQL query to report the students (student_id, student_name) being "quiet" in ALL exams.
30+
31+
-- Don't return the student who has never taken any exam. Return the result table ordered by student_id.
32+
33+
-- The query result format is in the following example.
34+
35+
36+
37+
-- Student table:
38+
-- +-------------+---------------+
39+
-- | student_id | student_name |
40+
-- +-------------+---------------+
41+
-- | 1 | Daniel |
42+
-- | 2 | Jade |
43+
-- | 3 | Stella |
44+
-- | 4 | Jonathan |
45+
-- | 5 | Will |
46+
-- +-------------+---------------+
47+
48+
-- Exam table:
49+
-- +------------+--------------+-----------+
50+
-- | exam_id | student_id | score |
51+
-- +------------+--------------+-----------+
52+
-- | 10 | 1 | 70 |
53+
-- | 10 | 2 | 80 |
54+
-- | 10 | 3 | 90 |
55+
-- | 20 | 1 | 80 |
56+
-- | 30 | 1 | 70 |
57+
-- | 30 | 3 | 80 |
58+
-- | 30 | 4 | 90 |
59+
-- | 40 | 1 | 60 |
60+
-- | 40 | 2 | 70 |
61+
-- | 40 | 4 | 80 |
62+
-- +------------+--------------+-----------+
63+
64+
-- Result table:
65+
-- +-------------+---------------+
66+
-- | student_id | student_name |
67+
-- +-------------+---------------+
68+
-- | 2 | Jade |
69+
-- +-------------+---------------+
70+
71+
-- For exam 1: Student 1 and 3 hold the lowest and high score respectively.
72+
-- For exam 2: Student 1 hold both highest and lowest score.
73+
-- For exam 3 and 4: Studnet 1 and 4 hold the lowest and high score respectively.
74+
-- Student 2 and 5 have never got the highest or lowest in any of the exam.
75+
-- Since student 5 is not taking any exam, he is excluded from the result.
76+
-- So, we only return the information of Student 2.
77+
78+
-- Solution
79+
with t1 as(
80+
select student_id
81+
from
82+
(select *,
83+
min(score) over(partition by exam_id) as least,
84+
max(score) over(partition by exam_id) as most
85+
from exam) a
86+
where least = score or most = score)
87+
88+
89+
select distinct student_id, student_name
90+
from exam join student
91+
using (student_id)
92+
where student_id != all(select student_id from t1)
93+
order by 1

Hard/Game Play Analysis 5.sql

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
-- Question 111
2+
-- Table: Activity
3+
4+
-- +--------------+---------+
5+
-- | Column Name | Type |
6+
-- +--------------+---------+
7+
-- | player_id | int |
8+
-- | device_id | int |
9+
-- | event_date | date |
10+
-- | games_played | int |
11+
-- +--------------+---------+
12+
-- (player_id, event_date) is the primary key of this table.
13+
-- This table shows the activity of players of some game.
14+
-- Each row is a record of a player who logged in and played a number of games (possibly 0) before logging out on some day using some device.
15+
16+
17+
-- We define the install date of a player to be the first login day of that player.
18+
19+
-- We also define day 1 retention of some date X to be the number of players whose install date is X and they logged back in on the day right after X, divided by the number of players whose install date is X, rounded to 2 decimal places.
20+
21+
-- Write an SQL query that reports for each install date, the number of players that installed the game on that day and the day 1 retention.
22+
23+
-- The query result format is in the following example:
24+
25+
-- Activity table:
26+
-- +-----------+-----------+------------+--------------+
27+
-- | player_id | device_id | event_date | games_played |
28+
-- +-----------+-----------+------------+--------------+
29+
-- | 1 | 2 | 2016-03-01 | 5 |
30+
-- | 1 | 2 | 2016-03-02 | 6 |
31+
-- | 2 | 3 | 2017-06-25 | 1 |
32+
-- | 3 | 1 | 2016-03-01 | 0 |
33+
-- | 3 | 4 | 2016-07-03 | 5 |
34+
-- +-----------+-----------+------------+--------------+
35+
36+
-- Result table:
37+
-- +------------+----------+----------------+
38+
-- | install_dt | installs | Day1_retention |
39+
-- +------------+----------+----------------+
40+
-- | 2016-03-01 | 2 | 0.50 |
41+
-- | 2017-06-25 | 1 | 0.00 |
42+
-- +------------+----------+----------------+
43+
-- Player 1 and 3 installed the game on 2016-03-01 but only player 1 logged back in on 2016-03-02 so the
44+
-- day 1 retention of 2016-03-01 is 1 / 2 = 0.50
45+
-- Player 2 installed the game on 2017-06-25 but didn't log back in on 2017-06-26 so the day 1 retention of 2017-06-25 is 0 / 1 = 0.00
46+
47+
-- Solution
48+
with t1 as(
49+
select *,
50+
row_number() over(partition by player_id order by event_date) as rnk,
51+
min(event_date) over(partition by player_id) as install_dt,
52+
lead(event_date,1) over(partition by player_id order by event_date) as nxt
53+
from Activity)
54+
55+
select distinct install_dt,
56+
count(distinct player_id) as installs,
57+
round(sum(case when nxt=event_date+1 then 1 else 0 end)/count(distinct player_id),2) as Day1_retention
58+
from t1
59+
where rnk = 1
60+
group by 1
61+
order by 1

Hard/Sales by day of the week.sql

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
-- Question 112
2+
-- Table: Orders
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | order_id | int |
8+
-- | customer_id | int |
9+
-- | order_date | date |
10+
-- | item_id | varchar |
11+
-- | quantity | int |
12+
-- +---------------+---------+
13+
-- (ordered_id, item_id) is the primary key for this table.
14+
-- This table contains information of the orders placed.
15+
-- order_date is the date when item_id was ordered by the customer with id customer_id.
16+
17+
18+
-- Table: Items
19+
20+
-- +---------------------+---------+
21+
-- | Column Name | Type |
22+
-- +---------------------+---------+
23+
-- | item_id | varchar |
24+
-- | item_name | varchar |
25+
-- | item_category | varchar |
26+
-- +---------------------+---------+
27+
-- item_id is the primary key for this table.
28+
-- item_name is the name of the item.
29+
-- item_category is the category of the item.
30+
31+
32+
-- You are the business owner and would like to obtain a sales report for category items and day of the week.
33+
34+
-- Write an SQL query to report how many units in each category have been ordered on each day of the week.
35+
36+
-- Return the result table ordered by category.
37+
38+
-- The query result format is in the following example:
39+
40+
41+
42+
-- Orders table:
43+
-- +------------+--------------+-------------+--------------+-------------+
44+
-- | order_id | customer_id | order_date | item_id | quantity |
45+
-- +------------+--------------+-------------+--------------+-------------+
46+
-- | 1 | 1 | 2020-06-01 | 1 | 10 |
47+
-- | 2 | 1 | 2020-06-08 | 2 | 10 |
48+
-- | 3 | 2 | 2020-06-02 | 1 | 5 |
49+
-- | 4 | 3 | 2020-06-03 | 3 | 5 |
50+
-- | 5 | 4 | 2020-06-04 | 4 | 1 |
51+
-- | 6 | 4 | 2020-06-05 | 5 | 5 |
52+
-- | 7 | 5 | 2020-06-05 | 1 | 10 |
53+
-- | 8 | 5 | 2020-06-14 | 4 | 5 |
54+
-- | 9 | 5 | 2020-06-21 | 3 | 5 |
55+
-- +------------+--------------+-------------+--------------+-------------+
56+
57+
-- Items table:
58+
-- +------------+----------------+---------------+
59+
-- | item_id | item_name | item_category |
60+
-- +------------+----------------+---------------+
61+
-- | 1 | LC Alg. Book | Book |
62+
-- | 2 | LC DB. Book | Book |
63+
-- | 3 | LC SmarthPhone | Phone |
64+
-- | 4 | LC Phone 2020 | Phone |
65+
-- | 5 | LC SmartGlass | Glasses |
66+
-- | 6 | LC T-Shirt XL | T-Shirt |
67+
-- +------------+----------------+---------------+
68+
69+
-- Result table:
70+
-- +------------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
71+
-- | Category | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday |
72+
-- +------------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
73+
-- | Book | 20 | 5 | 0 | 0 | 10 | 0 | 0 |
74+
-- | Glasses | 0 | 0 | 0 | 0 | 5 | 0 | 0 |
75+
-- | Phone | 0 | 0 | 5 | 1 | 0 | 0 | 10 |
76+
-- | T-Shirt | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
77+
-- +------------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
78+
-- On Monday (2020-06-01, 2020-06-08) were sold a total of 20 units (10 + 10) in the category Book (ids: 1, 2).
79+
-- On Tuesday (2020-06-02) were sold a total of 5 units in the category Book (ids: 1, 2).
80+
-- On Wednesday (2020-06-03) were sold a total of 5 units in the category Phone (ids: 3, 4).
81+
-- On Thursday (2020-06-04) were sold a total of 1 unit in the category Phone (ids: 3, 4).
82+
-- On Friday (2020-06-05) were sold 10 units in the category Book (ids: 1, 2) and 5 units in Glasses (ids: 5).
83+
-- On Saturday there are no items sold.
84+
-- On Sunday (2020-06-14, 2020-06-21) were sold a total of 10 units (5 +5) in the category Phone (ids: 3, 4).
85+
-- There are no sales of T-Shirt.
86+
87+
-- Solution
88+
with t1 as(
89+
select distinct item_category,
90+
case when dayname(order_date)='Monday' then sum(quantity) over(partition by item_category,dayname(order_date)) else 0 end as Monday,
91+
Case when dayname(order_date)='Tuesday' then sum(quantity) over(partition by item_category,dayname(order_date)) else 0 end as Tuesday,
92+
Case when dayname(order_date)='Wednesday' then sum(quantity) over(partition by item_category,dayname(order_date)) else 0 end as Wednesday,
93+
Case when dayname(order_date)='Thursday' then sum(quantity) over(partition by item_category,dayname(order_date)) else 0 end as Thursday,
94+
Case when dayname(order_date)='Friday' then sum(quantity) over(partition by item_category,dayname(order_date)) else 0 end as Friday,
95+
Case when dayname(order_date)='Saturday' then sum(quantity) over(partition by item_category,dayname(order_date)) else 0 end as Saturday,
96+
Case when dayname(order_date)='Sunday' then sum(quantity) over(partition by item_category,dayname(order_date)) else 0 end as Sunday
97+
from orders o
98+
right join items i
99+
using (item_id))
100+
101+
select item_category as category, sum(Monday) as Monday, sum(Tuesday) as Tuesday, sum(Wednesday) Wednesday, sum(Thursday) Thursday,
102+
sum(Friday) Friday, sum(Saturday) Saturday, sum(Sunday) Sunday
103+
from t1
104+
group by item_category
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
-- Question 114
2+
-- Table: Product
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | product_id | int |
8+
-- | product_name | varchar |
9+
-- +---------------+---------+
10+
-- product_id is the primary key for this table.
11+
-- product_name is the name of the product.
12+
13+
14+
-- Table: Sales
15+
16+
-- +---------------------+---------+
17+
-- | Column Name | Type |
18+
-- +---------------------+---------+
19+
-- | product_id | int |
20+
-- | period_start | varchar |
21+
-- | period_end | date |
22+
-- | average_daily_sales | int |
23+
-- +---------------------+---------+
24+
-- product_id is the primary key for this table.
25+
-- period_start and period_end indicates the start and end date for sales period, both dates are inclusive.
26+
-- The average_daily_sales column holds the average daily sales amount of the items for the period.
27+
28+
-- Write an SQL query to report the Total sales amount of each item for each year, with corresponding product name, product_id, product_name and report_year.
29+
30+
-- Dates of the sales years are between 2018 to 2020. Return the result table ordered by product_id and report_year.
31+
32+
-- The query result format is in the following example:
33+
34+
35+
-- Product table:
36+
-- +------------+--------------+
37+
-- | product_id | product_name |
38+
-- +------------+--------------+
39+
-- | 1 | LC Phone |
40+
-- | 2 | LC T-Shirt |
41+
-- | 3 | LC Keychain |
42+
-- +------------+--------------+
43+
44+
-- Sales table:
45+
-- +------------+--------------+-------------+---------------------+
46+
-- | product_id | period_start | period_end | average_daily_sales |
47+
-- +------------+--------------+-------------+---------------------+
48+
-- | 1 | 2019-01-25 | 2019-02-28 | 100 |
49+
-- | 2 | 2018-12-01 | 2020-01-01 | 10 |
50+
-- | 3 | 2019-12-01 | 2020-01-31 | 1 |
51+
-- +------------+--------------+-------------+---------------------+
52+
53+
-- Result table:
54+
-- +------------+--------------+-------------+--------------+
55+
-- | product_id | product_name | report_year | total_amount |
56+
-- +------------+--------------+-------------+--------------+
57+
-- | 1 | LC Phone | 2019 | 3500 |
58+
-- | 2 | LC T-Shirt | 2018 | 310 |
59+
-- | 2 | LC T-Shirt | 2019 | 3650 |
60+
-- | 2 | LC T-Shirt | 2020 | 10 |
61+
-- | 3 | LC Keychain | 2019 | 31 |
62+
-- | 3 | LC Keychain | 2020 | 31 |
63+
-- +------------+--------------+-------------+--------------+
64+
-- LC Phone was sold for the period of 2019-01-25 to 2019-02-28, and there are 35 days for this period. Total amount 35*100 = 3500.
65+
-- LC T-shirt was sold for the period of 2018-12-01 to 2020-01-01, and there are 31, 365, 1 days for years 2018, 2019 and 2020 respectively.
66+
-- LC Keychain was sold for the period of 2019-12-01 to 2020-01-31, and there are 31, 31 days for years 2019 and 2020 respectively.
67+
68+
-- Solution
69+
SELECT
70+
b.product_id,
71+
a.product_name,
72+
a.yr AS report_year,
73+
CASE
74+
WHEN YEAR(b.period_start)=YEAR(b.period_end) AND a.yr=YEAR(b.period_start) THEN DATEDIFF(b.period_end,b.period_start)+1
75+
WHEN a.yr=YEAR(b.period_start) THEN DATEDIFF(DATE_FORMAT(b.period_start,'%Y-12-31'),b.period_start)+1
76+
WHEN a.yr=YEAR(b.period_end) THEN DAYOFYEAR(b.period_end)
77+
WHEN a.yr>YEAR(b.period_start) AND a.yr<YEAR(b.period_end) THEN 365
78+
ELSE 0
79+
END * average_daily_sales AS total_amount
80+
FROM
81+
(SELECT product_id,product_name,'2018' AS yr FROM Product
82+
UNION
83+
SELECT product_id,product_name,'2019' AS yr FROM Product
84+
UNION
85+
SELECT product_id,product_name,'2020' AS yr FROM Product) a
86+
JOIN
87+
Sales b
88+
ON a.product_id=b.product_id
89+
HAVING total_amount > 0
90+
ORDER BY b.product_id,a.yr

0 commit comments

Comments
 (0)