Skip to content

Commit 09314fa

Browse files
committed
Upload 6-Subqueries
1 parent 005245c commit 09314fa

4 files changed

+304
-18
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
-- Source: https://leetcode.com/problems/restaurant-growth/description/?envType=study-plan-v2&envId=top-sql-50
2+
3+
-- Table: Customer
4+
5+
-- +---------------+---------+
6+
-- | Column Name | Type |
7+
-- +---------------+---------+
8+
-- | customer_id | int |
9+
-- | name | varchar |
10+
-- | visited_on | date |
11+
-- | amount | int |
12+
-- +---------------+---------+
13+
-- In SQL,(customer_id, visited_on) is the primary key for this table.
14+
-- This table contains data about customer transactions in a restaurant.
15+
-- visited_on is the date on which the customer with ID (customer_id) has visited the restaurant.
16+
-- amount is the total paid by a customer.
17+
18+
-- You are the restaurant owner and you want to analyze a possible expansion (there will be at least one customer every day).
19+
20+
-- 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.
21+
22+
-- Return the result table ordered by visited_on in ascending order.
23+
24+
-- The result format is in the following example.
25+
26+
-- Example 1:
27+
28+
-- Input:
29+
-- Customer table:
30+
-- +-------------+--------------+--------------+-------------+
31+
-- | customer_id | name | visited_on | amount |
32+
-- +-------------+--------------+--------------+-------------+
33+
-- | 1 | Jhon | 2019-01-01 | 100 |
34+
-- | 2 | Daniel | 2019-01-02 | 110 |
35+
-- | 3 | Jade | 2019-01-03 | 120 |
36+
-- | 4 | Khaled | 2019-01-04 | 130 |
37+
-- | 5 | Winston | 2019-01-05 | 110 |
38+
-- | 6 | Elvis | 2019-01-06 | 140 |
39+
-- | 7 | Anna | 2019-01-07 | 150 |
40+
-- | 8 | Maria | 2019-01-08 | 80 |
41+
-- | 9 | Jaze | 2019-01-09 | 110 |
42+
-- | 1 | Jhon | 2019-01-10 | 130 |
43+
-- | 3 | Jade | 2019-01-10 | 150 |
44+
-- +-------------+--------------+--------------+-------------+
45+
-- Output:
46+
-- +--------------+--------------+----------------+
47+
-- | visited_on | amount | average_amount |
48+
-- +--------------+--------------+----------------+
49+
-- | 2019-01-07 | 860 | 122.86 |
50+
-- | 2019-01-08 | 840 | 120 |
51+
-- | 2019-01-09 | 840 | 120 |
52+
-- | 2019-01-10 | 1000 | 142.86 |
53+
-- +--------------+--------------+----------------+
54+
-- Explanation:
55+
-- 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
56+
-- 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
57+
-- 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
58+
-- 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
59+
60+
------------------------------------------------------------------------------
61+
62+
-- SQL Schema
63+
64+
Create table If Not Exists Customer (customer_id int, name varchar(20), visited_on date, amount int)
65+
Truncate table Customer
66+
insert into Customer (customer_id, name, visited_on, amount) values ('1', 'Jhon', '2019-01-01', '100')
67+
insert into Customer (customer_id, name, visited_on, amount) values ('2', 'Daniel', '2019-01-02', '110')
68+
insert into Customer (customer_id, name, visited_on, amount) values ('3', 'Jade', '2019-01-03', '120')
69+
insert into Customer (customer_id, name, visited_on, amount) values ('4', 'Khaled', '2019-01-04', '130')
70+
insert into Customer (customer_id, name, visited_on, amount) values ('5', 'Winston', '2019-01-05', '110')
71+
insert into Customer (customer_id, name, visited_on, amount) values ('6', 'Elvis', '2019-01-06', '140')
72+
insert into Customer (customer_id, name, visited_on, amount) values ('7', 'Anna', '2019-01-07', '150')
73+
insert into Customer (customer_id, name, visited_on, amount) values ('8', 'Maria', '2019-01-08', '80')
74+
insert into Customer (customer_id, name, visited_on, amount) values ('9', 'Jaze', '2019-01-09', '110')
75+
insert into Customer (customer_id, name, visited_on, amount) values ('1', 'Jhon', '2019-01-10', '130')
76+
insert into Customer (customer_id, name, visited_on, amount) values ('3', 'Jade', '2019-01-10', '150')
77+
78+
-- MS SQL Server Code
79+
80+
WITH
81+
amount_per_day AS (
82+
SELECT
83+
visited_on
84+
, SUM(SUM(amount)) OVER(ORDER BY visited_on
85+
ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS amount
86+
, ROUND(AVG(SUM(amount)*1.0) OVER(ORDER BY visited_on
87+
ROWS BETWEEN 6 PRECEDING AND CURRENT ROW),2) AS average_amount
88+
FROM Customer
89+
GROUP BY visited_on
90+
),
91+
starting_date AS (
92+
SELECT DATEADD(day, 6, earliest_date) AS starting_date
93+
FROM
94+
( SELECT MIN(visited_on) AS earliest_date
95+
FROM amount_per_day
96+
) AS a
97+
)
98+
99+
SELECT visited_on, amount, average_amount
100+
FROM amount_per_day
101+
CROSS JOIN starting_date
102+
WHERE visited_on >= starting_date
103+
ORDER BY visited_on;
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
-- Source: https://leetcode.com/problems/department-top-three-salaries/description/?envType=study-plan-v2&envId=top-sql-50
2+
3+
-- Table: Employee
4+
5+
-- +--------------+---------+
6+
-- | Column Name | Type |
7+
-- +--------------+---------+
8+
-- | id | int |
9+
-- | name | varchar |
10+
-- | salary | int |
11+
-- | departmentId | int |
12+
-- +--------------+---------+
13+
-- id is the primary key (column with unique values) for this table.
14+
-- departmentId is a foreign key (reference column) of the ID from the Department table.
15+
-- Each row of this table indicates the ID, name, and salary of an employee. It also contains the ID of their department.
16+
17+
-- Table: Department
18+
19+
-- +-------------+---------+
20+
-- | Column Name | Type |
21+
-- +-------------+---------+
22+
-- | id | int |
23+
-- | name | varchar |
24+
-- +-------------+---------+
25+
-- id is the primary key (column with unique values) for this table.
26+
-- Each row of this table indicates the ID of a department and its name.
27+
28+
-- A company's executives are interested in seeing who earns the most money in each of the company's departments. A high earner in a department is an employee who has a salary in the top three unique salaries for that department.
29+
30+
-- Write a solution to find the employees who are high earners in each of the departments.
31+
32+
-- Return the result table in any order.
33+
34+
-- The result format is in the following example.
35+
36+
-- Example 1:
37+
38+
-- Input:
39+
-- Employee table:
40+
-- +----+-------+--------+--------------+
41+
-- | id | name | salary | departmentId |
42+
-- +----+-------+--------+--------------+
43+
-- | 1 | Joe | 85000 | 1 |
44+
-- | 2 | Henry | 80000 | 2 |
45+
-- | 3 | Sam | 60000 | 2 |
46+
-- | 4 | Max | 90000 | 1 |
47+
-- | 5 | Janet | 69000 | 1 |
48+
-- | 6 | Randy | 85000 | 1 |
49+
-- | 7 | Will | 70000 | 1 |
50+
-- +----+-------+--------+--------------+
51+
-- Department table:
52+
-- +----+-------+
53+
-- | id | name |
54+
-- +----+-------+
55+
-- | 1 | IT |
56+
-- | 2 | Sales |
57+
-- +----+-------+
58+
-- Output:
59+
-- +------------+----------+--------+
60+
-- | Department | Employee | Salary |
61+
-- +------------+----------+--------+
62+
-- | IT | Max | 90000 |
63+
-- | IT | Joe | 85000 |
64+
-- | IT | Randy | 85000 |
65+
-- | IT | Will | 70000 |
66+
-- | Sales | Henry | 80000 |
67+
-- | Sales | Sam | 60000 |
68+
-- +------------+----------+--------+
69+
-- Explanation:
70+
-- In the IT department:
71+
-- - Max earns the highest unique salary
72+
-- - Both Randy and Joe earn the second-highest unique salary
73+
-- - Will earns the third-highest unique salary
74+
75+
-- In the Sales department:
76+
-- - Henry earns the highest salary
77+
-- - Sam earns the second-highest salary
78+
-- - There is no third-highest salary as there are only two employees
79+
80+
------------------------------------------------------------------------------
81+
82+
-- SQL Schema
83+
84+
Create table If Not Exists Employee (id int, name varchar(255), salary int, departmentId int)
85+
Create table If Not Exists Department (id int, name varchar(255))
86+
Truncate table Employee
87+
insert into Employee (id, name, salary, departmentId) values ('1', 'Joe', '85000', '1')
88+
insert into Employee (id, name, salary, departmentId) values ('2', 'Henry', '80000', '2')
89+
insert into Employee (id, name, salary, departmentId) values ('3', 'Sam', '60000', '2')
90+
insert into Employee (id, name, salary, departmentId) values ('4', 'Max', '90000', '1')
91+
insert into Employee (id, name, salary, departmentId) values ('5', 'Janet', '69000', '1')
92+
insert into Employee (id, name, salary, departmentId) values ('6', 'Randy', '85000', '1')
93+
insert into Employee (id, name, salary, departmentId) values ('7', 'Will', '70000', '1')
94+
Truncate table Department
95+
insert into Department (id, name) values ('1', 'IT')
96+
insert into Department (id, name) values ('2', 'Sales')
97+
98+
-- MS SQL Server Code
99+
100+
SELECT d.name AS Department, e.name AS Employee, e.salary AS Salary
101+
FROM Employee e
102+
JOIN Department d
103+
ON e.departmentId = d.id
104+
JOIN (
105+
SELECT *, DENSE_RANK() OVER(PARTITION BY departmentId ORDER BY salary DESC) AS 'rank'
106+
FROM Employee
107+
) AS t
108+
ON t.id = e.id
109+
WHERE t.rank <= 3
110+
ORDER BY 1, 3 DESC
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
-- Source: https://leetcode.com/problems/employees-whose-manager-left-the-company/description/?envType=study-plan-v2&envId=top-sql-50
2+
3+
-- Table: Employees
4+
5+
-- +-------------+----------+
6+
-- | Column Name | Type |
7+
-- +-------------+----------+
8+
-- | employee_id | int |
9+
-- | name | varchar |
10+
-- | manager_id | int |
11+
-- | salary | int |
12+
-- +-------------+----------+
13+
-- In SQL, employee_id is the primary key for this table.
14+
-- This table contains information about the employees, their salary, and the ID of their manager. Some employees do not have a manager (manager_id is null).
15+
16+
-- Find the IDs of the employees whose salary is strictly less than $30000 and whose manager left the company. When a manager leaves the company, their information is deleted from the Employees table, but the reports still have their manager_id set to the manager that left.
17+
18+
-- Return the result table ordered by employee_id.
19+
20+
-- The result format is in the following example.
21+
22+
-- Example 1:
23+
24+
-- Input:
25+
-- Employees table:
26+
-- +-------------+-----------+------------+--------+
27+
-- | employee_id | name | manager_id | salary |
28+
-- +-------------+-----------+------------+--------+
29+
-- | 3 | Mila | 9 | 60301 |
30+
-- | 12 | Antonella | null | 31000 |
31+
-- | 13 | Emery | null | 67084 |
32+
-- | 1 | Kalel | 11 | 21241 |
33+
-- | 9 | Mikaela | null | 50937 |
34+
-- | 11 | Joziah | 6 | 28485 |
35+
-- +-------------+-----------+------------+--------+
36+
-- Output:
37+
-- +-------------+
38+
-- | employee_id |
39+
-- +-------------+
40+
-- | 11 |
41+
-- +-------------+
42+
43+
-- Explanation:
44+
-- The employees with a salary less than $30000 are 1 (Kalel) and 11 (Joziah).
45+
-- Kalel's manager is employee 11, who is still in the company (Joziah).
46+
-- Joziah's manager is employee 6, who left the company because there is no row for employee 6 as it was deleted.
47+
48+
------------------------------------------------------------------------------
49+
50+
-- SQL Schema
51+
52+
Create table If Not Exists Employees (employee_id int, name varchar(20), manager_id int, salary int)
53+
Truncate table Employees
54+
insert into Employees (employee_id, name, manager_id, salary) values ('3', 'Mila', '9', '60301')
55+
insert into Employees (employee_id, name, manager_id, salary) values ('12', 'Antonella', 'None', '31000')
56+
insert into Employees (employee_id, name, manager_id, salary) values ('13', 'Emery', 'None', '67084')
57+
insert into Employees (employee_id, name, manager_id, salary) values ('1', 'Kalel', '11', '21241')
58+
insert into Employees (employee_id, name, manager_id, salary) values ('9', 'Mikaela', 'None', '50937')
59+
insert into Employees (employee_id, name, manager_id, salary) values ('11', 'Joziah', '6', '28485')
60+
61+
-- MS SQL Server Code
62+
63+
SELECT DISTINCT e.employee_id
64+
FROM Employees e
65+
WHERE e.manager_id IS NOT NULL
66+
AND e.salary < 30000
67+
AND NOT EXISTS
68+
(
69+
SELECT employee_id
70+
FROM Employees
71+
WHERE employee_id = e.manager_id
72+
)
73+
ORDER BY employee_id;

README.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,24 +82,24 @@ This github repository contains my solutions for the Top SQL 50 problems using M
8282

8383
### Subqueries
8484

85-
| # | Problem | Level | Status |
86-
| ---- | ------------------------------------------------ | ------ | ------ |
87-
| 1978 | [Employees Whose Manager Left the Company]() | Easy | Solved |
88-
| 626 | [Exchange Seats]() | Medium | - |
89-
| 1341 | [Movie Rating]() | Medium | - |
90-
| 1321 | [Restaurant Growth]() | Medium | Solved |
91-
| 602 | [Friend Requests II: Who Has the Most Friends]() | Medium | - |
92-
| 585 | [Investments in 2016]() | Medium | - |
93-
| 185 | [Department Top Three Salaries]() | Hard | Solved |
85+
| # | Problem | Level | Status |
86+
| ---- | ----------------------------------------------------------------------------------------------------------- | ------ | ------ |
87+
| 1978 | [Employees Whose Manager Left the Company](/6-Subqueries/1978_Employees_Whose_Manager_Left_the_Company.sql) | Easy | Solved |
88+
| 626 | Exchange Seats | Medium | - |
89+
| 1341 | Movie Rating | Medium | - |
90+
| 1321 | [Restaurant Growth](/6-Subqueries/1321_Restaurant_Growth.sql) | Medium | Solved |
91+
| 602 | Friend Requests II: Who Has the Most Friends | Medium | - |
92+
| 585 | Investments in 2016 | Medium | - |
93+
| 185 | [Department Top Three Salaries](/6-Subqueries/185_Department_Top_Three_Salaries.sql) | Hard | Solved |
9494

9595
### Advanced String Functions / Regex Clause
9696

97-
| # | Problem | Level | Status |
98-
| ---- | ----------------------------------------- | ------ | ------ |
99-
| 1667 | [Fix Names in a Table]() | Easy | - |
100-
| 1527 | [Patients with a Condition]() | Easy | - |
101-
| 196 | [Delete Duplicate Emails]() | Easy | Solved |
102-
| 176 | [Second Highest Salary]() | Medium | Solved |
103-
| 1484 | [Group Sold Products By The Date]() | Easy | - |
104-
| 1327 | [List the Products Ordered in a Period]() | Easy | - |
105-
| 1517 | [Find Users With Valid E-mails]() | Easy | Solved |
97+
| # | Problem | Level | Status |
98+
| ---- | ------------------------------------- | ------ | ------ |
99+
| 1667 | Fix Names in a Table | Easy | - |
100+
| 1527 | Patients with a Condition | Easy | - |
101+
| 196 | [Delete Duplicate Emails]() | Easy | Solved |
102+
| 176 | [Second Highest Salary]() | Medium | Solved |
103+
| 1484 | Group Sold Products By The Date | Easy | - |
104+
| 1327 | List the Products Ordered in a Period | Easy | - |
105+
| 1517 | [Find Users With Valid E-mails]() | Easy | Solved |

0 commit comments

Comments
 (0)