Buses based on Source and Destination in SQL

Question:

Buses based on Source and Destination

Write a query to display list of bus number and names where source and destination of a bus is the destination and source of another bus. Display unique records in ascending order by bus_no.

Buses based on Source and Destination

Code:

Main.sql

select b.bus_no, b.bus_name
from buses b join(select s1.bus_no
from schedule s1 join schedule s2
on s1.source=s2.destination and s1.destination=s2.source) s
on b.bus_no=s.bus_no
order by bus_no;*

select b.bus_no, b.bus_name
from buses b, schedule s1, schedule s2
where b.bus_no=s1.bus_no and s1.source=s2.destination
and s2.source=s1.destination and s1.bus_no!=s2.bus_no
order by 1;

Recommended:

Leave a Comment

Your email address will not be published. Required fields are marked *