From 18559ef3700c974d8fec3cce1fcaf1461d6e6385 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Sun, 23 Mar 2025 23:10:23 +0530 Subject: [PATCH 1/5] Create 1341. Movie Rating.sql --- .../1341. Movie Rating.sql | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 LeetCode SQL 50 Solution/1341. Movie Rating.sql diff --git a/LeetCode SQL 50 Solution/1341. Movie Rating.sql b/LeetCode SQL 50 Solution/1341. Movie Rating.sql new file mode 100644 index 0000000..bb083f8 --- /dev/null +++ b/LeetCode SQL 50 Solution/1341. Movie Rating.sql @@ -0,0 +1,98 @@ +1341. Movie Rating +Solved +Medium +Topics +Companies +SQL Schema +Pandas Schema +Table: Movies + ++---------------+---------+ +| Column Name | Type | ++---------------+---------+ +| movie_id | int | +| title | varchar | ++---------------+---------+ +movie_id is the primary key (column with unique values) for this table. +title is the name of the movie. + + +Table: Users + ++---------------+---------+ +| Column Name | Type | ++---------------+---------+ +| user_id | int | +| name | varchar | ++---------------+---------+ +user_id is the primary key (column with unique values) for this table. +The column 'name' has unique values. +Table: MovieRating + ++---------------+---------+ +| Column Name | Type | ++---------------+---------+ +| movie_id | int | +| user_id | int | +| rating | int | +| created_at | date | ++---------------+---------+ +(movie_id, user_id) is the primary key (column with unique values) 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. + + +Write a solution to: + +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. +Find the movie name with the highest average rating in February 2020. In case of a tie, return the lexicographically smaller movie name. +The result format is in the following example. + + + +Example 1: + +Input: +Movies table: ++-------------+--------------+ +| movie_id | title | ++-------------+--------------+ +| 1 | Avengers | +| 2 | Frozen 2 | +| 3 | Joker | ++-------------+--------------+ +Users table: ++-------------+--------------+ +| user_id | name | ++-------------+--------------+ +| 1 | Daniel | +| 2 | Monica | +| 3 | Maria | +| 4 | James | ++-------------+--------------+ +MovieRating table: ++-------------+--------------+--------------+-------------+ +| movie_id | user_id | rating | created_at | ++-------------+--------------+--------------+-------------+ +| 1 | 1 | 3 | 2020-01-12 | +| 1 | 2 | 4 | 2020-02-11 | +| 1 | 3 | 2 | 2020-02-12 | +| 1 | 4 | 1 | 2020-01-01 | +| 2 | 1 | 5 | 2020-02-17 | +| 2 | 2 | 2 | 2020-02-01 | +| 2 | 3 | 2 | 2020-03-01 | +| 3 | 1 | 3 | 2020-02-22 | +| 3 | 2 | 4 | 2020-02-25 | ++-------------+--------------+--------------+-------------+ +Output: ++--------------+ +| results | ++--------------+ +| Daniel | +| Frozen 2 | ++--------------+ +Explanation: +Daniel and Monica have rated 3 movies ("Avengers", "Frozen 2" and "Joker") but Daniel is smaller lexicographically. +Frozen 2 and Joker have a rating average of 3.5 in February but Frozen 2 is smaller lexicographically. + + From b02ef66c0d67d270149f8ed827b127e3df30f0c5 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Sun, 23 Mar 2025 23:11:06 +0530 Subject: [PATCH 2/5] Update 1341. Movie Rating.sql --- .../1341. Movie Rating.sql | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/LeetCode SQL 50 Solution/1341. Movie Rating.sql b/LeetCode SQL 50 Solution/1341. Movie Rating.sql index bb083f8..d5af570 100644 --- a/LeetCode SQL 50 Solution/1341. Movie Rating.sql +++ b/LeetCode SQL 50 Solution/1341. Movie Rating.sql @@ -1,10 +1,5 @@ 1341. Movie Rating -Solved -Medium -Topics -Companies -SQL Schema -Pandas Schema +""" Table: Movies +---------------+---------+ @@ -95,4 +90,26 @@ Explanation: Daniel and Monica have rated 3 movies ("Avengers", "Frozen 2" and "Joker") but Daniel is smaller lexicographically. Frozen 2 and Joker have a rating average of 3.5 in February but Frozen 2 is smaller lexicographically. +""" +# Write your MySQL query statement below +( + SELECT name AS results + FROM + Users + JOIN MovieRating USING (user_id) + GROUP BY user_id + ORDER BY COUNT(1) DESC, name + LIMIT 1 +) +UNION ALL +( + SELECT title + FROM + MovieRating + JOIN Movies USING (movie_id) + WHERE DATE_FORMAT(created_at, '%Y-%m') = '2020-02' + GROUP BY movie_id + ORDER BY AVG(rating) DESC, title + LIMIT 1 +); \ No newline at end of file From c06955e21caeda8b9095117b446ec3706d433b5d Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Sun, 23 Mar 2025 23:11:53 +0530 Subject: [PATCH 3/5] Create 1321. Restaurant Growth.sql --- LeetCode SQL 50 Solution/1321. Restaurant Growth.sql | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 LeetCode SQL 50 Solution/1321. Restaurant Growth.sql diff --git a/LeetCode SQL 50 Solution/1321. Restaurant Growth.sql b/LeetCode SQL 50 Solution/1321. Restaurant Growth.sql new file mode 100644 index 0000000..e69de29 From 510ad9708262ce0634a9f4658ad96cb5d5b6af70 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Sun, 23 Mar 2025 23:12:13 +0530 Subject: [PATCH 4/5] Update 1321. Restaurant Growth.sql --- .../1321. Restaurant Growth.sql | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/LeetCode SQL 50 Solution/1321. Restaurant Growth.sql b/LeetCode SQL 50 Solution/1321. Restaurant Growth.sql index e69de29..6c7127c 100644 --- a/LeetCode SQL 50 Solution/1321. Restaurant Growth.sql +++ b/LeetCode SQL 50 Solution/1321. Restaurant Growth.sql @@ -0,0 +1,68 @@ +321. Restaurant Growth +Solved +Medium +Topics +Companies +SQL Schema +Pandas Schema +Table: Customer + ++---------------+---------+ +| Column Name | Type | ++---------------+---------+ +| customer_id | int | +| name | varchar | +| visited_on | date | +| amount | int | ++---------------+---------+ +In SQL,(customer_id, visited_on) is the primary key for this table. +This table contains data about customer transactions in a restaurant. +visited_on is the date on which the customer with ID (customer_id) has visited the restaurant. +amount is the total paid by a customer. + + +You are the restaurant owner and you want to analyze a possible expansion (there will be at least one customer every day). + +Compute the moving average of how much the customer paid in a seven days window (i.e., current day + 6 days before). average_amount should be rounded to two decimal places. + +Return the result table ordered by visited_on in ascending order. + +The result format is in the following example. + + + +Example 1: + +Input: +Customer table: ++-------------+--------------+--------------+-------------+ +| customer_id | name | visited_on | amount | ++-------------+--------------+--------------+-------------+ +| 1 | Jhon | 2019-01-01 | 100 | +| 2 | Daniel | 2019-01-02 | 110 | +| 3 | Jade | 2019-01-03 | 120 | +| 4 | Khaled | 2019-01-04 | 130 | +| 5 | Winston | 2019-01-05 | 110 | +| 6 | Elvis | 2019-01-06 | 140 | +| 7 | Anna | 2019-01-07 | 150 | +| 8 | Maria | 2019-01-08 | 80 | +| 9 | Jaze | 2019-01-09 | 110 | +| 1 | Jhon | 2019-01-10 | 130 | +| 3 | Jade | 2019-01-10 | 150 | ++-------------+--------------+--------------+-------------+ +Output: ++--------------+--------------+----------------+ +| visited_on | amount | average_amount | ++--------------+--------------+----------------+ +| 2019-01-07 | 860 | 122.86 | +| 2019-01-08 | 840 | 120 | +| 2019-01-09 | 840 | 120 | +| 2019-01-10 | 1000 | 142.86 | ++--------------+--------------+----------------+ +Explanation: +1st moving average from 2019-01-01 to 2019-01-07 has an average_amount of (100 + 110 + 120 + 130 + 110 + 140 + 150)/7 = 122.86 +2nd moving average from 2019-01-02 to 2019-01-08 has an average_amount of (110 + 120 + 130 + 110 + 140 + 150 + 80)/7 = 120 +3rd moving average from 2019-01-03 to 2019-01-09 has an average_amount of (120 + 130 + 110 + 140 + 150 + 80 + 110)/7 = 120 +4th moving average from 2019-01-04 to 2019-01-10 has an average_amount of (130 + 110 + 140 + 150 + 80 + 110 + 130 + 150)/7 = 142.86 + + From 748fc9ec9de422b69388e30284fd3974467a40c4 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Sun, 23 Mar 2025 23:13:00 +0530 Subject: [PATCH 5/5] Update 1321. Restaurant Growth.sql --- .../1321. Restaurant Growth.sql | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/LeetCode SQL 50 Solution/1321. Restaurant Growth.sql b/LeetCode SQL 50 Solution/1321. Restaurant Growth.sql index 6c7127c..384399e 100644 --- a/LeetCode SQL 50 Solution/1321. Restaurant Growth.sql +++ b/LeetCode SQL 50 Solution/1321. Restaurant Growth.sql @@ -66,3 +66,42 @@ Explanation: 4th moving average from 2019-01-04 to 2019-01-10 has an average_amount of (130 + 110 + 140 + 150 + 80 + 110 + 130 + 150)/7 = 142.86 +Solution 1: + +# Write your MySQL query statement below +WITH + t AS ( + SELECT + visited_on, + SUM(amount) OVER ( + ORDER BY visited_on + ROWS 6 PRECEDING + ) AS amount, + RANK() OVER ( + ORDER BY visited_on + ROWS 6 PRECEDING + ) AS rk + FROM + ( + SELECT visited_on, SUM(amount) AS amount + FROM Customer + GROUP BY visited_on + ) AS tt + ) +SELECT visited_on, amount, ROUND(amount / 7, 2) AS average_amount +FROM t +WHERE rk > 6; + + +Solution 2: +# Write your MySQL query statement below +SELECT + a.visited_on, + SUM(b.amount) AS amount, + ROUND(SUM(b.amount) / 7, 2) AS average_amount +FROM + (SELECT DISTINCT visited_on FROM customer) AS a + JOIN customer AS b ON DATEDIFF(a.visited_on, b.visited_on) BETWEEN 0 AND 6 +WHERE a.visited_on >= (SELECT MIN(visited_on) FROM customer) + 6 +GROUP BY 1 +ORDER BY 1; \ No newline at end of file