0% found this document useful (0 votes)
2 views9 pages

Self Join - SQLZoo

The document provides an overview of self joins in SQL using a database of Edinburgh buses. It includes various SQL queries to retrieve information about bus stops and routes, such as finding stops, counting routes, and connecting stops through self joins. The document serves as a tutorial for practicing SQL self joins with specific examples related to bus services.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Self Join - SQLZoo

The document provides an overview of self joins in SQL using a database of Edinburgh buses. It includes various SQL queries to retrieve information about bus stops and routes, such as finding stops, counting routes, and connecting stops through self joins. The document serves as a tutorial for practicing SQL self joins with specific examples related to bus services.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

06/05/2026, 16:44 Self join - SQLZoo

Self join
Language: English • 日本語 • 中文

Edinburgh Buses
Details of the database Looking at the data

stops(id, name)
route(num, company, pos, stop)

stops
id
name

route
num
company
pos
stop

Summary

How many stops are in the database.

SELECT * FROM stops

Submit SQL restore default

Wrong answer. Too many columns.


id name
1 Aberlady

[Link] 1/9
06/05/2026, 16:44 Self join - SQLZoo

2 Abington
3 Amisfield Park
4 Ancrum
5 Armadale
6 ASDA
7 ASDA/Brunstane

Find the id value for the stop 'Craiglockhart'

Submit SQL restore default

result

[Link] 2/9
06/05/2026, 16:44 Self join - SQLZoo

Give the id and the name for the stops on the '4' 'LRT' service.

Submit SQL restore default

result

Routes and stops


The query shown gives the number of routes that visit either London Road (149) or Craiglockhart (53). Run the
query and notice the two services that link these stops have a count of 2. Add a HAVING clause to restrict the
output to these two routes.

SELECT company, num, COUNT(*)


FROM route WHERE stop=149 OR stop=53
GROUP BY company, num

Submit SQL restore default

[Link] 3/9
06/05/2026, 16:44 Self join - SQLZoo

result

Execute the self join shown and observe that [Link] gives all the places you can get to from Craiglockhart,
without changing routes. Change the query so that it shows the services from Craiglockhart to London Road.

SELECT [Link], [Link], [Link], [Link]


FROM route a JOIN route b ON
([Link]=[Link] AND [Link]=[Link])
WHERE [Link]=53

Submit SQL restore default

result

[Link] 4/9
06/05/2026, 16:44 Self join - SQLZoo

The query shown is similar to the previous one, however by joining two copies of the stops table we can refer to
stops by name rather than by number. Change the query so that the services between 'Craiglockhart' and 'London
Road' are shown. If you are tired of these places try 'Fairmilehead' against 'Tollcross'

SELECT [Link], [Link], [Link], [Link]


FROM route a JOIN route b ON
([Link]=[Link] AND [Link]=[Link])
JOIN stops stopa ON ([Link]=[Link])
JOIN stops stopb ON ([Link]=[Link])
WHERE [Link]='Craiglockhart'

Submit SQL restore default

result

[Link] 5/9
06/05/2026, 16:44 Self join - SQLZoo

Using a self join


Give a list of all the services which connect stops 115 and 137 ('Haymarket' and 'Leith')

Submit SQL restore default

result

Give a list of the services which connect the stops 'Craiglockhart' and 'Tollcross'

Submit SQL restore default

result
[Link] 6/9
06/05/2026, 16:44 Self join - SQLZoo

Give a distinct list of the stops which may be reached from 'Craiglockhart' by taking one bus, including
'Craiglockhart' itself, offered by the LRT company. Include the company and bus no. of the relevant services.

Submit SQL restore default

result

[Link] 7/9
06/05/2026, 16:44 Self join - SQLZoo

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.

Hint

Submit SQL restore default

result

Clear your results

Self join Quiz

[Link] 8/9
06/05/2026, 16:44 Self join - SQLZoo

Retrieved from "[Link]

[Link] 9/9

You might also like