Self Join - SQLZOO
Self Join - SQLZOO
Self join
Edinburgh Buses
Details of the database Looking at the data
stops(id, name)
stops
id
name
route
num
company
pos
stop
Summary
1.
How many stops are in the database.
Correct answer
COUNT(*)
246
2.
Find the id value for the stop 'Craiglockhart'
https://sqlzoo.net/wiki/Self_join 1/6
12/26/21, 10:00 PM Self join - SQLZOO
SELECT id
FROM stops
Correct answer
id
53
3.
Give the id and the name for the stops on the '4' 'LRT' service.
Correct answer
id name
19 Bingham
177 Northfield
149 London Road
194 Princes Street
115 Haymarket
53 Craiglockhart
179 Oxgangs
85 Fairmilehead
117 Hillend
https://sqlzoo.net/wiki/Self_join 2/6
12/26/21, 10:00 PM Self join - SQLZOO
4.
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.
FROM route
HAVING visits = 2;
Correct answer
company num visits
LRT 4 2
LRT 45 2
5.
Execute the self join shown and observe that b.stop 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.
Correct answer
company num stop stop
LRT 4 53 149
LRT 45 53 149
https://sqlzoo.net/wiki/Self_join 3/6
12/26/21, 10:00 PM Self join - SQLZOO
6.
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'
Correct answer
company num name name
LRT 4 Craiglockhart London Road
LRT 45 Craiglockhart London Road
7.
Give a list of all the services which connect stops 115 and 137 ('Haymarket' and 'Leith')
Correct answer
company num
LRT 12
LRT 2
LRT 22
LRT 25
LRT 2A
SMT C5
https://sqlzoo.net/wiki/Self_join 4/6
12/26/21, 10:00 PM Self join - SQLZOO
8.
Give a list of the services which connect the stops 'Craiglockhart' and 'Tollcross'
Correct answer
company num
LRT 10
LRT 27
LRT 45
LRT 47
9.
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.
Correct answer
name company num
Silverknowes LRT 10
Muirhouse LRT 10
Newhaven LRT 10
Leith LRT 10
L ith W lk LRT 10
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.
Hint
https://sqlzoo.net/wiki/Self_join 5/6
12/26/21, 10:00 PM Self join - SQLZOO
Self-join twice to find buses that visit Craiglockhart and Lochend, then join those on matching stops.
SELECT r1.num, r1.company, s1.name, r4.num, r4.company
WHERE (r1.stop = (SELECT id FROM stops WHERE name = 'Craiglockhart')) AND (r4.stop = (SELECT id FROM stops WHERE name = 'Lochend'))
Correct answer
num company name num company
10 LRT Leith 34 LRT
10 LRT Leith 35 LRT
10 LRT Leith 87 LRT
10 LRT Leith C5 SMT
10 LRT Princes Street 65 LRT
10 LRT Princes Street C5 SMT
27 LRT Canonmills 34 LRT
27 LRT Canonmills 35 LRT
27 LRT C T ll 20 LRT
https://sqlzoo.net/wiki/Self_join 6/6