-
Notifications
You must be signed in to change notification settings - Fork 469
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b76f2ed
commit d42876f
Showing
2 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
-- Question 115 | ||
-- Write an SQL query to report the distinct titles of the kid-friendly movies streamed in June 2020. | ||
|
||
-- Return the result table in any order. | ||
|
||
-- The query result format is in the following example. | ||
|
||
|
||
|
||
-- TVProgram table: | ||
-- +--------------------+--------------+-------------+ | ||
-- | program_date | content_id | channel | | ||
-- +--------------------+--------------+-------------+ | ||
-- | 2020-06-10 08:00 | 1 | LC-Channel | | ||
-- | 2020-05-11 12:00 | 2 | LC-Channel | | ||
-- | 2020-05-12 12:00 | 3 | LC-Channel | | ||
-- | 2020-05-13 14:00 | 4 | Disney Ch | | ||
-- | 2020-06-18 14:00 | 4 | Disney Ch | | ||
-- | 2020-07-15 16:00 | 5 | Disney Ch | | ||
-- +--------------------+--------------+-------------+ | ||
|
||
-- Content table: | ||
-- +------------+----------------+---------------+---------------+ | ||
-- | content_id | title | Kids_content | content_type | | ||
-- +------------+----------------+---------------+---------------+ | ||
-- | 1 | Leetcode Movie | N | Movies | | ||
-- | 2 | Alg. for Kids | Y | Series | | ||
-- | 3 | Database Sols | N | Series | | ||
-- | 4 | Aladdin | Y | Movies | | ||
-- | 5 | Cinderella | Y | Movies | | ||
-- +------------+----------------+---------------+---------------+ | ||
|
||
-- Result table: | ||
-- +--------------+ | ||
-- | title | | ||
-- +--------------+ | ||
-- | Aladdin | | ||
-- +--------------+ | ||
-- "Leetcode Movie" is not a content for kids. | ||
-- "Alg. for Kids" is not a movie. | ||
-- "Database Sols" is not a movie | ||
-- "Alladin" is a movie, content for kids and was streamed in June 2020. | ||
-- "Cinderella" was not streamed in June 2020. | ||
|
||
-- Solution | ||
select distinct title | ||
from | ||
(select content_id, title | ||
from content | ||
where kids_content = 'Y' and content_type = 'Movies') a | ||
join | ||
tvprogram using (content_id) | ||
where month(program_date) = 6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
-- Question 116 | ||
-- Table Activities: | ||
|
||
-- +-------------+---------+ | ||
-- | Column Name | Type | | ||
-- +-------------+---------+ | ||
-- | sell_date | date | | ||
-- | product | varchar | | ||
-- +-------------+---------+ | ||
-- There is no primary key for this table, it may contains duplicates. | ||
-- Each row of this table contains the product name and the date it was sold in a market. | ||
|
||
|
||
-- Write an SQL query to find for each date, the number of distinct products sold and their names. | ||
|
||
-- The sold-products names for each date should be sorted lexicographically. | ||
|
||
-- Return the result table ordered by sell_date. | ||
|
||
-- The query result format is in the following example. | ||
|
||
-- Activities table: | ||
-- +------------+-------------+ | ||
-- | sell_date | product | | ||
-- +------------+-------------+ | ||
-- | 2020-05-30 | Headphone | | ||
-- | 2020-06-01 | Pencil | | ||
-- | 2020-06-02 | Mask | | ||
-- | 2020-05-30 | Basketball | | ||
-- | 2020-06-01 | Bible | | ||
-- | 2020-06-02 | Mask | | ||
-- | 2020-05-30 | T-Shirt | | ||
-- +------------+-------------+ | ||
|
||
-- Result table: | ||
-- +------------+----------+------------------------------+ | ||
-- | sell_date | num_sold | products | | ||
-- +------------+----------+------------------------------+ | ||
-- | 2020-05-30 | 3 | Basketball,Headphone,T-shirt | | ||
-- | 2020-06-01 | 2 | Bible,Pencil | | ||
-- | 2020-06-02 | 1 | Mask | | ||
-- +------------+----------+------------------------------+ | ||
-- For 2020-05-30, Sold items were (Headphone, Basketball, T-shirt), we sort them lexicographically and separate them by comma. | ||
-- For 2020-06-01, Sold items were (Pencil, Bible), we sort them lexicographically and separate them by comma. | ||
-- For 2020-06-02, Sold item is (Mask), we just return it. | ||
|
||
-- Solution | ||
select sell_date, count(distinct product) as num_sold, group_concat(distinct product) as products | ||
from activities | ||
group by 1 | ||
order by 1 |