Skip to content

Commit 981e304

Browse files
authored
Attempted 6 today
1 parent 55a1ce1 commit 981e304

6 files changed

+390
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
-- Question 31
2+
-- Table: Submissions
3+
4+
-- +---------------+----------+
5+
-- | Column Name | Type |
6+
-- +---------------+----------+
7+
-- | sub_id | int |
8+
-- | parent_id | int |
9+
-- +---------------+----------+
10+
-- There is no primary key for this table, it may have duplicate rows.
11+
-- Each row can be a post or comment on the post.
12+
-- parent_id is null for posts.
13+
-- parent_id for comments is sub_id for another post in the table.
14+
15+
16+
-- Write an SQL query to find number of comments per each post.
17+
18+
-- Result table should contain post_id and its corresponding number_of_comments,
19+
-- and must be sorted by post_id in ascending order.
20+
21+
-- Submissions may contain duplicate comments. You should count the number of unique comments per post.
22+
23+
-- Submissions may contain duplicate posts. You should treat them as one post.
24+
25+
-- The query result format is in the following example:
26+
27+
-- Submissions table:
28+
-- +---------+------------+
29+
-- | sub_id | parent_id |
30+
-- +---------+------------+
31+
-- | 1 | Null |
32+
-- | 2 | Null |
33+
-- | 1 | Null |
34+
-- | 12 | Null |
35+
-- | 3 | 1 |
36+
-- | 5 | 2 |
37+
-- | 3 | 1 |
38+
-- | 4 | 1 |
39+
-- | 9 | 1 |
40+
-- | 10 | 2 |
41+
-- | 6 | 7 |
42+
-- +---------+------------+
43+
44+
-- Result table:
45+
-- +---------+--------------------+
46+
-- | post_id | number_of_comments |
47+
-- +---------+--------------------+
48+
-- | 1 | 3 |
49+
-- | 2 | 2 |
50+
-- | 12 | 0 |
51+
-- +---------+--------------------+
52+
53+
-- The post with id 1 has three comments in the table with id 3, 4 and 9. The comment with id 3 is
54+
-- repeated in the table, we counted it only once.
55+
-- The post with id 2 has two comments in the table with id 5 and 10.
56+
-- The post with id 12 has no comments in the table.
57+
-- The comment with id 6 is a comment on a deleted post with id 7 so we ignored it.
58+
59+
-- Solution
60+
Select a.sub_id as post_id, coalesce(b.number_of_comments,0) as number_of_comments
61+
from(
62+
select distinct sub_id from submissions where parent_id is null) a
63+
left join(
64+
select parent_id, count(distinct(sub_id)) as number_of_comments
65+
from submissions
66+
group by parent_id
67+
having parent_id = any(select sub_id from submissions where parent_id is null)) b
68+
on a.sub_id = b.parent_id
69+
order by post_id

Easy/Product Sales Analysis 1.sql

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
-- Question 30
2+
-- Table: Sales
3+
4+
-- +-------------+-------+
5+
-- | Column Name | Type |
6+
-- +-------------+-------+
7+
-- | sale_id | int |
8+
-- | product_id | int |
9+
-- | year | int |
10+
-- | quantity | int |
11+
-- | price | int |
12+
-- +-------------+-------+
13+
-- (sale_id, year) is the primary key of this table.
14+
-- product_id is a foreign key to Product table.
15+
-- Note that the price is per unit.
16+
-- Table: Product
17+
18+
-- +--------------+---------+
19+
-- | Column Name | Type |
20+
-- +--------------+---------+
21+
-- | product_id | int |
22+
-- | product_name | varchar |
23+
-- +--------------+---------+
24+
-- product_id is the primary key of this table.
25+
26+
27+
-- Write an SQL query that reports all product names of the products in the Sales table along with their selling year and price.
28+
29+
-- For example:
30+
31+
-- Sales table:
32+
-- +---------+------------+------+----------+-------+
33+
-- | sale_id | product_id | year | quantity | price |
34+
-- +---------+------------+------+----------+-------+
35+
-- | 1 | 100 | 2008 | 10 | 5000 |
36+
-- | 2 | 100 | 2009 | 12 | 5000 |
37+
-- | 7 | 200 | 2011 | 15 | 9000 |
38+
-- +---------+------------+------+----------+-------+
39+
40+
-- Product table:
41+
-- +------------+--------------+
42+
-- | product_id | product_name |
43+
-- +------------+--------------+
44+
-- | 100 | Nokia |
45+
-- | 200 | Apple |
46+
-- | 300 | Samsung |
47+
-- +------------+--------------+
48+
49+
-- Result table:
50+
-- +--------------+-------+-------+
51+
-- | product_name | year | price |
52+
-- +--------------+-------+-------+
53+
-- | Nokia | 2008 | 5000 |
54+
-- | Nokia | 2009 | 5000 |
55+
-- | Apple | 2011 | 9000 |
56+
-- +--------------+-------+-------+
57+
58+
-- Solution
59+
Select a.product_name, b.year, b.price
60+
from product as a
61+
join
62+
sales as b
63+
on a.product_id = b.product_id

Easy/Product Sales Analysis 2.sql

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
-- Question 29
2+
-- Table: Sales
3+
4+
-- +-------------+-------+
5+
-- | Column Name | Type |
6+
-- +-------------+-------+
7+
-- | sale_id | int |
8+
-- | product_id | int |
9+
-- | year | int |
10+
-- | quantity | int |
11+
-- | price | int |
12+
-- +-------------+-------+
13+
-- sale_id is the primary key of this table.
14+
-- product_id is a foreign key to Product table.
15+
-- Note that the price is per unit.
16+
-- Table: Product
17+
18+
-- +--------------+---------+
19+
-- | Column Name | Type |
20+
-- +--------------+---------+
21+
-- | product_id | int |
22+
-- | product_name | varchar |
23+
-- +--------------+---------+
24+
-- product_id is the primary key of this table.
25+
26+
27+
-- Write an SQL query that reports the total quantity sold for every product id.
28+
29+
-- The query result format is in the following example:
30+
31+
-- Sales table:
32+
-- +---------+------------+------+----------+-------+
33+
-- | sale_id | product_id | year | quantity | price |
34+
-- +---------+------------+------+----------+-------+
35+
-- | 1 | 100 | 2008 | 10 | 5000 |
36+
-- | 2 | 100 | 2009 | 12 | 5000 |
37+
-- | 7 | 200 | 2011 | 15 | 9000 |
38+
-- +---------+------------+------+----------+-------+
39+
40+
-- Product table:
41+
-- +------------+--------------+
42+
-- | product_id | product_name |
43+
-- +------------+--------------+
44+
-- | 100 | Nokia |
45+
-- | 200 | Apple |
46+
-- | 300 | Samsung |
47+
-- +------------+--------------+
48+
49+
-- Result table:
50+
-- +--------------+----------------+
51+
-- | product_id | total_quantity |
52+
-- +--------------+----------------+
53+
-- | 100 | 22 |
54+
-- | 200 | 15 |
55+
-- +--------------+----------------+
56+
57+
-- Solution
58+
Select a.product_id, sum(a.quantity) as total_quantity
59+
from sales a
60+
join
61+
product b
62+
on a.product_id = b.product_id
63+
group by a.product_id

Easy/Project Employees 1.sql

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
-- Question 26
2+
-- Table: Project
3+
4+
-- +-------------+---------+
5+
-- | Column Name | Type |
6+
-- +-------------+---------+
7+
-- | project_id | int |
8+
-- | employee_id | int |
9+
-- +-------------+---------+
10+
-- (project_id, employee_id) is the primary key of this table.
11+
-- employee_id is a foreign key to Employee table.
12+
-- Table: Employee
13+
14+
-- +------------------+---------+
15+
-- | Column Name | Type |
16+
-- +------------------+---------+
17+
-- | employee_id | int |
18+
-- | name | varchar |
19+
-- | experience_years | int |
20+
-- +------------------+---------+
21+
-- employee_id is the primary key of this table.
22+
23+
24+
-- Write an SQL query that reports the average experience years of all the employees for each project, rounded to 2 digits.
25+
26+
-- The query result format is in the following example:
27+
28+
-- Project table:
29+
-- +-------------+-------------+
30+
-- | project_id | employee_id |
31+
-- +-------------+-------------+
32+
-- | 1 | 1 |
33+
-- | 1 | 2 |
34+
-- | 1 | 3 |
35+
-- | 2 | 1 |
36+
-- | 2 | 4 |
37+
-- +-------------+-------------+
38+
39+
-- Employee table:
40+
-- +-------------+--------+------------------+
41+
-- | employee_id | name | experience_years |
42+
-- +-------------+--------+------------------+
43+
-- | 1 | Khaled | 3 |
44+
-- | 2 | Ali | 2 |
45+
-- | 3 | John | 1 |
46+
-- | 4 | Doe | 2 |
47+
-- +-------------+--------+------------------+
48+
49+
-- Result table:
50+
-- +-------------+---------------+
51+
-- | project_id | average_years |
52+
-- +-------------+---------------+
53+
-- | 1 | 2.00 |
54+
-- | 2 | 2.50 |
55+
-- +-------------+---------------+
56+
-- The average experience years for the first project is (3 + 2 + 1) / 3 = 2.00 and for the second project is (3 + 2) / 2 = 2.50
57+
58+
-- Solution
59+
Select a.project_id, round(sum(b.experience_years)/count(b.employee_id),2) as average_years
60+
from project as a
61+
join
62+
employee as b
63+
on a.employee_id=b.employee_id
64+
group by a.project_id

Easy/Project Employees 2.sql

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
-- Question 28
2+
-- Table: Project
3+
4+
-- +-------------+---------+
5+
-- | Column Name | Type |
6+
-- +-------------+---------+
7+
-- | project_id | int |
8+
-- | employee_id | int |
9+
-- +-------------+---------+
10+
-- (project_id, employee_id) is the primary key of this table.
11+
-- employee_id is a foreign key to Employee table.
12+
-- Table: Employee
13+
14+
-- +------------------+---------+
15+
-- | Column Name | Type |
16+
-- +------------------+---------+
17+
-- | employee_id | int |
18+
-- | name | varchar |
19+
-- | experience_years | int |
20+
-- +------------------+---------+
21+
-- employee_id is the primary key of this table.
22+
23+
24+
-- Write an SQL query that reports all the projects that have the most employees.
25+
26+
-- The query result format is in the following example:
27+
28+
-- Project table:
29+
-- +-------------+-------------+
30+
-- | project_id | employee_id |
31+
-- +-------------+-------------+
32+
-- | 1 | 1 |
33+
-- | 1 | 2 |
34+
-- | 1 | 3 |
35+
-- | 2 | 1 |
36+
-- | 2 | 4 |
37+
-- +-------------+-------------+
38+
39+
-- Employee table:
40+
-- +-------------+--------+------------------+
41+
-- | employee_id | name | experience_years |
42+
-- +-------------+--------+------------------+
43+
-- | 1 | Khaled | 3 |
44+
-- | 2 | Ali | 2 |
45+
-- | 3 | John | 1 |
46+
-- | 4 | Doe | 2 |
47+
-- +-------------+--------+------------------+
48+
49+
-- Result table:
50+
-- +-------------+
51+
-- | project_id |
52+
-- +-------------+
53+
-- | 1 |
54+
-- +-------------+
55+
-- The first project has 3 employees while the second one has 2.
56+
57+
-- Solution
58+
select a.project_id
59+
from(
60+
select project_id,
61+
rank() over(order by count(employee_id) desc) as rk
62+
from project
63+
group by project_id) a
64+
where a.rk = 1

0 commit comments

Comments
 (0)