Skip to content

Commit efec05e

Browse files
authored
Attempted 8 today
1 parent 6acf05f commit efec05e

8 files changed

+528
-0
lines changed

Easy/Article views.sql

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
-- Question 42
2+
-- Table: Views
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | article_id | int |
8+
-- | author_id | int |
9+
-- | viewer_id | int |
10+
-- | view_date | date |
11+
-- +---------------+---------+
12+
-- There is no primary key for this table, it may have duplicate rows.
13+
-- Each row of this table indicates that some viewer viewed an article (written by some author) on some date.
14+
-- Note that equal author_id and viewer_id indicate the same person.
15+
16+
17+
-- Write an SQL query to find all the authors that viewed at least one of their own articles, sorted in ascending order by their id.
18+
19+
-- The query result format is in the following example:
20+
21+
-- Views table:
22+
-- +------------+-----------+-----------+------------+
23+
-- | article_id | author_id | viewer_id | view_date |
24+
-- +------------+-----------+-----------+------------+
25+
-- | 1 | 3 | 5 | 2019-08-01 |
26+
-- | 1 | 3 | 6 | 2019-08-02 |
27+
-- | 2 | 7 | 7 | 2019-08-01 |
28+
-- | 2 | 7 | 6 | 2019-08-02 |
29+
-- | 4 | 7 | 1 | 2019-07-22 |
30+
-- | 3 | 4 | 4 | 2019-07-21 |
31+
-- | 3 | 4 | 4 | 2019-07-21 |
32+
-- +------------+-----------+-----------+------------+
33+
34+
-- Result table:
35+
-- +------+
36+
-- | id |
37+
-- +------+
38+
-- | 4 |
39+
-- | 7 |
40+
-- +------+
41+
42+
-- Solution
43+
select distinct author_id as id
44+
from views
45+
where author_id = viewer_id
46+
order by author_id

Easy/Find the team size.sql

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
-- Question 47
2+
-- Table: Employee
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | employee_id | int |
8+
-- | team_id | int |
9+
-- +---------------+---------+
10+
-- employee_id is the primary key for this table.
11+
-- Each row of this table contains the ID of each employee and their respective team.
12+
-- Write an SQL query to find the team size of each of the employees.
13+
14+
-- Return result table in any order.
15+
16+
-- The query result format is in the following example:
17+
18+
-- Employee Table:
19+
-- +-------------+------------+
20+
-- | employee_id | team_id |
21+
-- +-------------+------------+
22+
-- | 1 | 8 |
23+
-- | 2 | 8 |
24+
-- | 3 | 8 |
25+
-- | 4 | 7 |
26+
-- | 5 | 9 |
27+
-- | 6 | 9 |
28+
-- +-------------+------------+
29+
-- Result table:
30+
-- +-------------+------------+
31+
-- | employee_id | team_size |
32+
-- +-------------+------------+
33+
-- | 1 | 3 |
34+
-- | 2 | 3 |
35+
-- | 3 | 3 |
36+
-- | 4 | 1 |
37+
-- | 5 | 2 |
38+
-- | 6 | 2 |
39+
-- +-------------+------------+
40+
-- Employees with Id 1,2,3 are part of a team with team_id = 8.
41+
-- Employees with Id 4 is part of a team with team_id = 7.
42+
-- Employees with Id 5,6 are part of a team with team_id = 9.
43+
44+
-- Solution
45+
Select employee_id, b.team_size
46+
from employee e
47+
join
48+
(
49+
Select team_id, count(team_id) as team_size
50+
from employee
51+
group by team_id) b
52+
on e.team_id = b.team_id
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
-- Question 45
2+
-- Table: Products
3+
4+
-- +------------------+---------+
5+
-- | Column Name | Type |
6+
-- +------------------+---------+
7+
-- | product_id | int |
8+
-- | product_name | varchar |
9+
-- | product_category | varchar |
10+
-- +------------------+---------+
11+
-- product_id is the primary key for this table.
12+
-- This table contains data about the company's products.
13+
-- Table: Orders
14+
15+
-- +---------------+---------+
16+
-- | Column Name | Type |
17+
-- +---------------+---------+
18+
-- | product_id | int |
19+
-- | order_date | date |
20+
-- | unit | int |
21+
-- +---------------+---------+
22+
-- There is no primary key for this table. It may have duplicate rows.
23+
-- product_id is a foreign key to Products table.
24+
-- unit is the number of products ordered in order_date.
25+
26+
27+
-- Write an SQL query to get the names of products with greater than or equal to 100 units ordered in February 2020 and their amount.
28+
29+
-- Return result table in any order.
30+
31+
-- The query result format is in the following example:
32+
33+
34+
35+
-- Products table:
36+
-- +-------------+-----------------------+------------------+
37+
-- | product_id | product_name | product_category |
38+
-- +-------------+-----------------------+------------------+
39+
-- | 1 | Leetcode Solutions | Book |
40+
-- | 2 | Jewels of Stringology | Book |
41+
-- | 3 | HP | Laptop |
42+
-- | 4 | Lenovo | Laptop |
43+
-- | 5 | Leetcode Kit | T-shirt |
44+
-- +-------------+-----------------------+------------------+
45+
46+
-- Orders table:
47+
-- +--------------+--------------+----------+
48+
-- | product_id | order_date | unit |
49+
-- +--------------+--------------+----------+
50+
-- | 1 | 2020-02-05 | 60 |
51+
-- | 1 | 2020-02-10 | 70 |
52+
-- | 2 | 2020-01-18 | 30 |
53+
-- | 2 | 2020-02-11 | 80 |
54+
-- | 3 | 2020-02-17 | 2 |
55+
-- | 3 | 2020-02-24 | 3 |
56+
-- | 4 | 2020-03-01 | 20 |
57+
-- | 4 | 2020-03-04 | 30 |
58+
-- | 4 | 2020-03-04 | 60 |
59+
-- | 5 | 2020-02-25 | 50 |
60+
-- | 5 | 2020-02-27 | 50 |
61+
-- | 5 | 2020-03-01 | 50 |
62+
-- +--------------+--------------+----------+
63+
64+
-- Result table:
65+
-- +--------------------+---------+
66+
-- | product_name | unit |
67+
-- +--------------------+---------+
68+
-- | Leetcode Solutions | 130 |
69+
-- | Leetcode Kit | 100 |
70+
-- +--------------------+---------+
71+
72+
-- Products with product_id = 1 is ordered in February a total of (60 + 70) = 130.
73+
-- Products with product_id = 2 is ordered in February a total of 80.
74+
-- Products with product_id = 3 is ordered in February a total of (2 + 3) = 5.
75+
-- Products with product_id = 4 was not ordered in February 2020.
76+
-- Products with product_id = 5 is ordered in February a total of (50 + 50) = 100.
77+
78+
-- Solution
79+
Select a.product_name, a.unit
80+
from
81+
(select p.product_name, sum(unit) as unit
82+
from orders o
83+
join products p
84+
on o.product_id = p.product_id
85+
where month(order_date)=2 and year(order_date) = 2020
86+
group by o.product_id) a
87+
where a.unit>=100
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
-- Question 41
2+
-- Table: Queries
3+
4+
-- +-------------+---------+
5+
-- | Column Name | Type |
6+
-- +-------------+---------+
7+
-- | query_name | varchar |
8+
-- | result | varchar |
9+
-- | position | int |
10+
-- | rating | int |
11+
-- +-------------+---------+
12+
-- There is no primary key for this table, it may have duplicate rows.
13+
-- This table contains information collected from some queries on a database.
14+
-- The position column has a value from 1 to 500.
15+
-- The rating column has a value from 1 to 5. Query with rating less than 3 is a poor query.
16+
17+
18+
-- We define query quality as:
19+
20+
-- The average of the ratio between query rating and its position.
21+
22+
-- We also define poor query percentage as:
23+
24+
-- The percentage of all queries with rating less than 3.
25+
26+
-- Write an SQL query to find each query_name, the quality and poor_query_percentage.
27+
28+
-- Both quality and poor_query_percentage should be rounded to 2 decimal places.
29+
30+
-- The query result format is in the following example:
31+
32+
-- Queries table:
33+
-- +------------+-------------------+----------+--------+
34+
-- | query_name | result | position | rating |
35+
-- +------------+-------------------+----------+--------+
36+
-- | Dog | Golden Retriever | 1 | 5 |
37+
-- | Dog | German Shepherd | 2 | 5 |
38+
-- | Dog | Mule | 200 | 1 |
39+
-- | Cat | Shirazi | 5 | 2 |
40+
-- | Cat | Siamese | 3 | 3 |
41+
-- | Cat | Sphynx | 7 | 4 |
42+
-- +------------+-------------------+----------+--------+
43+
44+
-- Result table:
45+
-- +------------+---------+-----------------------+
46+
-- | query_name | quality | poor_query_percentage |
47+
-- +------------+---------+-----------------------+
48+
-- | Dog | 2.50 | 33.33 |
49+
-- | Cat | 0.66 | 33.33 |
50+
-- +------------+---------+-----------------------+
51+
52+
-- Dog queries quality is ((5 / 1) + (5 / 2) + (1 / 200)) / 3 = 2.50
53+
-- Dog queries poor_ query_percentage is (1 / 3) * 100 = 33.33
54+
55+
-- Cat queries quality equals ((2 / 5) + (3 / 3) + (4 / 7)) / 3 = 0.66
56+
-- Cat queries poor_ query_percentage is (1 / 3) * 100 = 33.33
57+
58+
-- Solution
59+
Select query_name, round(sum(rating/position)/count(*),2) as quality,
60+
round(avg(case when rating<3 then 1 else 0 end)*100,2) as poor_query_percentage
61+
from queries
62+
group by query_name

Easy/Reformat department table.sql

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
-- Question 44
2+
-- Table: Department
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | id | int |
8+
-- | revenue | int |
9+
-- | month | varchar |
10+
-- +---------------+---------+
11+
-- (id, month) is the primary key of this table.
12+
-- The table has information about the revenue of each department per month.
13+
-- The month has values in ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"].
14+
15+
16+
-- Write an SQL query to reformat the table such that there is a department id column and a revenue column for each month.
17+
18+
-- The query result format is in the following example:
19+
20+
-- Department table:
21+
-- +------+---------+-------+
22+
-- | id | revenue | month |
23+
-- +------+---------+-------+
24+
-- | 1 | 8000 | Jan |
25+
-- | 2 | 9000 | Jan |
26+
-- | 3 | 10000 | Feb |
27+
-- | 1 | 7000 | Feb |
28+
-- | 1 | 6000 | Mar |
29+
-- +------+---------+-------+
30+
31+
-- Result table:
32+
-- +------+-------------+-------------+-------------+-----+-------------+
33+
-- | id | Jan_Revenue | Feb_Revenue | Mar_Revenue | ... | Dec_Revenue |
34+
-- +------+-------------+-------------+-------------+-----+-------------+
35+
-- | 1 | 8000 | 7000 | 6000 | ... | null |
36+
-- | 2 | 9000 | null | null | ... | null |
37+
-- | 3 | null | 10000 | null | ... | null |
38+
-- +------+-------------+-------------+-------------+-----+-------------+
39+
40+
-- Note that the result table has 13 columns (1 for the department id + 12 for the months).
41+
42+
-- Solution
43+
select id,
44+
sum(if(month='Jan',revenue,null)) as Jan_Revenue,
45+
sum(if(month='Feb',revenue,null)) as Feb_Revenue,
46+
sum(if(month='Mar',revenue,null)) as Mar_Revenue,
47+
sum(if(month='Apr',revenue,null)) as Apr_Revenue,
48+
sum(if(month='May',revenue,null)) as May_Revenue,
49+
sum(if(month='Jun',revenue,null)) as Jun_Revenue,
50+
sum(if(month='Jul',revenue,null)) as Jul_Revenue,
51+
sum(if(month='Aug',revenue,null)) as Aug_Revenue,
52+
sum(if(month='Sep',revenue,null)) as Sep_Revenue,
53+
sum(if(month='Oct',revenue,null)) as Oct_Revenue,
54+
sum(if(month='Nov',revenue,null)) as Nov_Revenue,
55+
sum(if(month='Dec',revenue,null)) as Dec_Revenue
56+
from Department
57+
group by id
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
-- Question 48
2+
-- Table: Employees
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | id | int |
8+
-- | name | varchar |
9+
-- +---------------+---------+
10+
-- id is the primary key for this table.
11+
-- Each row of this table contains the id and the name of an employee in a company.
12+
13+
14+
-- Table: EmployeeUNI
15+
16+
-- +---------------+---------+
17+
-- | Column Name | Type |
18+
-- +---------------+---------+
19+
-- | id | int |
20+
-- | unique_id | int |
21+
-- +---------------+---------+
22+
-- (id, unique_id) is the primary key for this table.
23+
-- Each row of this table contains the id and the corresponding unique id of an employee in the company.
24+
25+
26+
-- Write an SQL query to show the unique ID of each user, If a user doesn't have a unique ID replace just show null.
27+
28+
-- Return the result table in any order.
29+
30+
-- The query result format is in the following example:
31+
32+
-- Employees table:
33+
-- +----+----------+
34+
-- | id | name |
35+
-- +----+----------+
36+
-- | 1 | Alice |
37+
-- | 7 | Bob |
38+
-- | 11 | Meir |
39+
-- | 90 | Winston |
40+
-- | 3 | Jonathan |
41+
-- +----+----------+
42+
43+
-- EmployeeUNI table:
44+
-- +----+-----------+
45+
-- | id | unique_id |
46+
-- +----+-----------+
47+
-- | 3 | 1 |
48+
-- | 11 | 2 |
49+
-- | 90 | 3 |
50+
-- +----+-----------+
51+
52+
-- EmployeeUNI table:
53+
-- +-----------+----------+
54+
-- | unique_id | name |
55+
-- +-----------+----------+
56+
-- | null | Alice |
57+
-- | null | Bob |
58+
-- | 2 | Meir |
59+
-- | 3 | Winston |
60+
-- | 1 | Jonathan |
61+
-- +-----------+----------+
62+
63+
-- Alice and Bob don't have a unique ID, We will show null instead.
64+
-- The unique ID of Meir is 2.
65+
-- The unique ID of Winston is 3.
66+
-- The unique ID of Jonathan is 1.
67+
68+
-- Solution
69+
select unique_id, name
70+
from employees e
71+
left join
72+
employeeuni u
73+
on e.id = u.id
74+
order by e.id

0 commit comments

Comments
 (0)