Skip to content

Commit 66f7606

Browse files
authored
Today's 5
1 parent 73513b0 commit 66f7606

File tree

5 files changed

+347
-0
lines changed

5 files changed

+347
-0
lines changed

Medium/Calculate Salaries.sql

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
-- Question 74
2+
-- Table Salaries:
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | company_id | int |
8+
-- | employee_id | int |
9+
-- | employee_name | varchar |
10+
-- | salary | int |
11+
-- +---------------+---------+
12+
-- (company_id, employee_id) is the primary key for this table.
13+
-- This table contains the company id, the id, the name and the salary for an employee.
14+
15+
16+
-- Write an SQL query to find the salaries of the employees after applying taxes.
17+
18+
-- The tax rate is calculated for each company based on the following criteria:
19+
20+
-- 0% If the max salary of any employee in the company is less than 1000$.
21+
-- 24% If the max salary of any employee in the company is in the range [1000, 10000] inclusive.
22+
-- 49% If the max salary of any employee in the company is greater than 10000$.
23+
-- Return the result table in any order. Round the salary to the nearest integer.
24+
25+
-- The query result format is in the following example:
26+
27+
-- Salaries table:
28+
-- +------------+-------------+---------------+--------+
29+
-- | company_id | employee_id | employee_name | salary |
30+
-- +------------+-------------+---------------+--------+
31+
-- | 1 | 1 | Tony | 2000 |
32+
-- | 1 | 2 | Pronub | 21300 |
33+
-- | 1 | 3 | Tyrrox | 10800 |
34+
-- | 2 | 1 | Pam | 300 |
35+
-- | 2 | 7 | Bassem | 450 |
36+
-- | 2 | 9 | Hermione | 700 |
37+
-- | 3 | 7 | Bocaben | 100 |
38+
-- | 3 | 2 | Ognjen | 2200 |
39+
-- | 3 | 13 | Nyancat | 3300 |
40+
-- | 3 | 15 | Morninngcat | 1866 |
41+
-- +------------+-------------+---------------+--------+
42+
43+
-- Result table:
44+
-- +------------+-------------+---------------+--------+
45+
-- | company_id | employee_id | employee_name | salary |
46+
-- +------------+-------------+---------------+--------+
47+
-- | 1 | 1 | Tony | 1020 |
48+
-- | 1 | 2 | Pronub | 10863 |
49+
-- | 1 | 3 | Tyrrox | 5508 |
50+
-- | 2 | 1 | Pam | 300 |
51+
-- | 2 | 7 | Bassem | 450 |
52+
-- | 2 | 9 | Hermione | 700 |
53+
-- | 3 | 7 | Bocaben | 76 |
54+
-- | 3 | 2 | Ognjen | 1672 |
55+
-- | 3 | 13 | Nyancat | 2508 |
56+
-- | 3 | 15 | Morninngcat | 5911 |
57+
-- +------------+-------------+---------------+--------+
58+
-- For company 1, Max salary is 21300. Employees in company 1 have taxes = 49%
59+
-- For company 2, Max salary is 700. Employees in company 2 have taxes = 0%
60+
-- For company 3, Max salary is 7777. Employees in company 3 have taxes = 24%
61+
-- The salary after taxes = salary - (taxes percentage / 100) * salary
62+
-- For example, Salary for Morninngcat (3, 15) after taxes = 7777 - 7777 * (24 / 100) = 7777 - 1866.48 = 5910.52, which is rounded to 5911.
63+
64+
-- Solution
65+
with t1 as (
66+
select company_id, employee_id, employee_name, salary as sa, max(salary) over(partition by company_id) as maximum
67+
from salaries)
68+
69+
select company_id, employee_id, employee_name,
70+
case when t1.maximum<1000 then t1.sa
71+
when t1.maximum between 1000 and 10000 then round(t1.sa*.76,0)
72+
else round(t1.sa*.51,0)
73+
end as salary
74+
from t1
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
-- Question 72
2+
-- Table: Customers
3+
4+
-- +---------------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------------+---------+
7+
-- | customer_id | int |
8+
-- | customer_name | varchar |
9+
-- +---------------------+---------+
10+
-- customer_id is the primary key for this table.
11+
-- customer_name is the name of the customer.
12+
13+
14+
-- Table: Orders
15+
16+
-- +---------------+---------+
17+
-- | Column Name | Type |
18+
-- +---------------+---------+
19+
-- | order_id | int |
20+
-- | customer_id | int |
21+
-- | product_name | varchar |
22+
-- +---------------+---------+
23+
-- order_id is the primary key for this table.
24+
-- customer_id is the id of the customer who bought the product "product_name".
25+
26+
27+
-- Write an SQL query to report the customer_id and customer_name of customers who bought products "A", "B" but did not buy the product "C" since we want to recommend them buy this product.
28+
29+
-- Return the result table ordered by customer_id.
30+
31+
-- The query result format is in the following example.
32+
33+
34+
35+
-- Customers table:
36+
-- +-------------+---------------+
37+
-- | customer_id | customer_name |
38+
-- +-------------+---------------+
39+
-- | 1 | Daniel |
40+
-- | 2 | Diana |
41+
-- | 3 | Elizabeth |
42+
-- | 4 | Jhon |
43+
-- +-------------+---------------+
44+
45+
-- Orders table:
46+
-- +------------+--------------+---------------+
47+
-- | order_id | customer_id | product_name |
48+
-- +------------+--------------+---------------+
49+
-- | 10 | 1 | A |
50+
-- | 20 | 1 | B |
51+
-- | 30 | 1 | D |
52+
-- | 40 | 1 | C |
53+
-- | 50 | 2 | A |
54+
-- | 60 | 3 | A |
55+
-- | 70 | 3 | B |
56+
-- | 80 | 3 | D |
57+
-- | 90 | 4 | C |
58+
-- +------------+--------------+---------------+
59+
60+
-- Result table:
61+
-- +-------------+---------------+
62+
-- | customer_id | customer_name |
63+
-- +-------------+---------------+
64+
-- | 3 | Elizabeth |
65+
-- +-------------+---------------+
66+
-- Only the customer_id with id 3 bought the product A and B but not the product C.
67+
68+
-- Solution
69+
with t1 as
70+
(
71+
select customer_id
72+
from orders
73+
where product_name = 'B' and
74+
customer_id in (select customer_id
75+
from orders
76+
where product_name = 'A'))
77+
78+
Select t1.customer_id, c.customer_name
79+
from t1 join customers c
80+
on t1.customer_id = c.customer_id
81+
where t1.customer_id != all(select customer_id
82+
from orders
83+
where product_name = 'C')
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-- Question 75
2+
-- The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.
3+
4+
-- +------+----------+-----------+----------+
5+
-- |Id |Name |Department |ManagerId |
6+
-- +------+----------+-----------+----------+
7+
-- |101 |John |A |null |
8+
-- |102 |Dan |A |101 |
9+
-- |103 |James |A |101 |
10+
-- |104 |Amy |A |101 |
11+
-- |105 |Anne |A |101 |
12+
-- |106 |Ron |B |101 |
13+
-- +------+----------+-----------+----------+
14+
-- Given the Employee table, write a SQL query that finds out managers with at least 5 direct report. For the above table, your SQL query should return:
15+
16+
-- +-------+
17+
-- | Name |
18+
-- +-------+
19+
-- | John |
20+
-- +-------+
21+
-- Note:
22+
-- No one would report to himself.
23+
24+
-- Solution
25+
with t1 as
26+
(
27+
select managerid, count(name) as total
28+
from employee
29+
group by managerid
30+
)
31+
32+
select e.name
33+
from t1
34+
join employee e
35+
on t1.managerid = e.id
36+
where t1.total>=5

Medium/Reported Posts 2.sql

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
-- Question 73
2+
-- Table: Actions
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | user_id | int |
8+
-- | post_id | int |
9+
-- | action_date | date |
10+
-- | action | enum |
11+
-- | extra | varchar |
12+
-- +---------------+---------+
13+
-- There is no primary key for this table, it may have duplicate rows.
14+
-- The action column is an ENUM type of ('view', 'like', 'reaction', 'comment', 'report', 'share').
15+
-- The extra column has optional information about the action such as a reason for report or a type of reaction.
16+
-- Table: Removals
17+
18+
-- +---------------+---------+
19+
-- | Column Name | Type |
20+
-- +---------------+---------+
21+
-- | post_id | int |
22+
-- | remove_date | date |
23+
-- +---------------+---------+
24+
-- post_id is the primary key of this table.
25+
-- Each row in this table indicates that some post was removed as a result of being reported or as a result of an admin review.
26+
27+
28+
-- Write an SQL query to find the average for daily percentage of posts that got removed after being reported as spam, rounded to 2 decimal places.
29+
30+
-- The query result format is in the following example:
31+
32+
-- Actions table:
33+
-- +---------+---------+-------------+--------+--------+
34+
-- | user_id | post_id | action_date | action | extra |
35+
-- +---------+---------+-------------+--------+--------+
36+
-- | 1 | 1 | 2019-07-01 | view | null |
37+
-- | 1 | 1 | 2019-07-01 | like | null |
38+
-- | 1 | 1 | 2019-07-01 | share | null |
39+
-- | 2 | 2 | 2019-07-04 | view | null |
40+
-- | 2 | 2 | 2019-07-04 | report | spam |
41+
-- | 3 | 4 | 2019-07-04 | view | null |
42+
-- | 3 | 4 | 2019-07-04 | report | spam |
43+
-- | 4 | 3 | 2019-07-02 | view | null |
44+
-- | 4 | 3 | 2019-07-02 | report | spam |
45+
-- | 5 | 2 | 2019-07-03 | view | null |
46+
-- | 5 | 2 | 2019-07-03 | report | racism |
47+
-- | 5 | 5 | 2019-07-03 | view | null |
48+
-- | 5 | 5 | 2019-07-03 | report | racism |
49+
-- +---------+---------+-------------+--------+--------+
50+
51+
-- Removals table:
52+
-- +---------+-------------+
53+
-- | post_id | remove_date |
54+
-- +---------+-------------+
55+
-- | 2 | 2019-07-20 |
56+
-- | 3 | 2019-07-18 |
57+
-- +---------+-------------+
58+
59+
-- Result table:
60+
-- +-----------------------+
61+
-- | average_daily_percent |
62+
-- +-----------------------+
63+
-- | 75.00 |
64+
-- +-----------------------+
65+
-- The percentage for 2019-07-04 is 50% because only one post of two spam reported posts was removed.
66+
-- The percentage for 2019-07-02 is 100% because one post was reported as spam and it was removed.
67+
-- The other days had no spam reports so the average is (50 + 100) / 2 = 75%
68+
-- Note that the output is only one number and that we do not care about the remove dates.
69+
70+
-- Solution
71+
with t1 as(
72+
select a.action_date, (count(distinct r.post_id)+0.0)/(count(distinct a.post_id)+0.0) as result
73+
from (select action_date, post_id
74+
from actions
75+
where extra = 'spam' and action = 'report') a
76+
left join
77+
removals r
78+
on a.post_id = r.post_id
79+
group by a.action_date)
80+
81+
select round(avg(t1.result)*100,2) as average_daily_percent
82+
from t1

Medium/Restaurant growth.sql

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
-- Question 71
2+
-- Table: Customer
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | customer_id | int |
8+
-- | name | varchar |
9+
-- | visited_on | date |
10+
-- | amount | int |
11+
-- +---------------+---------+
12+
-- (customer_id, visited_on) is the primary key for this table.
13+
-- This table contains data about customer transactions in a restaurant.
14+
-- visited_on is the date on which the customer with ID (customer_id) have visited the restaurant.
15+
-- amount is the total paid by a customer.
16+
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+
-- Write an SQL query to compute moving average of how much customer paid in a 7 days window (current day + 6 days before) .
21+
22+
-- The query result format is in the following example:
23+
24+
-- Return result table ordered by visited_on.
25+
26+
-- average_amount should be rounded to 2 decimal places, all dates are in the format ('YYYY-MM-DD').
27+
28+
29+
30+
-- Customer table:
31+
-- +-------------+--------------+--------------+-------------+
32+
-- | customer_id | name | visited_on | amount |
33+
-- +-------------+--------------+--------------+-------------+
34+
-- | 1 | Jhon | 2019-01-01 | 100 |
35+
-- | 2 | Daniel | 2019-01-02 | 110 |
36+
-- | 3 | Jade | 2019-01-03 | 120 |
37+
-- | 4 | Khaled | 2019-01-04 | 130 |
38+
-- | 5 | Winston | 2019-01-05 | 110 |
39+
-- | 6 | Elvis | 2019-01-06 | 140 |
40+
-- | 7 | Anna | 2019-01-07 | 150 |
41+
-- | 8 | Maria | 2019-01-08 | 80 |
42+
-- | 9 | Jaze | 2019-01-09 | 110 |
43+
-- | 1 | Jhon | 2019-01-10 | 130 |
44+
-- | 3 | Jade | 2019-01-10 | 150 |
45+
-- +-------------+--------------+--------------+-------------+
46+
47+
-- Result table:
48+
-- +--------------+--------------+----------------+
49+
-- | visited_on | amount | average_amount |
50+
-- +--------------+--------------+----------------+
51+
-- | 2019-01-07 | 860 | 122.86 |
52+
-- | 2019-01-08 | 840 | 120 |
53+
-- | 2019-01-09 | 840 | 120 |
54+
-- | 2019-01-10 | 1000 | 142.86 |
55+
-- +--------------+--------------+----------------+
56+
57+
-- 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
58+
-- 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
59+
-- 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
60+
-- 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
61+
62+
-- Solution
63+
select visited_on, sum(amount) over(order by visited_on rows 6 preceding),
64+
round(avg(amount) over(order by visited_on rows 6 preceding),2)
65+
from
66+
(
67+
select visited_on, sum(amount) as amount
68+
from customer
69+
group by visited_on
70+
order by visited_on
71+
) a
72+
order by visited_on offset 6 rows

0 commit comments

Comments
 (0)