Skip to content

Commit c7737d2

Browse files
Added Lift Overloaded
1 parent 3552e59 commit c7737d2

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

Lift Overloaded/README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# [Lift Overloaded](https://www.namastesql.com/coding-problem/15-lift-overloaded-part-1)
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>Table Name : lifts</b>
12+
13+
| Column Name |Type |
14+
| ------------- | ------------- |
15+
| id | int |
16+
| capacity_kg | int |
17+
18+
19+
<b>Table Name : lift_passengers</b>
20+
21+
| COLUMN_NAME | DATA_TYPE |
22+
| ------------- | ------------- |
23+
| passenger_name | varchar(10) |
24+
| weight_kg | int |
25+
| lift_id | int |
26+
27+
You are given a table of list of lifts , their maximum capacity and people along with their weight who wants to enter into it. You need to make sure maximum people enter into the lift without lift getting overloaded.
28+
29+
For each lift, Write a SQL Query to find the comma separated list of people who can be accommodated. The comma separated list should have people in the order of their weight in increasing order.
30+
31+
Display the output in increasing order of id.
32+
33+
<details>
34+
<summary>
35+
Input
36+
</summary>
37+
<br>
38+
39+
<b>Table Name: lifts</b>
40+
41+
| id |capacity_kg |
42+
| ------------- | ------------- |
43+
| 1 | 250 |
44+
| 2 | 280 |
45+
46+
<b>Table Name: lift_passengers</b>
47+
48+
| passenger_name | weight_kg | lift_id |
49+
| ------------- | ------------- | --- |
50+
| Dheeraj | 78 | 1 |
51+
| Adarsh | 70 | 1 |
52+
| Rahul | 80 | 1 |
53+
| Srinivas | 85 | 1 |
54+
| Dushyant | 95 | 1 |
55+
| Priti | 50 | 2 |
56+
| Neha | 53 | 2 |
57+
| Vimal | 58 | 2 |
58+
| Himanshi | 62 | 2 |
59+
| Akshay | 73 | 2 |
60+
| Devendra | 78 | 2 |
61+
| Amrendra | 83 | 2 |
62+
63+
64+
</details>
65+
66+
<details>
67+
<summary>
68+
Output
69+
</summary>
70+
71+
<br>
72+
73+
| id | passenger_list |
74+
|----|---------------------------|
75+
| 1 | Adarsh,Dheeraj,Rahul |
76+
| 2 | Priti,Neha,Vimal,Himanshi |

Lift Overloaded/schema.sql

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
CREATE TABLE lifts (id INT PRIMARY KEY, capacity_kg INT);
2+
3+
CREATE TABLE lift_passengers (
4+
passenger_name VARCHAR(100),
5+
weight_kg INT,
6+
lift_id INT,
7+
FOREIGN KEY (lift_id) REFERENCES lifts(id)
8+
);
9+
10+
INSERT INTO
11+
lifts (id, capacity_kg)
12+
VALUES
13+
(1, 250),
14+
(2, 280);
15+
16+
INSERT INTO
17+
lift_passengers (passenger_name, weight_kg, lift_id)
18+
VALUES
19+
('Dheeraj', 78, 1),
20+
('Adarsh', 70, 1),
21+
('Rahul', 80, 1),
22+
('Srinivas', 85, 1),
23+
('Dushyant', 95, 1),
24+
('Priti', 50, 2),
25+
('Neha', 53, 2),
26+
('Vimal', 58, 2),
27+
('Himanshi', 62, 2),
28+
('Akshay', 73, 2),
29+
('Devendra', 78, 2),
30+
('Amrendra', 83, 2);

Lift Overloaded/solution.sql

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
WITH cte AS (
2+
SELECT
3+
a.id,
4+
a.capacity_kg,
5+
b.passenger_name,
6+
b.weight_kg
7+
FROM
8+
lifts a
9+
INNER JOIN lift_passengers b ON a.id = b.lift_id
10+
),
11+
intrmdt AS (
12+
SELECT
13+
id,
14+
passenger_name,
15+
sum(weight_kg) over(
16+
PARTITION by id
17+
ORDER BY
18+
weight_kg
19+
) cum_sum_weight_kg,
20+
capacity_kg
21+
FROM
22+
cte
23+
)
24+
SELECT
25+
id,
26+
string_agg(passenger_name, ',') passenger_list
27+
FROM
28+
intrmdt
29+
WHERE
30+
cum_sum_weight_kg <= capacity_kg
31+
GROUP BY
32+
id;

0 commit comments

Comments
 (0)