Skip to content

Commit 8ab80c2

Browse files
Added Increasing Sales Revenue
1 parent 9e63971 commit 8ab80c2

File tree

4 files changed

+166
-5
lines changed

4 files changed

+166
-5
lines changed

Increasing Sales Revenue/READme.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Increasing Sales Revenue
2+
![Star Badge](https://img.shields.io/static/v1?label=%F0%9F%8C%9F&message=If%20Useful&style=style=flat&color=BC4E99)
3+
[![View Main Folder](https://img.shields.io/badge/View-Main_Folder-971901?)](https://github.com/thecoddiwompler/SQL-Practice-Questions/tree/main)
4+
[![View Repositories](https://img.shields.io/badge/View-My_Repositories-blue?logo=GitHub)](https://github.com/thecoddiwompler?tab=repositories)
5+
[![View My Profile](https://img.shields.io/badge/View-My_Profile-green?logo=GitHub)](https://github.com/thecoddiwompler)
6+
7+
---
8+
9+
## 🛠️ Problem Statement
10+
11+
<b> This problem has been asked in Deloitte Interview. </b>
12+
13+
<b>Table Name : Products</b>
14+
15+
| Column Name |Type |
16+
| ------------- | ------------- |
17+
| product_id | int |
18+
| product_name | varchar(50) |
19+
| category | varchar(50) |
20+
21+
* <b> product_id is Primary Key of this table. </b> <br>
22+
<br>
23+
24+
<b>Table Name : Sales</b>
25+
26+
| Column Name |Type |
27+
| ------------- | ------------- |
28+
| product_id | int |
29+
| year | int |
30+
| total_sales_revenue | DECIMAL(10, 2) |
31+
32+
* <b> product_id, year is the Primary Key of this table. </b>
33+
* product_id is the Foreign key to Products table.
34+
<br><br>
35+
36+
Write a sql query to find the products whose total sales revenue has increased every year. Include the product_id , product_name and category in the result.
37+
Sort the result by product_id.
38+
39+
</br>
40+
<details>
41+
<summary>
42+
Input
43+
</summary>
44+
</br>
45+
46+
<b> Table Name: Products </b></br>
47+
48+
| product_id | product_name | category |
49+
|------------|--------------|-------------------|
50+
| 1 | Laptops | Electronics |
51+
| 2 | Jeans | Clothing |
52+
| 3 | Chairs | Home Appliances |
53+
54+
<b> Table Name: Sales </b></br>
55+
56+
| product_id | year | total_sales_revenue |
57+
|------------|------|---------------------|
58+
| 1 | 2019 | 1000.00 |
59+
| 1 | 2020 | 1200.00 |
60+
| 1 | 2021 | 1100.00 |
61+
| 2 | 2019 | 500.00 |
62+
| 2 | 2020 | 600.00 |
63+
| 2 | 2021 | 900.00 |
64+
| 3 | 2019 | 300.00 |
65+
| 3 | 2020 | 450.00 |
66+
| 3 | 2021 | 400.00 |
67+
68+
69+
</details>
70+
71+
<details>
72+
<summary>
73+
Output
74+
</summary>
75+
</br>
76+
77+
| product_id | product_name | category |
78+
|------------|--------------|-------------------|
79+
| 2 | Jeans | Clothing |
80+
81+
</details>
82+
83+
---

Increasing Sales Revenue/schema.sql

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
CREATE TABLE products (
2+
product_id INT PRIMARY KEY,
3+
product_name VARCHAR(50),
4+
category VARCHAR(50)
5+
);
6+
7+
CREATE TABLE sales (
8+
product_id INT,
9+
year INT,
10+
total_sales_revenue DECIMAL(10, 2),
11+
PRIMARY KEY (product_id, year),
12+
FOREIGN KEY (product_id) REFERENCES products(product_id)
13+
);
14+
15+
INSERT INTO
16+
products (product_id, product_name, category)
17+
VALUES
18+
(1, 'Laptops', 'Electronics'),
19+
(2, 'Jeans', 'Clothing'),
20+
(3, 'Chairs', 'Home Appliances');
21+
22+
COMMIT;
23+
24+
INSERT INTO
25+
sales (product_id, year, total_sales_revenue)
26+
VALUES
27+
(1, 2019, 1000.00),
28+
(1, 2020, 1200.00),
29+
(1, 2021, 1100.00),
30+
(2, 2019, 500.00),
31+
(2, 2020, 600.00),
32+
(2, 2021, 900.00),
33+
(3, 2019, 300.00),
34+
(3, 2020, 450.00),
35+
(3, 2021, 400.00);
36+
37+
COMMIT;

Increasing Sales Revenue/solution.sql

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
WITH cte AS (
2+
SELECT
3+
product_id,
4+
RANK() OVER(
5+
PARTITION BY product_id
6+
ORDER BY
7+
year
8+
) year_rank,
9+
RANK() OVER(
10+
PARTITION BY product_id
11+
ORDER BY
12+
total_sales_revenue
13+
) sales_rank
14+
FROM
15+
sales
16+
),
17+
sol AS (
18+
SELECT
19+
product_id,
20+
COUNT(product_id) total_count,
21+
COUNT(product_id) filter(
22+
WHERE
23+
year_rank = sales_rank
24+
) revenue_increase_count
25+
FROM
26+
cte
27+
GROUP BY
28+
product_id
29+
)
30+
SELECT
31+
a.product_id,
32+
b.product_name,
33+
b.category
34+
FROM
35+
sol a
36+
INNER JOIN products b ON a.product_id = b.product_id
37+
WHERE
38+
a.total_count = a.revenue_increase_count
39+
ORDER BY
40+
a.product_id

iqsql.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
2. Employees Check-in Details
33
3. Employees Hiring [Difficult]
44
4. Highest-Grossing Items
5-
5. Last Person to Fit in the Bus
6-
6. Manager with at least 5 direct reportees
7-
7. Mismatched IDs
8-
8. Odd and Even Measurements
9-
9. Qualifying Criteria
5+
5. Increasing Sales Revenue
6+
6. Last Person to Fit in the Bus
7+
7. Manager with at least 5 direct reportees
8+
8. Mismatched IDs
9+
9. Odd and Even Measurements
10+
10. Qualifying Criteria

0 commit comments

Comments
 (0)