I tried to follow the hint by using self join twice but failed.
Find the routes involving two buses that can go from Craiglockhart to Sighthill. 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.
Hint: Self-join twice to find buses that visit Craiglockhart and Sighthill, then join those on matching stops.
My code:
select
a.num, a.company, stopsc.name, c.num, c.company
from
route a
join route b
-- From Craiglockhart to the transfer stop
on (a.num=b.num and a.company=b.company)
join route c
-- to connect the transfer stop
on (b.stop=c.stop)
join route d
-- From transfer stop to the final stop which is Sighthill
on (c.num=d.num and c.company=d.company)
join stops stopsa
on (a.stop=stopsa.id)
join stops stopsb
on (b.stop=stopsb.id)
join stops stopsc
on (c.stop=stopsc.id)
join stops stopsd
on (d.stop=stopsd.id)
where
stopsa.name='Craiglockhart'
and stopsd.name='Sighthill'
-- to delete the same route
and a.num!=c.num
order by a.num
What is the logical mistake of my answer?
I know that there is another answer on sqlselfjoin but I would like to know which step I go wrong on.