Skip to content

Commit a202231

Browse files
ThanhNITjavadev
authored andcommitted
Added tasks 1327, 1341, 1378, 1517, 1633
1 parent dc99c5d commit a202231

File tree

15 files changed

+849
-0
lines changed

15 files changed

+849
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
1327\. List the Products Ordered in a Period
2+
3+
Easy
4+
5+
SQL Schema
6+
7+
Table: `Products`
8+
9+
+------------------+---------+
10+
| Column Name | Type |
11+
+------------------+---------+
12+
| product_id | int |
13+
| product_name | varchar |
14+
| product_category | varchar |
15+
+------------------+---------+
16+
17+
product_id is the primary key for this table. This table contains data about the company's products.
18+
19+
Table: `Orders`
20+
21+
+---------------+---------+
22+
| Column Name | Type |
23+
+---------------+---------+
24+
| product_id | int |
25+
| order_date | date |
26+
| unit | int |
27+
+---------------+---------+
28+
29+
There is no primary key for this table. It may have duplicate rows. product_id is a foreign key to the Products table. unit is the number of products ordered in order_date.
30+
31+
Write an SQL query to get the names of products that have at least `100` units ordered in **February 2020** and their amount.
32+
33+
Return result table in **any order**.
34+
35+
The query result format is in the following example.
36+
37+
**Example 1:**
38+
39+
**Input:** Products table:
40+
41+
+-------------+-----------------------+------------------+
42+
| product_id | product_name | product_category |
43+
+-------------+-----------------------+------------------+
44+
| 1 | Leetcode Solutions | Book |
45+
| 2 | Jewels of Stringology | Book |
46+
| 3 | HP | Laptop |
47+
| 4 | Lenovo | Laptop |
48+
| 5 | Leetcode Kit | T-shirt |
49+
+-------------+-----------------------+------------------+
50+
51+
Orders table:
52+
53+
+--------------+--------------+----------+
54+
| product_id | order_date | unit |
55+
+--------------+--------------+----------+
56+
| 1 | 2020-02-05 | 60 |
57+
| 1 | 2020-02-10 | 70 |
58+
| 2 | 2020-01-18 | 30 |
59+
| 2 | 2020-02-11 | 80 |
60+
| 3 | 2020-02-17 | 2 |
61+
| 3 | 2020-02-24 | 3 |
62+
| 4 | 2020-03-01 | 20 |
63+
| 4 | 2020-03-04 | 30 |
64+
| 4 | 2020-03-04 | 60 |
65+
| 5 | 2020-02-25 | 50 |
66+
| 5 | 2020-02-27 | 50 |
67+
| 5 | 2020-03-01 | 50 |
68+
+--------------+--------------+----------+
69+
70+
**Output:**
71+
72+
+--------------------+---------+
73+
| product_name | unit |
74+
+--------------------+---------+
75+
| Leetcode Solutions | 130 |
76+
| Leetcode Kit | 100 |
77+
+--------------------+---------+
78+
79+
**Explanation:**
80+
81+
Products with product_id = 1 is ordered in February a total of (60 + 70) = 130.
82+
83+
Products with product_id = 2 is ordered in February a total of 80. Products with product_id = 3 is ordered in February a total of (2 + 3) = 5.
84+
85+
Products with product_id = 4 was not ordered in February 2020. Products with product_id = 5 is ordered in February a total of (50 + 50) = 100.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Write your MySQL query statement below
2+
# #Easy #Database #2023_08_16_Time_1374_ms_(80.18%)_Space_0B_(100.00%)
3+
SELECT * FROM (
4+
SELECT
5+
a.product_name,
6+
SUM(b.unit) as unit
7+
FROM Products a
8+
LEFT JOIN Orders b
9+
ON a.product_id = b.product_id
10+
WHERE b.order_date BETWEEN '2020-02-01' AND '2020-02-29'
11+
GROUP BY a.product_name
12+
) AS d
13+
GROUP BY d.product_name
14+
HAVING d.unit >= 100
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
1341\. Movie Rating
2+
3+
Medium
4+
5+
SQL Schema
6+
7+
Table: `Movies`
8+
9+
+---------------+---------+
10+
| Column Name | Type |
11+
+---------------+---------+
12+
| movie_id | int |
13+
| title | varchar |
14+
+---------------+---------+
15+
16+
movie_id is the primary key for this table. title is the name of the movie.
17+
18+
Table: `Users`
19+
20+
+---------------+---------+
21+
| Column Name | Type |
22+
+---------------+---------+
23+
| user_id | int |
24+
| name | varchar |
25+
+---------------+---------+
26+
27+
user_id is the primary key for this table.
28+
29+
Table: `MovieRating`
30+
31+
+---------------+---------+
32+
| Column Name | Type |
33+
+---------------+---------+
34+
| movie_id | int |
35+
| user_id | int |
36+
| rating | int |
37+
| created_at | date |
38+
+---------------+---------+
39+
40+
(movie_id, user_id) is the primary key for this table. This table contains the rating of a movie by a user in their review. created_at is the user's review date.
41+
42+
Write an SQL query to:
43+
44+
* Find the name of the user who has rated the greatest number of movies. In case of a tie, return the lexicographically smaller user name.
45+
* Find the movie name with the **highest average** rating in `February 2020`. In case of a tie, return the lexicographically smaller movie name.
46+
47+
The query result format is in the following example.
48+
49+
**Example 1:**
50+
51+
**Input:** Movies table:
52+
53+
+-------------+--------------+
54+
| movie_id | title |
55+
+-------------+--------------+
56+
| 1 | Avengers |
57+
| 2 | Frozen 2 |
58+
| 3 | Joker |
59+
+-------------+--------------+
60+
61+
Users table:
62+
63+
+-------------+--------------+
64+
| user_id | name |
65+
+-------------+--------------+
66+
| 1 | Daniel |
67+
| 2 | Monica |
68+
| 3 | Maria |
69+
| 4 | James |
70+
+-------------+--------------+
71+
72+
MovieRating table:
73+
74+
+-------------+--------------+--------------+-------------+
75+
| movie_id | user_id | rating | created_at |
76+
+-------------+--------------+--------------+-------------+
77+
| 1 | 1 | 3 | 2020-01-12 |
78+
| 1 | 2 | 4 | 2020-02-11 |
79+
| 1 | 3 | 2 | 2020-02-12 |
80+
| 1 | 4 | 1 | 2020-01-01 |
81+
| 2 | 1 | 5 | 2020-02-17 |
82+
| 2 | 2 | 2 | 2020-02-01 |
83+
| 2 | 3 | 2 | 2020-03-01 |
84+
| 3 | 1 | 3 | 2020-02-22 |
85+
| 3 | 2 | 4 | 2020-02-25 |
86+
+-------------+--------------+--------------+-------------+
87+
88+
**Output:**
89+
90+
+--------------+
91+
| results |
92+
+--------------+
93+
| Daniel |
94+
| Frozen 2 |
95+
+--------------+
96+
97+
**Explanation:**
98+
99+
Daniel and Monica have rated 3 movies ("Avengers", "Frozen 2" and "Joker") but Daniel is smaller lexicographically.
100+
101+
Frozen 2 and Joker have a rating average of 3.5 in February but Frozen 2 is smaller lexicographically.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Write your MySQL query statement below
2+
# #Medium #Database #2023_08_16_Time_2515_ms_(78.94%)_Space_0B_(100.00%)
3+
(SELECT name results
4+
FROM Users as U, MovieRating as MR
5+
WHERE U.user_id = MR.user_id
6+
GROUP BY U.user_id
7+
ORDER BY COUNT(MR.user_id) DESC, name ASC LIMIT 1)
8+
UNION ALL
9+
(SELECT title results
10+
FROM Movies as M, MovieRating as MR
11+
WHERE M.movie_id = MR.movie_id AND created_at BETWEEN '2020-02-01' AND '2020-02-29'
12+
GROUP BY M.movie_id
13+
ORDER BY AVG(rating) DESC, title ASC LIMIT 1)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
1378\. Replace Employee ID With The Unique Identifier
2+
3+
Easy
4+
5+
SQL Schema
6+
7+
Table: `Employees`
8+
9+
+---------------+---------+
10+
| Column Name | Type |
11+
+---------------+---------+
12+
| id | int |
13+
| name | varchar |
14+
+---------------+---------+
15+
id is the primary key for this table.
16+
Each row of this table contains the id and the name of an employee in a company.
17+
18+
Table: `EmployeeUNI`
19+
20+
+---------------+---------+
21+
| Column Name | Type |
22+
+---------------+---------+
23+
| id | int |
24+
| unique_id | int |
25+
+---------------+---------+
26+
(id, unique_id) is the primary key for this table.
27+
Each row of this table contains the id and the corresponding unique id of an employee in the company.
28+
29+
Write an SQL query to show the **unique ID** of each user, If a user does not have a unique ID replace just show `null`.
30+
31+
Return the result table in **any** order.
32+
33+
The query result format is in the following example.
34+
35+
**Example 1:**
36+
37+
**Input:**,
38+
39+
Employees table:
40+
+----+----------+
41+
| id | name |
42+
+----+----------+
43+
| 1 | Alice |
44+
| 7 | Bob |
45+
| 11 | Meir |
46+
| 90 | Winston |
47+
| 3 | Jonathan |
48+
+----+----------+
49+
50+
EmployeeUNI table:
51+
+----+-----------+
52+
| id | unique_id |
53+
+----+-----------+
54+
| 3 | 1 |
55+
| 11 | 2 |
56+
| 90 | 3 |
57+
+----+-----------+
58+
59+
**Output:**
60+
61+
+-----------+----------+
62+
| unique_id | name |
63+
+-----------+----------+
64+
| null | Alice |
65+
| null | Bob |
66+
| 2 | Meir |
67+
| 3 | Winston |
68+
| 1 | Jonathan |
69+
+-----------+----------+
70+
71+
**Explanation:**
72+
73+
Alice and Bob do not have a unique ID, We will show null instead.
74+
75+
The unique ID of Meir is 2.
76+
77+
The unique ID of Winston is 3.
78+
79+
The unique ID of Jonathan is 1.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Write your MySQL query statement below
2+
# #Easy #Database #2023_08_16_Time_2498_ms_(63.60%)_Space_0B_(100.00%)
3+
select u.unique_id, e.name
4+
from Employees e
5+
left join EmployeeUNI u
6+
on e.id = u.id;
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
1517\. Find Users With Valid E-Mails
2+
3+
Easy
4+
5+
SQL Schema
6+
7+
Table: `Users`
8+
9+
+---------------+---------+
10+
| Column Name | Type |
11+
+---------------+---------+
12+
| user_id | int |
13+
| name | varchar |
14+
| mail | varchar |
15+
+---------------+---------+
16+
user_id is the primary key for this table.
17+
This table contains information of the users signed up in a website. Some e-mails are invalid.
18+
19+
Write an SQL query to find the users who have **valid emails**.
20+
21+
A valid e-mail has a prefix name and a domain where:
22+
23+
* **The prefix name** is a string that may contain letters (upper or lower case), digits, underscore `'_'`, period `'.'`, and/or dash `'-'`. The prefix name **must** start with a letter.
24+
* **The domain** is `'@leetcode.com'`.
25+
26+
Return the result table in **any order**.
27+
28+
The query result format is in the following example.
29+
30+
**Example 1:**
31+
32+
**Input:**
33+
34+
Users table:
35+
+---------+-----------+-------------------------+
36+
| user_id | name | mail |
37+
+---------+-----------+-------------------------+
38+
| 1 | Winston | winston@leetcode.com |
39+
| 2 | Jonathan | jonathanisgreat |
40+
| 3 | Annabelle | bella-@leetcode.com |
41+
| 4 | Sally | sally.come@leetcode.com |
42+
| 5 | Marwan | quarz#2020@leetcode.com |
43+
| 6 | David | david69@gmail.com |
44+
| 7 | Shapiro | .shapo@leetcode.com |
45+
+---------+-----------+-------------------------+
46+
47+
**Output:**
48+
49+
+---------+-----------+-------------------------+
50+
| user_id | name | mail |
51+
+---------+-----------+-------------------------+
52+
| 1 | Winston | winston@leetcode.com |
53+
| 3 | Annabelle | bella-@leetcode.com |
54+
| 4 | Sally | sally.come@leetcode.com |
55+
+---------+-----------+-------------------------+
56+
57+
**Explanation:**
58+
59+
The mail of user 2 does not have a domain.
60+
61+
The mail of user 5 has the # sign which is not allowed.
62+
63+
The mail of user 6 does not have the leetcode domain.
64+
65+
The mail of user 7 starts with a period.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Write your MySQL query statement below
2+
# #Easy #Database #2023_08_16_Time_1356_ms_(75.95%)_Space_0B_(100.00%)
3+
SELECT *
4+
FROM Users
5+
WHERE mail REGEXP '^[a-zA-Z][a-zA-Z0-9_.-]*@leetcode[.]com'

0 commit comments

Comments
 (0)