diff --git a/ADVANCED SELECT AND JOINS/1204. Last Person to Fit in the Bus.sql b/ADVANCED SELECT AND JOINS/1204. Last Person to Fit in the Bus.sql new file mode 100644 index 0000000..e69de29 diff --git a/ADVANCED SELECT AND JOINS/1731. The Number of Employees Which Report to Each Employee.sql b/ADVANCED SELECT AND JOINS/1731. The Number of Employees Which Report to Each Employee.sql index c8c10c9..6e4630e 100644 --- a/ADVANCED SELECT AND JOINS/1731. The Number of Employees Which Report to Each Employee.sql +++ b/ADVANCED SELECT AND JOINS/1731. The Number of Employees Which Report to Each Employee.sql @@ -1,13 +1,13 @@ -# Write your MySQL query statement below -SELECT m.employee_id, - m.name, - count(e.employee_id) AS reports_count, - round(avg(e.age)) AS average_age -FROM - employees e JOIN employees m -ON - e.reports_to = m.employee_id -GROUP BY - employee_id -ORDER BY +# Write your MySQL query statement below +SELECT m.employee_id, + m.name, + count(e.employee_id) AS reports_count, + round(avg(e.age)) AS average_age +FROM + employees e JOIN employees m +ON + e.reports_to = m.employee_id +GROUP BY + employee_id +ORDER BY employee_id; \ No newline at end of file diff --git a/ADVANCED SELECT AND JOINS/1789. Primary Department for Each Employee.sql b/ADVANCED SELECT AND JOINS/1789. Primary Department for Each Employee.sql index 0ffc107..a9f6673 100644 --- a/ADVANCED SELECT AND JOINS/1789. Primary Department for Each Employee.sql +++ b/ADVANCED SELECT AND JOINS/1789. Primary Department for Each Employee.sql @@ -1,11 +1,11 @@ -# Write your MySQL query statement below -SELECT employee_id, department_id -FROM Employee -WHERE primary_flag = 'Y' - OR - employee_id IN ( - SELECT employee_id - FROM Employee - GROUP BY employee_id - HAVING COUNT(employee_id) = 1 +# Write your MySQL query statement below +SELECT employee_id, department_id +FROM Employee +WHERE primary_flag = 'Y' + OR + employee_id IN ( + SELECT employee_id + FROM Employee + GROUP BY employee_id + HAVING COUNT(employee_id) = 1 ); \ No newline at end of file diff --git a/ADVANCED SELECT AND JOINS/180. Consecutive Numbers.sql b/ADVANCED SELECT AND JOINS/180. Consecutive Numbers.sql index 3ba7866..49ccce5 100644 --- a/ADVANCED SELECT AND JOINS/180. Consecutive Numbers.sql +++ b/ADVANCED SELECT AND JOINS/180. Consecutive Numbers.sql @@ -1,6 +1,6 @@ --- Write your MySQL/PostgreSQL query statement below -SELECT DISTINCT l1.num as ConsecutiveNums -FROM Logs l1 -JOIN Logs l2 ON l1.num = l2.num and l1.id = l2.id + 1 -JOIN Logs l3 ON l2.num = l3.num and l2.id = l3.id + 1; - +-- Write your MySQL/PostgreSQL query statement below +SELECT DISTINCT l1.num as ConsecutiveNums +FROM Logs l1 +JOIN Logs l2 ON l1.num = l2.num and l1.id = l2.id + 1 +JOIN Logs l3 ON l2.num = l3.num and l2.id = l3.id + 1; + diff --git a/ADVANCED SELECT AND JOINS/1907. Count Salary Categories.sql b/ADVANCED SELECT AND JOINS/1907. Count Salary Categories.sql index 945b33e..8102411 100644 --- a/ADVANCED SELECT AND JOINS/1907. Count Salary Categories.sql +++ b/ADVANCED SELECT AND JOINS/1907. Count Salary Categories.sql @@ -1,15 +1,15 @@ -SELECT "Low Salary" AS category, - sum(income < 20000) AS accounts_count - FROM Accounts - -UNION - -SELECT "Average Salary" AS category, - sum(income BETWEEN 20000 AND 50000) AS accounts_count - FROM Accounts - -UNION - -SELECT "High Salary" AS category, - sum(income > 50000) AS accounts_count +SELECT "Low Salary" AS category, + sum(income < 20000) AS accounts_count + FROM Accounts + +UNION + +SELECT "Average Salary" AS category, + sum(income BETWEEN 20000 AND 50000) AS accounts_count + FROM Accounts + +UNION + +SELECT "High Salary" AS category, + sum(income > 50000) AS accounts_count FROM Accounts; \ No newline at end of file diff --git a/ADVANCED SELECT AND JOINS/610. Triangle Judgement.sql b/ADVANCED SELECT AND JOINS/610. Triangle Judgement.sql index f667669..68e84c1 100644 --- a/ADVANCED SELECT AND JOINS/610. Triangle Judgement.sql +++ b/ADVANCED SELECT AND JOINS/610. Triangle Judgement.sql @@ -1,10 +1,10 @@ -# Write your MySQL query statement below --- Using the triangle inequality: - --- x + y > z --- x + z > y --- y + z > x - -SELECT x, y, z, CASE WHEN (x+y) > z AND (x+z) > y AND (y+z) > x THEN -'Yes' ELSE 'No' END AS triangle -FROM Triangle; +# Write your MySQL query statement below +-- Using the triangle inequality: + +-- x + y > z +-- x + z > y +-- y + z > x + +SELECT x, y, z, CASE WHEN (x+y) > z AND (x+z) > y AND (y+z) > x THEN +'Yes' ELSE 'No' END AS triangle +FROM Triangle; diff --git a/ADVANCED STRING FUNCTIONS + REGEX + CLAUSE/1517. Find Users With Valid E-Mails.sql b/ADVANCED STRING FUNCTIONS + REGEX + CLAUSE/1517. Find Users With Valid E-Mails.sql index 89cc677..78a821f 100644 --- a/ADVANCED STRING FUNCTIONS + REGEX + CLAUSE/1517. Find Users With Valid E-Mails.sql +++ b/ADVANCED STRING FUNCTIONS + REGEX + CLAUSE/1517. Find Users With Valid E-Mails.sql @@ -1,3 +1,3 @@ -# Write your MySQL query statement below -SELECT * FROM users -WHERE mail REGEXP '^[a-zA-Z][a-zA-Z0-9_.-]*@leetcode[.]com$'; +# Write your MySQL query statement below +SELECT * FROM users +WHERE mail REGEXP '^[a-zA-Z][a-zA-Z0-9_.-]*@leetcode[.]com$'; diff --git a/ADVANCED STRING FUNCTIONS + REGEX + CLAUSE/1667. Fix Names in a Table.sql b/ADVANCED STRING FUNCTIONS + REGEX + CLAUSE/1667. Fix Names in a Table.sql index 5ba47f2..5dd5ead 100644 --- a/ADVANCED STRING FUNCTIONS + REGEX + CLAUSE/1667. Fix Names in a Table.sql +++ b/ADVANCED STRING FUNCTIONS + REGEX + CLAUSE/1667. Fix Names in a Table.sql @@ -1,4 +1,4 @@ -# Write your MySQL query statement below -SELECT user_id, CONCAT(UPPER(LEFT(name, 1)), LOWER(RIGHT(name, LENGTH(name)-1))) AS name -FROM Users +# Write your MySQL query statement below +SELECT user_id, CONCAT(UPPER(LEFT(name, 1)), LOWER(RIGHT(name, LENGTH(name)-1))) AS name +FROM Users ORDER BY user_id; \ No newline at end of file diff --git a/BASIC AGGREGATION FUNCTION/1075. Project Employees I.sql b/BASIC AGGREGATION FUNCTION/1075. Project Employees I.sql index b7856ac..fd4f694 100644 --- a/BASIC AGGREGATION FUNCTION/1075. Project Employees I.sql +++ b/BASIC AGGREGATION FUNCTION/1075. Project Employees I.sql @@ -1,5 +1,5 @@ --- Write your PostgreSQL query statement below -SELECT p.project_id, ROUND(AVG(e.experience_years), 2) AS average_years -FROM Project p -LEFT JOIN Employee e ON p.employee_id = e.employee_id -GROUP BY p.project_id; +-- Write your PostgreSQL query statement below +SELECT p.project_id, ROUND(AVG(e.experience_years), 2) AS average_years +FROM Project p +LEFT JOIN Employee e ON p.employee_id = e.employee_id +GROUP BY p.project_id; diff --git a/BASIC AGGREGATION FUNCTION/1251. Average Selling Price.sql b/BASIC AGGREGATION FUNCTION/1251. Average Selling Price.sql index 520ebc1..7c2bdc2 100644 --- a/BASIC AGGREGATION FUNCTION/1251. Average Selling Price.sql +++ b/BASIC AGGREGATION FUNCTION/1251. Average Selling Price.sql @@ -1,6 +1,6 @@ -# Write your MySQL query statement below -SELECT p.product_id, IFNULL(ROUND(SUM(units*price)/SUM(units),2),0) AS average_price -FROM Prices p LEFT JOIN UnitsSold u -ON p.product_id = u.product_id AND -u.purchase_date BETWEEN start_date AND end_date +# Write your MySQL query statement below +SELECT p.product_id, IFNULL(ROUND(SUM(units*price)/SUM(units),2),0) AS average_price +FROM Prices p LEFT JOIN UnitsSold u +ON p.product_id = u.product_id AND +u.purchase_date BETWEEN start_date AND end_date group by product_id \ No newline at end of file diff --git a/BASIC AGGREGATION FUNCTION/1633. Percentage of Users Attended a Contest.sql b/BASIC AGGREGATION FUNCTION/1633. Percentage of Users Attended a Contest.sql index a7e9500..5c260b0 100644 --- a/BASIC AGGREGATION FUNCTION/1633. Percentage of Users Attended a Contest.sql +++ b/BASIC AGGREGATION FUNCTION/1633. Percentage of Users Attended a Contest.sql @@ -1,7 +1,7 @@ -# Write your MySQL query statement below -SELECT contest_id, ROUND((COUNT(DISTINCT user_id)*100)/( - SELECT COUNT(user_id) FROM Users -), 2) AS percentage -FROM Register -GROUP BY contest_id +# Write your MySQL query statement below +SELECT contest_id, ROUND((COUNT(DISTINCT user_id)*100)/( + SELECT COUNT(user_id) FROM Users +), 2) AS percentage +FROM Register +GROUP BY contest_id ORDER BY percentage DESC, contest_id; \ No newline at end of file diff --git a/BASIC AGGREGATION FUNCTION/620. Not Boring Movies.sql b/BASIC AGGREGATION FUNCTION/620. Not Boring Movies.sql index d74782f..47b5906 100644 --- a/BASIC AGGREGATION FUNCTION/620. Not Boring Movies.sql +++ b/BASIC AGGREGATION FUNCTION/620. Not Boring Movies.sql @@ -1,5 +1,5 @@ -# Write your MySQL query statement below -SELECT id, movie, description, rating -FROM Cinema -WHERE MOD(id, 2) <> 0 AND description <> "boring" +# Write your MySQL query statement below +SELECT id, movie, description, rating +FROM Cinema +WHERE MOD(id, 2) <> 0 AND description <> "boring" ORDER BY rating DESC; \ No newline at end of file diff --git a/BASIC JOIN/1068. Product Sales Analysis I.sql b/BASIC JOIN/1068. Product Sales Analysis I.sql index 40c9ec1..847f25f 100644 --- a/BASIC JOIN/1068. Product Sales Analysis I.sql +++ b/BASIC JOIN/1068. Product Sales Analysis I.sql @@ -1,3 +1,3 @@ -# Write your MySQL query statement below -SELECT p.product_name, s.year, s.price +# Write your MySQL query statement below +SELECT p.product_name, s.year, s.price FROM Sales s LEFT JOIN Product p ON s.product_id = p.product_id; \ No newline at end of file diff --git a/BASIC JOIN/1378. Replace Employee ID With The Unique Identifier.sql b/BASIC JOIN/1378. Replace Employee ID With The Unique Identifier.sql index 3bdf113..a1619de 100644 --- a/BASIC JOIN/1378. Replace Employee ID With The Unique Identifier.sql +++ b/BASIC JOIN/1378. Replace Employee ID With The Unique Identifier.sql @@ -1,3 +1,3 @@ -# Write your MySQL query statement below -SELECT eu.unique_id AS unique_id, e.name AS name +# Write your MySQL query statement below +SELECT eu.unique_id AS unique_id, e.name AS name FROM Employees e LEFT JOIN EmployeeUNI eu ON e.id = eu.id; \ No newline at end of file diff --git a/BASIC JOIN/1581. Customer Who Visited but Did Not Make Any Transactions.sql b/BASIC JOIN/1581. Customer Who Visited but Did Not Make Any Transactions.sql index a8f0d31..8bbe792 100644 --- a/BASIC JOIN/1581. Customer Who Visited but Did Not Make Any Transactions.sql +++ b/BASIC JOIN/1581. Customer Who Visited but Did Not Make Any Transactions.sql @@ -1,6 +1,6 @@ -# Write your MySQL query statement below -SELECT v.customer_id, COUNT(v.visit_id) AS count_no_trans -FROM Visits v LEFT JOIN Transactions t -ON v.visit_id = t.visit_id -WHERE t.transaction_id IS NULL +# Write your MySQL query statement below +SELECT v.customer_id, COUNT(v.visit_id) AS count_no_trans +FROM Visits v LEFT JOIN Transactions t +ON v.visit_id = t.visit_id +WHERE t.transaction_id IS NULL GROUP BY v.customer_id; \ No newline at end of file diff --git a/BASIC JOIN/197. Rising Temperature.sql b/BASIC JOIN/197. Rising Temperature.sql index b5cc4b8..44d57c3 100644 --- a/BASIC JOIN/197. Rising Temperature.sql +++ b/BASIC JOIN/197. Rising Temperature.sql @@ -1,4 +1,4 @@ -# Write your MySQL query statement below -SELECT W1.id -FROM Weather AS W1, Weather AS W2 +# Write your MySQL query statement below +SELECT W1.id +FROM Weather AS W1, Weather AS W2 WHERE W1.Temperature > W2.Temperature AND DATEDIFF(W1.recordDate, W2.recordDate) = 1; \ No newline at end of file diff --git a/README.md b/README.md index 08a11db..614cfd5 100644 --- a/README.md +++ b/README.md @@ -1,44 +1,44 @@ -# LeetCode SQL TOP 50 Solutions - -This repository contains solutions to the top 50 SQL problems on LeetCode, covering advanced string functions, regex, clauses, basic aggregation functions, joins, sorting, grouping, subqueries, and more. - -## Repository Structure - -- `README.md`: An overview of the repository and how to use it. -- `advanced_string_functions_regex_clauses.sql`: Solutions for advanced string functions, regex, and clause-based problems. -- `basic_aggregation_function.sql`: Solutions focusing on basic aggregation functions like COUNT, SUM, AVG, etc. -- `basic_join.sql`: Solutions demonstrating various types of SQL joins (INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN). -- `select.sql`: Basic SELECT statement solutions. -- `sorting_and_group.sql`: Solutions for sorting and grouping data using ORDER BY and GROUP BY. -- `subqueries.sql`: Solutions involving subqueries. - -## How to Use This Repository - -1. Clone this repository to your local machine. -2. Open each SQL file in a text editor or IDE that supports SQL syntax highlighting. -3. Review the provided solutions and explanations. -4. Try solving the problems on your own before looking at the solutions. -5. Use these solutions as a reference or learning tool to improve your SQL skills. - -## Latest Commit - -**Author:** deepak14ri -**Commit Message:** new problem solved -**Latest Commit Hash:** b6bce78 -**Date:** 12 hours ago - -## History -- **SELECT**: Basic SELECT statement solutions to retrieve data from one or more tables. -- **BASIC AGGREGATION FUNCTION**: Solutions for filtering employee information using basic aggregation functions. -- **BASIC JOIN**: Demonstrates various join techniques to combine rows from two or more tables based on a related column. -- **SORTING AND GROUP**: Solutions for sorting and grouping employee information. -- **SUBQUERIES**: Solutions involving subqueries to perform operations within a query. -- **ADVANCED STRING FUNCTIONS + REGEX + CLAUSE**: Solutions for problems requiring advanced string manipulation, regular expressions, and specific SQL clauses. - -## Contributing - -Contributions are welcome Please feel free to submit a pull request if you have improvements or additional solutions. - -## License - -This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. +# LeetCode SQL TOP 50 Solutions + +This repository contains solutions to the top 50 SQL problems on LeetCode, covering advanced string functions, regex, clauses, basic aggregation functions, joins, sorting, grouping, subqueries, and more. + +## Repository Structure + +- `README.md`: An overview of the repository and how to use it. +- `advanced_string_functions_regex_clauses.sql`: Solutions for advanced string functions, regex, and clause-based problems. +- `basic_aggregation_function.sql`: Solutions focusing on basic aggregation functions like COUNT, SUM, AVG, etc. +- `basic_join.sql`: Solutions demonstrating various types of SQL joins (INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN). +- `select.sql`: Basic SELECT statement solutions. +- `sorting_and_group.sql`: Solutions for sorting and grouping data using ORDER BY and GROUP BY. +- `subqueries.sql`: Solutions involving subqueries. + +## How to Use This Repository + +1. Clone this repository to your local machine. +2. Open each SQL file in a text editor or IDE that supports SQL syntax highlighting. +3. Review the provided solutions and explanations. +4. Try solving the problems on your own before looking at the solutions. +5. Use these solutions as a reference or learning tool to improve your SQL skills. + +## Latest Commit + +**Author:** deepak14ri +**Commit Message:** new problem solved +**Latest Commit Hash:** b6bce78 +**Date:** 12 hours ago + +## History +- **SELECT**: Basic SELECT statement solutions to retrieve data from one or more tables. +- **BASIC AGGREGATION FUNCTION**: Solutions for filtering employee information using basic aggregation functions. +- **BASIC JOIN**: Demonstrates various join techniques to combine rows from two or more tables based on a related column. +- **SORTING AND GROUP**: Solutions for sorting and grouping employee information. +- **SUBQUERIES**: Solutions involving subqueries to perform operations within a query. +- **ADVANCED STRING FUNCTIONS + REGEX + CLAUSE**: Solutions for problems requiring advanced string manipulation, regular expressions, and specific SQL clauses. + +## Contributing + +Contributions are welcome Please feel free to submit a pull request if you have improvements or additional solutions. + +## License + +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. diff --git a/SELECT/1148. Article Views I.sql b/SELECT/1148. Article Views I.sql index 122ffe7..881117d 100644 --- a/SELECT/1148. Article Views I.sql +++ b/SELECT/1148. Article Views I.sql @@ -1,4 +1,4 @@ -# Write your MySQL query statement below -SELECT DISTINCT author_id as id from Views -WHERE author_id = viewer_id +# Write your MySQL query statement below +SELECT DISTINCT author_id as id from Views +WHERE author_id = viewer_id ORDER BY id; \ No newline at end of file diff --git a/SELECT/1683. Invalid Tweets.sql b/SELECT/1683. Invalid Tweets.sql index e3e56c4..dbcb81a 100644 --- a/SELECT/1683. Invalid Tweets.sql +++ b/SELECT/1683. Invalid Tweets.sql @@ -1,2 +1,2 @@ -# Write your MySQL query statement below +# Write your MySQL query statement below SELECT tweet_id FROM Tweets WHERE length(content) > 15; \ No newline at end of file diff --git a/SELECT/1757. Recyclable and Low Fat Products.sql b/SELECT/1757. Recyclable and Low Fat Products.sql index 48001a3..57edd1e 100644 --- a/SELECT/1757. Recyclable and Low Fat Products.sql +++ b/SELECT/1757. Recyclable and Low Fat Products.sql @@ -1,5 +1,5 @@ -# Write your MySQL query statement below -SELECT product_id -FROM Products -WHERE low_fats = 'Y' -AND recyclable = 'Y'; +# Write your MySQL query statement below +SELECT product_id +FROM Products +WHERE low_fats = 'Y' +AND recyclable = 'Y'; diff --git a/SELECT/584. Find Customer Referee.sql b/SELECT/584. Find Customer Referee.sql index 8cea979..11c5268 100644 --- a/SELECT/584. Find Customer Referee.sql +++ b/SELECT/584. Find Customer Referee.sql @@ -1,3 +1,3 @@ --- Write your PostgreSQL query statement below -SELECT name from Customer -WHERE referee_id is null or referee_id != 2; +-- Write your PostgreSQL query statement below +SELECT name from Customer +WHERE referee_id is null or referee_id != 2; diff --git a/SELECT/595. Big Countries.sql b/SELECT/595. Big Countries.sql index 23f745b..1e39d52 100644 --- a/SELECT/595. Big Countries.sql +++ b/SELECT/595. Big Countries.sql @@ -1,5 +1,5 @@ -# Write your MySQL query statement below -SELECT name, population, area -FROM World -WHERE area >= 3000000 +# Write your MySQL query statement below +SELECT name, population, area +FROM World +WHERE area >= 3000000 OR population >= 25000000; \ No newline at end of file diff --git a/SORTING AND GROUP/1070. Product Sales Analysis III.sql b/SORTING AND GROUP/1070. Product Sales Analysis III.sql index 1a64cf0..9d064da 100644 --- a/SORTING AND GROUP/1070. Product Sales Analysis III.sql +++ b/SORTING AND GROUP/1070. Product Sales Analysis III.sql @@ -1,11 +1,11 @@ -# Write your MySQL query statement below -SELECT product_id, year as first_year, quantity, price -FROM Sales -WHERE (product_id, year) -IN (SELECT product_id, - min(year) - FROM Sales - GROUP BY - product_id - ); - +# Write your MySQL query statement below +SELECT product_id, year as first_year, quantity, price +FROM Sales +WHERE (product_id, year) +IN (SELECT product_id, + min(year) + FROM Sales + GROUP BY + product_id + ); + diff --git a/SORTING AND GROUP/1141. User Activity for the Past 30 Days I.sql b/SORTING AND GROUP/1141. User Activity for the Past 30 Days I.sql index 6a99f9e..40de4ae 100644 --- a/SORTING AND GROUP/1141. User Activity for the Past 30 Days I.sql +++ b/SORTING AND GROUP/1141. User Activity for the Past 30 Days I.sql @@ -1,5 +1,5 @@ -# Write your MySQL query statement below -SELECT activity_date AS day, COUNT(DISTINCT user_id) AS active_users -FROM Activity -WHERE activity_date BETWEEN '2019-06-28' AND '2019-07-27' +# Write your MySQL query statement below +SELECT activity_date AS day, COUNT(DISTINCT user_id) AS active_users +FROM Activity +WHERE activity_date BETWEEN '2019-06-28' AND '2019-07-27' GROUP BY activity_date; \ No newline at end of file diff --git a/SORTING AND GROUP/2356. Number of Unique Subjects Taught by Each Teacher.sql b/SORTING AND GROUP/2356. Number of Unique Subjects Taught by Each Teacher.sql index 59d9ce1..a6e2e69 100644 --- a/SORTING AND GROUP/2356. Number of Unique Subjects Taught by Each Teacher.sql +++ b/SORTING AND GROUP/2356. Number of Unique Subjects Taught by Each Teacher.sql @@ -1,4 +1,4 @@ -# Write your MySQL query statement below -SELECT teacher_id, COUNT(DISTINCT subject_id) AS cnt -FROM Teacher +# Write your MySQL query statement below +SELECT teacher_id, COUNT(DISTINCT subject_id) AS cnt +FROM Teacher GROUP BY teacher_id; \ No newline at end of file diff --git a/SORTING AND GROUP/596. Classes More Than 5 Students.sql b/SORTING AND GROUP/596. Classes More Than 5 Students.sql index 74df932..5e1b408 100644 --- a/SORTING AND GROUP/596. Classes More Than 5 Students.sql +++ b/SORTING AND GROUP/596. Classes More Than 5 Students.sql @@ -1,3 +1,3 @@ -# Write your MySQL query statement below -SELECT class FROM Courses +# Write your MySQL query statement below +SELECT class FROM Courses GROUP BY class HAVING COUNT(class) >= 5; \ No newline at end of file diff --git a/SUBQUERIES/1321. Restaurant Growth.sql b/SUBQUERIES/1321. Restaurant Growth.sql index becc06b..87ac687 100644 --- a/SUBQUERIES/1321. Restaurant Growth.sql +++ b/SUBQUERIES/1321. Restaurant Growth.sql @@ -1,6 +1,6 @@ -SELECT visited_on, - SUM(amount) OVER (ORDER BY visited_on ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS amount, - ROUND(SUM(amount) OVER (ORDER BY visited_on ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) / 7, 2) AS average_amount -FROM customer -WHERE visited_on >= (SELECT DATE_ADD(MIN(visited_on), INTERVAL 6 DAY) FROM customer) -ORDER BY visited_on; +SELECT visited_on, + SUM(amount) OVER (ORDER BY visited_on ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS amount, + ROUND(SUM(amount) OVER (ORDER BY visited_on ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) / 7, 2) AS average_amount +FROM customer +WHERE visited_on >= (SELECT DATE_ADD(MIN(visited_on), INTERVAL 6 DAY) FROM customer) +ORDER BY visited_on; diff --git a/SUBQUERIES/1341. Movie Rating.sql b/SUBQUERIES/1341. Movie Rating.sql index 5d5a048..8847f0c 100644 --- a/SUBQUERIES/1341. Movie Rating.sql +++ b/SUBQUERIES/1341. Movie Rating.sql @@ -1,17 +1,17 @@ -# Write your MySQL query statement below -(SELECT name AS results -FROM Users -INNER JOIN MovieRating USING(user_id) -GROUP BY user_id -ORDER BY COUNT(rating) DESC, name -LIMIT 1) - -UNION ALL - -(SELECT title as results -FROM Movies -INNER JOIN MovieRating USING(movie_id) -WHERE MONTH(created_at) = '02' AND YEAR(created_at) = '2020' -GROUP BY title -ORDER BY AVG(rating) DESC, title +# Write your MySQL query statement below +(SELECT name AS results +FROM Users +INNER JOIN MovieRating USING(user_id) +GROUP BY user_id +ORDER BY COUNT(rating) DESC, name +LIMIT 1) + +UNION ALL + +(SELECT title as results +FROM Movies +INNER JOIN MovieRating USING(movie_id) +WHERE MONTH(created_at) = '02' AND YEAR(created_at) = '2020' +GROUP BY title +ORDER BY AVG(rating) DESC, title LIMIT 1) \ No newline at end of file diff --git a/SUBQUERIES/185. Department Top Three Salaries.sql b/SUBQUERIES/185. Department Top Three Salaries.sql index 1f31892..2de346d 100644 --- a/SUBQUERIES/185. Department Top Three Salaries.sql +++ b/SUBQUERIES/185. Department Top Three Salaries.sql @@ -1,13 +1,13 @@ --- Write your MySQL query statement below -SELECT Department, Employee, Salary -FROM ( - SELECT - d.name AS Department, - e.name AS Employee, - e.salary AS Salary, - DENSE_RANK() OVER (PARTITION BY d.name ORDER BY Salary DESC) AS rnk - FROM Employee e - JOIN Department d - ON e.departmentId = d.id -) AS rnk_tbl +-- Write your MySQL query statement below +SELECT Department, Employee, Salary +FROM ( + SELECT + d.name AS Department, + e.name AS Employee, + e.salary AS Salary, + DENSE_RANK() OVER (PARTITION BY d.name ORDER BY Salary DESC) AS rnk + FROM Employee e + JOIN Department d + ON e.departmentId = d.id +) AS rnk_tbl WHERE rnk <= 3; \ No newline at end of file diff --git a/SUBQUERIES/1978. Employees Whose Manager Left the Company.sql b/SUBQUERIES/1978. Employees Whose Manager Left the Company.sql index 546e3e4..dde29cc 100644 --- a/SUBQUERIES/1978. Employees Whose Manager Left the Company.sql +++ b/SUBQUERIES/1978. Employees Whose Manager Left the Company.sql @@ -1,5 +1,5 @@ -SELECT employee_id FROM Employees -WHERE salary < 30000 AND manager_id NOT IN ( - SELECT employee_id FROM Employees -) +SELECT employee_id FROM Employees +WHERE salary < 30000 AND manager_id NOT IN ( + SELECT employee_id FROM Employees +) ORDER BY employee_id; \ No newline at end of file diff --git a/SUBQUERIES/626. Exchange Seats.sql b/SUBQUERIES/626. Exchange Seats.sql index bedc6a6..b61ef4e 100644 --- a/SUBQUERIES/626. Exchange Seats.sql +++ b/SUBQUERIES/626. Exchange Seats.sql @@ -1,13 +1,13 @@ -# Write your MySQL query statement below -SELECT - CASE - WHEN id = (SELECT MAX(id) FROM seat) AND id % 2 = 1 - THEN id - WHEN id % 2 = 1 - THEN id + 1 - WHEN id % 2 = 0 - THEN id - 1 - END as id, student -FROM seat -ORDER BY id; - +# Write your MySQL query statement below +SELECT + CASE + WHEN id = (SELECT MAX(id) FROM seat) AND id % 2 = 1 + THEN id + WHEN id % 2 = 1 + THEN id + 1 + WHEN id % 2 = 0 + THEN id - 1 + END as id, student +FROM seat +ORDER BY id; +