Skip to content

Commit a77c8ca

Browse files
Added Trip Details
1 parent 061bb0d commit a77c8ca

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

Trip Details/README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Trip Details
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 : travel_data</b>
12+
13+
| Column Name |Type |
14+
| ------------- | ------------- |
15+
| customer | varchar(20) |
16+
| start_loc | varchar(50) |
17+
| end_loc | varchar(50) |
18+
19+
This table contains data about passengers traveling between various cities. </br>
20+
</br>
21+
22+
You are given the travel data for each customer in no particular order. <br>
23+
Write a SQL to find the start location and end location of the customer and total number of locations covered by them.</br>
24+
</br>
25+
26+
The query result format is in the following example:
27+
28+
<details>
29+
<summary>
30+
Input
31+
</summary>
32+
</br>
33+
34+
<b>Table Name: travel_data</b>
35+
36+
| customer | start_loc | end_loc |
37+
|----------|------------|------------|
38+
| c1 | New York | Lima |
39+
| c1 | London | New York |
40+
| c1 | Lima | Sao Paulo |
41+
| c1 | Sao Paulo | New Delhi |
42+
| c2 | Mumbai | Hyderabad |
43+
| c2 | Surat | Pune |
44+
| c2 | Hyderabad | Surat |
45+
| c3 | Kochi | Kurnool |
46+
| c3 | Lucknow | Agra |
47+
| c3 | Agra | Jaipur |
48+
| c3 | Jaipur | Kochi |
49+
50+
51+
52+
</details>
53+
54+
<details>
55+
<summary>
56+
Output
57+
</summary>
58+
</br>
59+
60+
| customer | start_loc | end_loc | place_visited |
61+
|----------|-----------|-----------|---------------|
62+
| c1 | London | New Delhi | 5 |
63+
| c2 | Mumbai | Pune | 4 |
64+
| c3 | Lucknow | Kurnool | 5 |
65+
66+
67+
</details>
68+
69+
---

Trip Details/schema.sql

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
CREATE TABLE travel_data (
2+
customer VARCHAR(10),
3+
start_loc VARCHAR(50),
4+
end_loc VARCHAR(50)
5+
);
6+
7+
INSERT INTO
8+
travel_data (customer, start_loc, end_loc)
9+
VALUES
10+
('c1', 'New York', 'Lima'),
11+
('c1', 'London', 'New York'),
12+
('c1', 'Lima', 'Sao Paulo'),
13+
('c1', 'Sao Paulo', 'New Delhi'),
14+
('c2', 'Mumbai', 'Hyderabad'),
15+
('c2', 'Surat', 'Pune'),
16+
('c2', 'Hyderabad', 'Surat'),
17+
('c3', 'Kochi', 'Kurnool'),
18+
('c3', 'Lucknow', 'Agra'),
19+
('c3', 'Agra', 'Jaipur'),
20+
('c3', 'Jaipur', 'Kochi');
21+
22+
COMMIT;

Trip Details/solution.sql

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
WITH cte AS (
2+
SELECT
3+
a.customer,
4+
a.start_loc,
5+
a.end_loc,
6+
b.end_loc prev_end_loc,
7+
c.start_loc next_start_loc
8+
FROM
9+
travel_data a
10+
LEFT OUTER JOIN travel_data b ON a.start_loc = b.end_loc
11+
LEFT OUTER JOIN travel_data c ON a.end_loc = c.start_loc
12+
),
13+
start_location AS (
14+
SELECT
15+
customer,
16+
start_loc
17+
FROM
18+
cte
19+
WHERE
20+
prev_end_loc IS NULL
21+
),
22+
end_location AS (
23+
SELECT
24+
customer,
25+
end_loc
26+
FROM
27+
cte
28+
WHERE
29+
next_start_loc IS NULL
30+
),
31+
counter AS (
32+
SELECT
33+
customer,
34+
COUNT(*) + 1 place_visited
35+
FROM
36+
cte
37+
GROUP BY
38+
customer
39+
)
40+
SELECT
41+
a.customer,
42+
a.start_loc,
43+
b.end_loc,
44+
c.place_visited
45+
FROM
46+
start_location a
47+
INNER JOIN end_location b ON a.customer = b.customer
48+
INNER JOIN counter c ON a.customer = c.customer;

0 commit comments

Comments
 (0)