Skip to content

Commit 340351d

Browse files
Added Final Destination
1 parent 8ab80c2 commit 340351d

File tree

4 files changed

+122
-7
lines changed

4 files changed

+122
-7
lines changed

Final Destination/READme.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Final Destination
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 : Flights</b>
12+
13+
| Column Name |Type |
14+
| ------------- | ------------- |
15+
| cid | VARCHAR(512) |
16+
| fid | VARCHAR(512) |
17+
| origin | VARCHAR(512) |
18+
| Destination | VARCHAR(512) |
19+
20+
* fid (Flight ID) is the primary key column for this table.
21+
* Each row of this table indicates the cid (Customer ID), fid, Origin, and Destination of the flight.
22+
<br/>
23+
24+
25+
26+
Write a SQL query to find the Initial Origin and Final Destination for each cid.</br>
27+
Return the result table ordered by cid in ascending order.
28+
29+
</br>
30+
The query result format is in the following example.
31+
</br>
32+
</br>
33+
<details>
34+
<summary>
35+
Input
36+
</summary>
37+
<br>
38+
<b>Table Name: Flights</b>
39+
40+
| cid | fid | origin | destination |
41+
|-----|-----|--------|-------------|
42+
| 1 | f1 | Del | Hyd |
43+
| 1 | f2 | Hyd | Blr |
44+
| 1 | f3 | Blr | Pune |
45+
| 2 | f4 | Mum | Agra |
46+
| 2 | f5 | Agra | Kol |
47+
48+
49+
</details>
50+
51+
<details>
52+
<summary>
53+
Output
54+
</summary>
55+
56+
| cid | origin | destination |
57+
| ---- | ---- | ---|
58+
| 1 | Del | Pune |
59+
| 2 | Mum | Kol |
60+
61+
</details>
62+
63+
---

Final Destination/schema.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
CREATE TABLE flights (
2+
cid VARCHAR(512),
3+
fid VARCHAR(512),
4+
origin VARCHAR(512),
5+
Destination VARCHAR(512)
6+
);
7+
8+
INSERT INTO
9+
flights (cid, fid, origin, Destination)
10+
VALUES
11+
('1', 'f1', 'Del', 'Hyd'),
12+
('1', 'f2', 'Hyd', 'Blr'),
13+
('1', 'f3', 'Blr', 'Pune'),
14+
('2', 'f4', 'Mum', 'Agra'),
15+
('2', 'f5', 'Agra', 'Kol');
16+
17+
COMMIT;

Final Destination/solution.sql

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
WITH intermediate_origin AS (
2+
SELECT
3+
a.cid,
4+
a.fid,
5+
a.origin,
6+
a.destination,
7+
b.destination prev_destination
8+
FROM
9+
flights a
10+
LEFT OUTER JOIN flights b ON a.cid = b.cid
11+
AND a.origin = b.destination
12+
),
13+
intermediate_destination AS (
14+
SELECT
15+
a.cid,
16+
a.fid,
17+
a.origin,
18+
a.destination,
19+
b.origin next_destination
20+
FROM
21+
flights a
22+
LEFT OUTER JOIN flights b ON a.cid = b.cid
23+
AND a.destination = b.origin
24+
)
25+
SELECT
26+
a.cid,
27+
a.origin,
28+
b.destination
29+
FROM
30+
intermediate_origin a
31+
INNER JOIN intermediate_destination b ON a.cid = b.cid
32+
WHERE
33+
prev_destination IS NULL
34+
AND next_destination IS NULL;

iqsql.md

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

0 commit comments

Comments
 (0)