I am a beginner of sql and 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
on (a.num=b.num and a.company=b.company) **From Craiglockhart to the transfer stop**
join route c
on (b.stop=c.stop) **to connect the transfer stop**
join route d
on (c.num=d.num and c.company=d.company) **From transfer stop to the final stop which is Sighthill**
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'
and a.num!=c.num **to delete the same route**
order by a.num
Please kindly tell me 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 did I go wrong. Thank you so much!