Skip to content

Commit 73aed0d

Browse files
authored
Attempted 5
1 parent 7d8f391 commit 73aed0d

File tree

5 files changed

+292
-0
lines changed

5 files changed

+292
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
-- Question 55
2+
-- Table: Employees
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | employee_id | int |
8+
-- | employee_name | varchar |
9+
-- | manager_id | int |
10+
-- +---------------+---------+
11+
-- employee_id is the primary key for this table.
12+
-- Each row of this table indicates that the employee with ID employee_id and name employee_name reports his
13+
-- work to his/her direct manager with manager_id
14+
-- The head of the company is the employee with employee_id = 1.
15+
16+
17+
-- Write an SQL query to find employee_id of all employees that directly or indirectly report their work to the head of the company.
18+
19+
-- The indirect relation between managers will not exceed 3 managers as the company is small.
20+
21+
-- Return result table in any order without duplicates.
22+
23+
-- The query result format is in the following example:
24+
25+
-- Employees table:
26+
-- +-------------+---------------+------------+
27+
-- | employee_id | employee_name | manager_id |
28+
-- +-------------+---------------+------------+
29+
-- | 1 | Boss | 1 |
30+
-- | 3 | Alice | 3 |
31+
-- | 2 | Bob | 1 |
32+
-- | 4 | Daniel | 2 |
33+
-- | 7 | Luis | 4 |
34+
-- | 8 | Jhon | 3 |
35+
-- | 9 | Angela | 8 |
36+
-- | 77 | Robert | 1 |
37+
-- +-------------+---------------+------------+
38+
39+
-- Result table:
40+
-- +-------------+
41+
-- | employee_id |
42+
-- +-------------+
43+
-- | 2 |
44+
-- | 77 |
45+
-- | 4 |
46+
-- | 7 |
47+
-- +-------------+
48+
49+
-- The head of the company is the employee with employee_id 1.
50+
-- The employees with employee_id 2 and 77 report their work directly to the head of the company.
51+
-- The employee with employee_id 4 report his work indirectly to the head of the company 4 --> 2 --> 1.
52+
-- The employee with employee_id 7 report his work indirectly to the head of the company 7 --> 4 --> 2 --> 1.
53+
-- The employees with employee_id 3, 8 and 9 don't report their work to head of company directly or indirectly.
54+
55+
-- Solution
56+
select employee_id
57+
from employees
58+
where manager_id = 1 and employee_id != 1
59+
union
60+
select employee_id
61+
from employees
62+
where manager_id = any (select employee_id
63+
from employees
64+
where manager_id = 1 and employee_id != 1)
65+
union
66+
select employee_id
67+
from employees
68+
where manager_id = any (select employee_id
69+
from employees
70+
where manager_id = any (select employee_id
71+
from employees
72+
where manager_id = 1 and employee_id != 1))
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
-- Question 57
2+
-- The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id.
3+
4+
-- +----+-------+--------+--------------+
5+
-- | Id | Name | Salary | DepartmentId |
6+
-- +----+-------+--------+--------------+
7+
-- | 1 | Joe | 70000 | 1 |
8+
-- | 2 | Jim | 90000 | 1 |
9+
-- | 3 | Henry | 80000 | 2 |
10+
-- | 4 | Sam | 60000 | 2 |
11+
-- | 5 | Max | 90000 | 1 |
12+
-- +----+-------+--------+--------------+
13+
-- The Department table holds all departments of the company.
14+
15+
-- +----+----------+
16+
-- | Id | Name |
17+
-- +----+----------+
18+
-- | 1 | IT |
19+
-- | 2 | Sales |
20+
-- +----+----------+
21+
-- Write a SQL query to find employees who have the highest salary in each of the departments.
22+
-- For the above tables, your SQL query should return the following rows (order of rows does not matter).
23+
24+
-- +------------+----------+--------+
25+
-- | Department | Employee | Salary |
26+
-- +------------+----------+--------+
27+
-- | IT | Max | 90000 |
28+
-- | IT | Jim | 90000 |
29+
-- | Sales | Henry | 80000 |
30+
-- +------------+----------+--------+
31+
-- Explanation:
32+
33+
-- Max and Jim both have the highest salary in the IT department and Henry has the highest salary in the Sales department.
34+
35+
-- Solution
36+
select a.Department, a.Employee, a.Salary
37+
from(
38+
select d.name as Department, e.name as Employee, Salary,
39+
rank() over(partition by d.name order by salary desc) as rk
40+
from employee e
41+
join department d
42+
on e.departmentid = d.id) a
43+
where a.rk=1

Medium/Exchange Seats.sql

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
-- Question 56
2+
-- Mary is a teacher in a middle school and she has a table seat storing students' names and their corresponding seat ids.
3+
4+
-- The column id is continuous increment.
5+
6+
7+
-- Mary wants to change seats for the adjacent students.
8+
9+
10+
-- Can you write a SQL query to output the result for Mary?
11+
12+
13+
-- +---------+---------+
14+
-- | id | student |
15+
-- +---------+---------+
16+
-- | 1 | Abbot |
17+
-- | 2 | Doris |
18+
-- | 3 | Emerson |
19+
-- | 4 | Green |
20+
-- | 5 | Jeames |
21+
-- +---------+---------+
22+
-- For the sample input, the output is:
23+
24+
25+
-- +---------+---------+
26+
-- | id | student |
27+
-- +---------+---------+
28+
-- | 1 | Doris |
29+
-- | 2 | Abbot |
30+
-- | 3 | Green |
31+
-- | 4 | Emerson |
32+
-- | 5 | Jeames |
33+
-- +---------+---------+
34+
35+
-- Solution
36+
select row_number() over (order by (if(id%2=1,id+1,id-1))) as id, student
37+
from seat

Medium/NPV Queries.sql

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
-- Question 54
2+
-- Table: NPV
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | id | int |
8+
-- | year | int |
9+
-- | npv | int |
10+
-- +---------------+---------+
11+
-- (id, year) is the primary key of this table.
12+
-- The table has information about the id and the year of each inventory and the corresponding net present value.
13+
14+
15+
-- Table: Queries
16+
17+
-- +---------------+---------+
18+
-- | Column Name | Type |
19+
-- +---------------+---------+
20+
-- | id | int |
21+
-- | year | int |
22+
-- +---------------+---------+
23+
-- (id, year) is the primary key of this table.
24+
-- The table has information about the id and the year of each inventory query.
25+
26+
27+
-- Write an SQL query to find the npv of all each query of queries table.
28+
29+
-- Return the result table in any order.
30+
31+
-- The query result format is in the following example:
32+
33+
-- NPV table:
34+
-- +------+--------+--------+
35+
-- | id | year | npv |
36+
-- +------+--------+--------+
37+
-- | 1 | 2018 | 100 |
38+
-- | 7 | 2020 | 30 |
39+
-- | 13 | 2019 | 40 |
40+
-- | 1 | 2019 | 113 |
41+
-- | 2 | 2008 | 121 |
42+
-- | 3 | 2009 | 12 |
43+
-- | 11 | 2020 | 99 |
44+
-- | 7 | 2019 | 0 |
45+
-- +------+--------+--------+
46+
47+
-- Queries table:
48+
-- +------+--------+
49+
-- | id | year |
50+
-- +------+--------+
51+
-- | 1 | 2019 |
52+
-- | 2 | 2008 |
53+
-- | 3 | 2009 |
54+
-- | 7 | 2018 |
55+
-- | 7 | 2019 |
56+
-- | 7 | 2020 |
57+
-- | 13 | 2019 |
58+
-- +------+--------+
59+
60+
-- Result table:
61+
-- +------+--------+--------+
62+
-- | id | year | npv |
63+
-- +------+--------+--------+
64+
-- | 1 | 2019 | 113 |
65+
-- | 2 | 2008 | 121 |
66+
-- | 3 | 2009 | 12 |
67+
-- | 7 | 2018 | 0 |
68+
-- | 7 | 2019 | 0 |
69+
-- | 7 | 2020 | 30 |
70+
-- | 13 | 2019 | 40 |
71+
-- +------+--------+--------+
72+
73+
-- The npv value of (7, 2018) is not present in the NPV table, we consider it 0.
74+
-- The npv values of all other queries can be found in the NPV table.
75+
76+
-- Solution
77+
select q.id, q.year, coalesce(n.npv,0) as npv
78+
from queries q
79+
left join npv n
80+
on q.id = n.id and q.year=n.year

Medium/Tree Node.sql

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
-- Question 58
2+
-- Given a table tree, id is identifier of the tree node and p_id is its parent node's id.
3+
4+
-- +----+------+
5+
-- | id | p_id |
6+
-- +----+------+
7+
-- | 1 | null |
8+
-- | 2 | 1 |
9+
-- | 3 | 1 |
10+
-- | 4 | 2 |
11+
-- | 5 | 2 |
12+
-- +----+------+
13+
-- Each node in the tree can be one of three types:
14+
-- Leaf: if the node is a leaf node.
15+
-- Root: if the node is the root of the tree.
16+
-- Inner: If the node is neither a leaf node nor a root node.
17+
18+
19+
-- Write a query to print the node id and the type of the node. Sort your output by the node id. The result for the above sample is:
20+
21+
22+
-- +----+------+
23+
-- | id | Type |
24+
-- +----+------+
25+
-- | 1 | Root |
26+
-- | 2 | Inner|
27+
-- | 3 | Leaf |
28+
-- | 4 | Leaf |
29+
-- | 5 | Leaf |
30+
-- +----+------+
31+
32+
33+
-- Explanation
34+
35+
36+
37+
-- Node '1' is root node, because its parent node is NULL and it has child node '2' and '3'.
38+
-- Node '2' is inner node, because it has parent node '1' and child node '4' and '5'.
39+
-- Node '3', '4' and '5' is Leaf node, because they have parent node and they don't have child node.
40+
41+
-- And here is the image of the sample tree as below:
42+
43+
44+
-- 1
45+
-- / \
46+
-- 2 3
47+
-- / \
48+
-- 4 5
49+
-- Note
50+
51+
-- If there is only one node on the tree, you only need to output its root attributes.
52+
53+
-- Solution
54+
select id,
55+
case when p_id is null then 'Root'
56+
when id not in (select p_id from tree where p_id is not null group by p_id) then 'Leaf'
57+
else 'Inner'
58+
end as Type
59+
from tree
60+
order by id

0 commit comments

Comments
 (0)