Skip to content

Commit 005245c

Browse files
committed
Upload 5-Advanced-Select-and-Joins
1 parent 2b8e971 commit 005245c

4 files changed

+161
-9
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
-- Source: https://leetcode.com/problems/product-price-at-a-given-date/description/?envType=study-plan-v2&envId=top-sql-50
2+
3+
-- Table: Products
4+
5+
-- +---------------+---------+
6+
-- | Column Name | Type |
7+
-- +---------------+---------+
8+
-- | product_id | int |
9+
-- | new_price | int |
10+
-- | change_date | date |
11+
-- +---------------+---------+
12+
-- (product_id, change_date) is the primary key (combination of columns with unique values) of this table.
13+
-- Each row of this table indicates that the price of some product was changed to a new price at some date.
14+
15+
-- Write a solution to find the prices of all products on 2019-08-16. Assume the price of all products before any change is 10.
16+
17+
-- Return the result table in any order.
18+
19+
-- The result format is in the following example.
20+
21+
-- Example 1:
22+
23+
-- Input:
24+
-- Products table:
25+
-- +------------+-----------+-------------+
26+
-- | product_id | new_price | change_date |
27+
-- +------------+-----------+-------------+
28+
-- | 1 | 20 | 2019-08-14 |
29+
-- | 2 | 50 | 2019-08-14 |
30+
-- | 1 | 30 | 2019-08-15 |
31+
-- | 1 | 35 | 2019-08-16 |
32+
-- | 2 | 65 | 2019-08-17 |
33+
-- | 3 | 20 | 2019-08-18 |
34+
-- +------------+-----------+-------------+
35+
-- Output:
36+
-- +------------+-------+
37+
-- | product_id | price |
38+
-- +------------+-------+
39+
-- | 2 | 50 |
40+
-- | 1 | 35 |
41+
-- | 3 | 10 |
42+
-- +------------+-------+
43+
44+
------------------------------------------------------------------------------
45+
46+
-- SQL Schema
47+
48+
Create table If Not Exists Products (product_id int, new_price int, change_date date)
49+
Truncate table Products
50+
insert into Products (product_id, new_price, change_date) values ('1', '20', '2019-08-14')
51+
insert into Products (product_id, new_price, change_date) values ('2', '50', '2019-08-14')
52+
insert into Products (product_id, new_price, change_date) values ('1', '30', '2019-08-15')
53+
insert into Products (product_id, new_price, change_date) values ('1', '35', '2019-08-16')
54+
insert into Products (product_id, new_price, change_date) values ('2', '65', '2019-08-17')
55+
insert into Products (product_id, new_price, change_date) values ('3', '20', '2019-08-18')
56+
57+
-- MS SQL Server Code
58+
59+
SELECT p.product_id,
60+
COALESCE(
61+
(SELECT TOP 1 new_price
62+
FROM Products
63+
WHERE product_id = p.product_id AND change_date <= '2019-08-16'
64+
ORDER BY change_date DESC),
65+
10) AS price
66+
FROM (SELECT DISTINCT product_id FROM Products) AS p;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
-- Source: https://leetcode.com/problems/the-number-of-employees-which-report-to-each-employee/?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+
-- | reports_to | int |
11+
-- | age | int |
12+
-- +-------------+----------+
13+
-- employee_id is the primary key for this table.
14+
-- This table contains information about the employees and the id of the manager they report to. Some employees do not report to anyone (reports_to is null).
15+
16+
-- For this problem, we will consider a manager an employee who has at least 1 other employee reporting to them.
17+
18+
-- Write an SQL query to report the ids and the names of all managers, the number of employees who report directly to them, and the average age of the reports rounded to the nearest integer.
19+
20+
-- Return the result table ordered by employee_id.
21+
22+
------------------------------------------------------------------------------
23+
24+
-- SQL Schema
25+
26+
Create table If Not Exists Employees(employee_id int, name varchar(20), reports_to int, age int)
27+
Truncate table Employees
28+
insert into Employees (employee_id, name, reports_to, age) values ('9', 'Hercy', 'None', '43')
29+
insert into Employees (employee_id, name, reports_to, age) values ('6', 'Alice', '9', '41')
30+
insert into Employees (employee_id, name, reports_to, age) values ('4', 'Bob', '9', '36')
31+
insert into Employees (employee_id, name, reports_to, age) values ('2', 'Winston', 'None', '37')
32+
33+
-- MS SQL Server Code
34+
35+
SELECT e.employee_id, e.name, COUNT(*) AS 'reports_count', ROUND(CAST(SUM(m.age) AS FLOAT)/COUNT(*),0) AS 'average_age'
36+
FROM Employees e
37+
JOIN Employees m
38+
ON e.employee_id = m.reports_to
39+
GROUP BY e.employee_id, e.name
40+
ORDER BY e.employee_id
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
-- Source: https://leetcode.com/problems/primary-department-for-each-employee/description/?envType=study-plan-v2&envId=top-sql-50
2+
3+
-- Table: Employee
4+
5+
-- +---------------+---------+
6+
-- | Column Name | Type |
7+
-- +---------------+---------+
8+
-- | employee_id | int |
9+
-- | department_id | int |
10+
-- | primary_flag | varchar |
11+
-- +---------------+---------+
12+
-- (employee_id, department_id) is the primary key (combination of columns with unique values) for this table.
13+
-- employee_id is the id of the employee.
14+
-- department_id is the id of the department to which the employee belongs.
15+
-- primary_flag is an ENUM (category) of type ('Y', 'N'). If the flag is 'Y', the department is the primary department for the employee. If the flag is 'N', the department is not the primary.
16+
17+
-- Employees can belong to multiple departments. When the employee joins other departments, they need to decide which department is their primary department. Note that when an employee belongs to only one department, their primary column is 'N'.
18+
19+
-- Write a solution to report all the employees with their primary department. For employees who belong to one department, report their only department.
20+
21+
-- Return the result table in any order.
22+
23+
------------------------------------------------------------------------------
24+
25+
-- SQL Schema
26+
27+
Create table If Not Exists Employee (employee_id int, department_id int, primary_flag ENUM('Y','N'))
28+
Truncate table Employee
29+
insert into Employee (employee_id, department_id, primary_flag) values ('1', '1', 'N')
30+
insert into Employee (employee_id, department_id, primary_flag) values ('2', '1', 'Y')
31+
insert into Employee (employee_id, department_id, primary_flag) values ('2', '2', 'N')
32+
insert into Employee (employee_id, department_id, primary_flag) values ('3', '3', 'N')
33+
insert into Employee (employee_id, department_id, primary_flag) values ('4', '2', 'N')
34+
insert into Employee (employee_id, department_id, primary_flag) values ('4', '3', 'Y')
35+
insert into Employee (employee_id, department_id, primary_flag) values ('4', '4', 'N')
36+
37+
-- MS SQL Server Code
38+
39+
SELECT employee_id, department_id
40+
FROM Employee e
41+
WHERE employee_id IN (
42+
SELECT employee_id
43+
FROM Employee
44+
GROUP BY employee_id
45+
HAVING count(distinct department_id) = 1
46+
) OR primary_flag = "Y"

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ This github repository contains my solutions for the Top SQL 50 problems using M
7070

7171
### Advanced Select and Joins
7272

73-
| # | Problem | Level | Status |
74-
| ---- | --------------------------------------------------------- | ------ | ------ |
75-
| 1731 | [The Number of Employees Which Report to Each Employee]() | Easy | Solved |
76-
| 1789 | [Primary Department for Each Employee]() | Easy | Solved |
77-
| 610 | [Triangle Judgement]() | Easy | - |
78-
| 180 | [Consecutive Numbers]() | Medium | - |
79-
| 1164 | [Product Price at a Given Date]() | Medium | Solved |
80-
| 1204 | [Last Person to Fit in the Bus]() | Medium | - |
81-
| 1907 | [Count Salary Categories]() | Medium | - |
73+
| # | Problem | Level | Status |
74+
| ---- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | ------ | ------ |
75+
| 1731 | [The Number of Employees Which Report to Each Employee](/5-Advanced-Select-and-Joins/1731_The_Number_of_Employees_Which_Report_to_Each_Employee.sql) | Easy | Solved |
76+
| 1789 | [Primary Department for Each Employee](/5-Advanced-Select-and-Joins/1789_Primary_Department_for_Each_Employee.sql) | Easy | Solved |
77+
| 610 | Triangle Judgement | Easy | - |
78+
| 180 | Consecutive Numbers | Medium | - |
79+
| 1164 | [Product Price at a Given Date](/5-Advanced-Select-and-Joins/1164_Product_Price_at_a_Given_Date.sql) | Medium | Solved |
80+
| 1204 | Last Person to Fit in the Bus | Medium | - |
81+
| 1907 | Count Salary Categories | Medium | - |
8282

8383
### Subqueries
8484

0 commit comments

Comments
 (0)