Skip to content

Commit d42876f

Browse files
authored
Add files via upload
1 parent b76f2ed commit d42876f

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
-- Question 115
2+
-- Write an SQL query to report the distinct titles of the kid-friendly movies streamed in June 2020.
3+
4+
-- Return the result table in any order.
5+
6+
-- The query result format is in the following example.
7+
8+
9+
10+
-- TVProgram table:
11+
-- +--------------------+--------------+-------------+
12+
-- | program_date | content_id | channel |
13+
-- +--------------------+--------------+-------------+
14+
-- | 2020-06-10 08:00 | 1 | LC-Channel |
15+
-- | 2020-05-11 12:00 | 2 | LC-Channel |
16+
-- | 2020-05-12 12:00 | 3 | LC-Channel |
17+
-- | 2020-05-13 14:00 | 4 | Disney Ch |
18+
-- | 2020-06-18 14:00 | 4 | Disney Ch |
19+
-- | 2020-07-15 16:00 | 5 | Disney Ch |
20+
-- +--------------------+--------------+-------------+
21+
22+
-- Content table:
23+
-- +------------+----------------+---------------+---------------+
24+
-- | content_id | title | Kids_content | content_type |
25+
-- +------------+----------------+---------------+---------------+
26+
-- | 1 | Leetcode Movie | N | Movies |
27+
-- | 2 | Alg. for Kids | Y | Series |
28+
-- | 3 | Database Sols | N | Series |
29+
-- | 4 | Aladdin | Y | Movies |
30+
-- | 5 | Cinderella | Y | Movies |
31+
-- +------------+----------------+---------------+---------------+
32+
33+
-- Result table:
34+
-- +--------------+
35+
-- | title |
36+
-- +--------------+
37+
-- | Aladdin |
38+
-- +--------------+
39+
-- "Leetcode Movie" is not a content for kids.
40+
-- "Alg. for Kids" is not a movie.
41+
-- "Database Sols" is not a movie
42+
-- "Alladin" is a movie, content for kids and was streamed in June 2020.
43+
-- "Cinderella" was not streamed in June 2020.
44+
45+
-- Solution
46+
select distinct title
47+
from
48+
(select content_id, title
49+
from content
50+
where kids_content = 'Y' and content_type = 'Movies') a
51+
join
52+
tvprogram using (content_id)
53+
where month(program_date) = 6
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
-- Question 116
2+
-- Table Activities:
3+
4+
-- +-------------+---------+
5+
-- | Column Name | Type |
6+
-- +-------------+---------+
7+
-- | sell_date | date |
8+
-- | product | varchar |
9+
-- +-------------+---------+
10+
-- There is no primary key for this table, it may contains duplicates.
11+
-- Each row of this table contains the product name and the date it was sold in a market.
12+
13+
14+
-- Write an SQL query to find for each date, the number of distinct products sold and their names.
15+
16+
-- The sold-products names for each date should be sorted lexicographically.
17+
18+
-- Return the result table ordered by sell_date.
19+
20+
-- The query result format is in the following example.
21+
22+
-- Activities table:
23+
-- +------------+-------------+
24+
-- | sell_date | product |
25+
-- +------------+-------------+
26+
-- | 2020-05-30 | Headphone |
27+
-- | 2020-06-01 | Pencil |
28+
-- | 2020-06-02 | Mask |
29+
-- | 2020-05-30 | Basketball |
30+
-- | 2020-06-01 | Bible |
31+
-- | 2020-06-02 | Mask |
32+
-- | 2020-05-30 | T-Shirt |
33+
-- +------------+-------------+
34+
35+
-- Result table:
36+
-- +------------+----------+------------------------------+
37+
-- | sell_date | num_sold | products |
38+
-- +------------+----------+------------------------------+
39+
-- | 2020-05-30 | 3 | Basketball,Headphone,T-shirt |
40+
-- | 2020-06-01 | 2 | Bible,Pencil |
41+
-- | 2020-06-02 | 1 | Mask |
42+
-- +------------+----------+------------------------------+
43+
-- For 2020-05-30, Sold items were (Headphone, Basketball, T-shirt), we sort them lexicographically and separate them by comma.
44+
-- For 2020-06-01, Sold items were (Pencil, Bible), we sort them lexicographically and separate them by comma.
45+
-- For 2020-06-02, Sold item is (Mask), we just return it.
46+
47+
-- Solution
48+
select sell_date, count(distinct product) as num_sold, group_concat(distinct product) as products
49+
from activities
50+
group by 1
51+
order by 1

0 commit comments

Comments
 (0)