Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -874,3 +874,19 @@ WHERE S1.name = 'Craiglockhart'
AND R1.num = R2.num
AND R2.stop = S2.id;
```
10. Find the routes involving two buses that can go from Craiglockhart to Lochend.
Show the bus no. and company for the first bus, the name of the stop for the transfer,
and the bus no. and company for the second bus.
```sql
SELECT R1.num, R1.company, S2.name, R4.num, R4.company
FROM route R1
JOIN route R2 ON (R1.company = R2.company AND R1.num = R2.num)
JOIN route R3 ON (R2.stop = R3.stop)
JOIN route R4 ON (R3.company = R4.company AND R3.num = R4.num)
JOIN stops S1 ON R1.stop = S1.id
JOIN stops S2 ON R2.stop = S2.id
JOIN stops S3 ON R3.stop = S3.id
JOIN stops S4 ON R4.stop = S4.id
WHERE S1.name = 'Craiglockhart' AND S4.name = 'Lochend'
ORDER BY R1.num, S2.name, R4.num
```
15 changes: 15 additions & 0 deletions solutions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -764,3 +764,18 @@ WHERE S1.name = 'Craiglockhart'
AND R1.company = R2.company
AND R1.num = R2.num
AND R2.stop = S2.id;

-- 10. Find the routes involving two buses that can go from Craiglockhart to Lochend.
-- Show the bus no. and company for the first bus, the name of the stop for the transfer,
-- and the bus no. and company for the second bus.
SELECT R1.num, R1.company, S2.name, R4.num, R4.company
FROM route R1
JOIN route R2 ON (R1.company = R2.company AND R1.num = R2.num)
JOIN route R3 ON (R2.stop = R3.stop)
JOIN route R4 ON (R3.company = R4.company AND R3.num = R4.num)
JOIN stops S1 ON R1.stop = S1.id
JOIN stops S2 ON R2.stop = S2.id
JOIN stops S3 ON R3.stop = S3.id
JOIN stops S4 ON R4.stop = S4.id
WHERE S1.name = 'Craiglockhart' AND S4.name = 'Lochend'
ORDER BY R1.num, S2.name, R4.num