Skip to content

Commit 91e529a

Browse files
author
deepak14ri
committed
test
1 parent c1dd09b commit 91e529a

30 files changed

+221
-221
lines changed
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
# Write your MySQL query statement below
2-
SELECT m.employee_id,
3-
m.name,
4-
count(e.employee_id) AS reports_count,
5-
round(avg(e.age)) AS average_age
6-
FROM
7-
employees e JOIN employees m
8-
ON
9-
e.reports_to = m.employee_id
10-
GROUP BY
11-
employee_id
12-
ORDER BY
1+
# Write your MySQL query statement below
2+
SELECT m.employee_id,
3+
m.name,
4+
count(e.employee_id) AS reports_count,
5+
round(avg(e.age)) AS average_age
6+
FROM
7+
employees e JOIN employees m
8+
ON
9+
e.reports_to = m.employee_id
10+
GROUP BY
11+
employee_id
12+
ORDER BY
1313
employee_id;
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# Write your MySQL query statement below
2-
SELECT employee_id, department_id
3-
FROM Employee
4-
WHERE primary_flag = 'Y'
5-
OR
6-
employee_id IN (
7-
SELECT employee_id
8-
FROM Employee
9-
GROUP BY employee_id
10-
HAVING COUNT(employee_id) = 1
1+
# Write your MySQL query statement below
2+
SELECT employee_id, department_id
3+
FROM Employee
4+
WHERE primary_flag = 'Y'
5+
OR
6+
employee_id IN (
7+
SELECT employee_id
8+
FROM Employee
9+
GROUP BY employee_id
10+
HAVING COUNT(employee_id) = 1
1111
);
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
-- Write your MySQL/PostgreSQL query statement below
2-
SELECT DISTINCT l1.num as ConsecutiveNums
3-
FROM Logs l1
4-
JOIN Logs l2 ON l1.num = l2.num and l1.id = l2.id + 1
5-
JOIN Logs l3 ON l2.num = l3.num and l2.id = l3.id + 1;
6-
1+
-- Write your MySQL/PostgreSQL query statement below
2+
SELECT DISTINCT l1.num as ConsecutiveNums
3+
FROM Logs l1
4+
JOIN Logs l2 ON l1.num = l2.num and l1.id = l2.id + 1
5+
JOIN Logs l3 ON l2.num = l3.num and l2.id = l3.id + 1;
6+
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
SELECT "Low Salary" AS category,
2-
sum(income < 20000) AS accounts_count
3-
FROM Accounts
4-
5-
UNION
6-
7-
SELECT "Average Salary" AS category,
8-
sum(income BETWEEN 20000 AND 50000) AS accounts_count
9-
FROM Accounts
10-
11-
UNION
12-
13-
SELECT "High Salary" AS category,
14-
sum(income > 50000) AS accounts_count
1+
SELECT "Low Salary" AS category,
2+
sum(income < 20000) AS accounts_count
3+
FROM Accounts
4+
5+
UNION
6+
7+
SELECT "Average Salary" AS category,
8+
sum(income BETWEEN 20000 AND 50000) AS accounts_count
9+
FROM Accounts
10+
11+
UNION
12+
13+
SELECT "High Salary" AS category,
14+
sum(income > 50000) AS accounts_count
1515
FROM Accounts;
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# Write your MySQL query statement below
2-
-- Using the triangle inequality:
3-
4-
-- x + y > z
5-
-- x + z > y
6-
-- y + z > x
7-
8-
SELECT x, y, z, CASE WHEN (x+y) > z AND (x+z) > y AND (y+z) > x THEN
9-
'Yes' ELSE 'No' END AS triangle
10-
FROM Triangle;
1+
# Write your MySQL query statement below
2+
-- Using the triangle inequality:
3+
4+
-- x + y > z
5+
-- x + z > y
6+
-- y + z > x
7+
8+
SELECT x, y, z, CASE WHEN (x+y) > z AND (x+z) > y AND (y+z) > x THEN
9+
'Yes' ELSE 'No' END AS triangle
10+
FROM Triangle;
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# Write your MySQL query statement below
2-
SELECT * FROM users
3-
WHERE mail REGEXP '^[a-zA-Z][a-zA-Z0-9_.-]*@leetcode[.]com$';
1+
# Write your MySQL query statement below
2+
SELECT * FROM users
3+
WHERE mail REGEXP '^[a-zA-Z][a-zA-Z0-9_.-]*@leetcode[.]com$';
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Write your MySQL query statement below
2-
SELECT user_id, CONCAT(UPPER(LEFT(name, 1)), LOWER(RIGHT(name, LENGTH(name)-1))) AS name
3-
FROM Users
1+
# Write your MySQL query statement below
2+
SELECT user_id, CONCAT(UPPER(LEFT(name, 1)), LOWER(RIGHT(name, LENGTH(name)-1))) AS name
3+
FROM Users
44
ORDER BY user_id;
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
-- Write your PostgreSQL query statement below
2-
SELECT p.project_id, ROUND(AVG(e.experience_years), 2) AS average_years
3-
FROM Project p
4-
LEFT JOIN Employee e ON p.employee_id = e.employee_id
5-
GROUP BY p.project_id;
1+
-- Write your PostgreSQL query statement below
2+
SELECT p.project_id, ROUND(AVG(e.experience_years), 2) AS average_years
3+
FROM Project p
4+
LEFT JOIN Employee e ON p.employee_id = e.employee_id
5+
GROUP BY p.project_id;
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Write your MySQL query statement below
2-
SELECT p.product_id, IFNULL(ROUND(SUM(units*price)/SUM(units),2),0) AS average_price
3-
FROM Prices p LEFT JOIN UnitsSold u
4-
ON p.product_id = u.product_id AND
5-
u.purchase_date BETWEEN start_date AND end_date
1+
# Write your MySQL query statement below
2+
SELECT p.product_id, IFNULL(ROUND(SUM(units*price)/SUM(units),2),0) AS average_price
3+
FROM Prices p LEFT JOIN UnitsSold u
4+
ON p.product_id = u.product_id AND
5+
u.purchase_date BETWEEN start_date AND end_date
66
group by product_id
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# Write your MySQL query statement below
2-
SELECT contest_id, ROUND((COUNT(DISTINCT user_id)*100)/(
3-
SELECT COUNT(user_id) FROM Users
4-
), 2) AS percentage
5-
FROM Register
6-
GROUP BY contest_id
1+
# Write your MySQL query statement below
2+
SELECT contest_id, ROUND((COUNT(DISTINCT user_id)*100)/(
3+
SELECT COUNT(user_id) FROM Users
4+
), 2) AS percentage
5+
FROM Register
6+
GROUP BY contest_id
77
ORDER BY percentage DESC, contest_id;
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Write your MySQL query statement below
2-
SELECT id, movie, description, rating
3-
FROM Cinema
4-
WHERE MOD(id, 2) <> 0 AND description <> "boring"
1+
# Write your MySQL query statement below
2+
SELECT id, movie, description, rating
3+
FROM Cinema
4+
WHERE MOD(id, 2) <> 0 AND description <> "boring"
55
ORDER BY rating DESC;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# Write your MySQL query statement below
2-
SELECT p.product_name, s.year, s.price
1+
# Write your MySQL query statement below
2+
SELECT p.product_name, s.year, s.price
33
FROM Sales s LEFT JOIN Product p ON s.product_id = p.product_id;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# Write your MySQL query statement below
2-
SELECT eu.unique_id AS unique_id, e.name AS name
1+
# Write your MySQL query statement below
2+
SELECT eu.unique_id AS unique_id, e.name AS name
33
FROM Employees e LEFT JOIN EmployeeUNI eu ON e.id = eu.id;
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Write your MySQL query statement below
2-
SELECT v.customer_id, COUNT(v.visit_id) AS count_no_trans
3-
FROM Visits v LEFT JOIN Transactions t
4-
ON v.visit_id = t.visit_id
5-
WHERE t.transaction_id IS NULL
1+
# Write your MySQL query statement below
2+
SELECT v.customer_id, COUNT(v.visit_id) AS count_no_trans
3+
FROM Visits v LEFT JOIN Transactions t
4+
ON v.visit_id = t.visit_id
5+
WHERE t.transaction_id IS NULL
66
GROUP BY v.customer_id;
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Write your MySQL query statement below
2-
SELECT W1.id
3-
FROM Weather AS W1, Weather AS W2
1+
# Write your MySQL query statement below
2+
SELECT W1.id
3+
FROM Weather AS W1, Weather AS W2
44
WHERE W1.Temperature > W2.Temperature AND DATEDIFF(W1.recordDate, W2.recordDate) = 1;

README.md

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
1-
# LeetCode SQL TOP 50 Solutions
2-
3-
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.
4-
5-
## Repository Structure
6-
7-
- `README.md`: An overview of the repository and how to use it.
8-
- `advanced_string_functions_regex_clauses.sql`: Solutions for advanced string functions, regex, and clause-based problems.
9-
- `basic_aggregation_function.sql`: Solutions focusing on basic aggregation functions like COUNT, SUM, AVG, etc.
10-
- `basic_join.sql`: Solutions demonstrating various types of SQL joins (INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN).
11-
- `select.sql`: Basic SELECT statement solutions.
12-
- `sorting_and_group.sql`: Solutions for sorting and grouping data using ORDER BY and GROUP BY.
13-
- `subqueries.sql`: Solutions involving subqueries.
14-
15-
## How to Use This Repository
16-
17-
1. Clone this repository to your local machine.
18-
2. Open each SQL file in a text editor or IDE that supports SQL syntax highlighting.
19-
3. Review the provided solutions and explanations.
20-
4. Try solving the problems on your own before looking at the solutions.
21-
5. Use these solutions as a reference or learning tool to improve your SQL skills.
22-
23-
## Latest Commit
24-
25-
**Author:** deepak14ri
26-
**Commit Message:** test
27-
**Latest Commit Hash:** b6bce78
28-
**Date:** 12 hours ago
29-
30-
## History
31-
- **SELECT**: Basic SELECT statement solutions to retrieve data from one or more tables.
32-
- **BASIC AGGREGATION FUNCTION**: Solutions for filtering employee information using basic aggregation functions.
33-
- **BASIC JOIN**: Demonstrates various join techniques to combine rows from two or more tables based on a related column.
34-
- **SORTING AND GROUP**: Solutions for sorting and grouping employee information.
35-
- **SUBQUERIES**: Solutions involving subqueries to perform operations within a query.
36-
- **ADVANCED STRING FUNCTIONS + REGEX + CLAUSE**: Solutions for problems requiring advanced string manipulation, regular expressions, and specific SQL clauses.
37-
38-
## Contributing
39-
40-
Contributions are welcome Please feel free to submit a pull request if you have improvements or additional solutions.
41-
42-
## License
43-
44-
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
1+
# LeetCode SQL TOP 50 Solutions
2+
3+
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.
4+
5+
## Repository Structure
6+
7+
- `README.md`: An overview of the repository and how to use it.
8+
- `advanced_string_functions_regex_clauses.sql`: Solutions for advanced string functions, regex, and clause-based problems.
9+
- `basic_aggregation_function.sql`: Solutions focusing on basic aggregation functions like COUNT, SUM, AVG, etc.
10+
- `basic_join.sql`: Solutions demonstrating various types of SQL joins (INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN).
11+
- `select.sql`: Basic SELECT statement solutions.
12+
- `sorting_and_group.sql`: Solutions for sorting and grouping data using ORDER BY and GROUP BY.
13+
- `subqueries.sql`: Solutions involving subqueries.
14+
15+
## How to Use This Repository
16+
17+
1. Clone this repository to your local machine.
18+
2. Open each SQL file in a text editor or IDE that supports SQL syntax highlighting.
19+
3. Review the provided solutions and explanations.
20+
4. Try solving the problems on your own before looking at the solutions.
21+
5. Use these solutions as a reference or learning tool to improve your SQL skills.
22+
23+
## Latest Commit
24+
25+
**Author:** deepak14ri
26+
**Commit Message:** new problem solved
27+
**Latest Commit Hash:** b6bce78
28+
**Date:** 12 hours ago
29+
30+
## History
31+
- **SELECT**: Basic SELECT statement solutions to retrieve data from one or more tables.
32+
- **BASIC AGGREGATION FUNCTION**: Solutions for filtering employee information using basic aggregation functions.
33+
- **BASIC JOIN**: Demonstrates various join techniques to combine rows from two or more tables based on a related column.
34+
- **SORTING AND GROUP**: Solutions for sorting and grouping employee information.
35+
- **SUBQUERIES**: Solutions involving subqueries to perform operations within a query.
36+
- **ADVANCED STRING FUNCTIONS + REGEX + CLAUSE**: Solutions for problems requiring advanced string manipulation, regular expressions, and specific SQL clauses.
37+
38+
## Contributing
39+
40+
Contributions are welcome Please feel free to submit a pull request if you have improvements or additional solutions.
41+
42+
## License
43+
44+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

SELECT/1148. Article Views I.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Write your MySQL query statement below
2-
SELECT DISTINCT author_id as id from Views
3-
WHERE author_id = viewer_id
1+
# Write your MySQL query statement below
2+
SELECT DISTINCT author_id as id from Views
3+
WHERE author_id = viewer_id
44
ORDER BY id;

SELECT/1683. Invalid Tweets.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# Write your MySQL query statement below
1+
# Write your MySQL query statement below
22
SELECT tweet_id FROM Tweets WHERE length(content) > 15;
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Write your MySQL query statement below
2-
SELECT product_id
3-
FROM Products
4-
WHERE low_fats = 'Y'
5-
AND recyclable = 'Y';
1+
# Write your MySQL query statement below
2+
SELECT product_id
3+
FROM Products
4+
WHERE low_fats = 'Y'
5+
AND recyclable = 'Y';

SELECT/584. Find Customer Referee.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
-- Write your PostgreSQL query statement below
2-
SELECT name from Customer
3-
WHERE referee_id is null or referee_id != 2;
1+
-- Write your PostgreSQL query statement below
2+
SELECT name from Customer
3+
WHERE referee_id is null or referee_id != 2;

SELECT/595. Big Countries.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Write your MySQL query statement below
2-
SELECT name, population, area
3-
FROM World
4-
WHERE area >= 3000000
1+
# Write your MySQL query statement below
2+
SELECT name, population, area
3+
FROM World
4+
WHERE area >= 3000000
55
OR population >= 25000000;
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# Write your MySQL query statement below
2-
SELECT product_id, year as first_year, quantity, price
3-
FROM Sales
4-
WHERE (product_id, year)
5-
IN (SELECT product_id,
6-
min(year)
7-
FROM Sales
8-
GROUP BY
9-
product_id
10-
);
11-
1+
# Write your MySQL query statement below
2+
SELECT product_id, year as first_year, quantity, price
3+
FROM Sales
4+
WHERE (product_id, year)
5+
IN (SELECT product_id,
6+
min(year)
7+
FROM Sales
8+
GROUP BY
9+
product_id
10+
);
11+
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Write your MySQL query statement below
2-
SELECT activity_date AS day, COUNT(DISTINCT user_id) AS active_users
3-
FROM Activity
4-
WHERE activity_date BETWEEN '2019-06-28' AND '2019-07-27'
1+
# Write your MySQL query statement below
2+
SELECT activity_date AS day, COUNT(DISTINCT user_id) AS active_users
3+
FROM Activity
4+
WHERE activity_date BETWEEN '2019-06-28' AND '2019-07-27'
55
GROUP BY activity_date;
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Write your MySQL query statement below
2-
SELECT teacher_id, COUNT(DISTINCT subject_id) AS cnt
3-
FROM Teacher
1+
# Write your MySQL query statement below
2+
SELECT teacher_id, COUNT(DISTINCT subject_id) AS cnt
3+
FROM Teacher
44
GROUP BY teacher_id;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# Write your MySQL query statement below
2-
SELECT class FROM Courses
1+
# Write your MySQL query statement below
2+
SELECT class FROM Courses
33
GROUP BY class HAVING COUNT(class) >= 5;

0 commit comments

Comments
 (0)