0% found this document useful (0 votes)
43 views

Self Join - SQLZOO

1. The document describes how to use self joins in SQL to query data from tables where the tables are joined to themselves. It provides examples of queries on data about bus routes and stops to illustrate self joins. 2. The examples show how to find bus routes between stops, list routes connecting two stops, and routes involving transfers between buses to reach a destination. 3. Self joins allow querying the same table multiple times in a FROM clause to join the table to itself to find relationships within the table.

Uploaded by

Zhiqiang Wang
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)
43 views

Self Join - SQLZOO

1. The document describes how to use self joins in SQL to query data from tables where the tables are joined to themselves. It provides examples of queries on data about bus routes and stops to illustrate self joins. 2. The examples show how to find bus routes between stops, list routes connecting two stops, and routes involving transfers between buses to reach a destination. 3. Self joins allow querying the same table multiple times in a FROM clause to join the table to itself to find relationships within the table.

Uploaded by

Zhiqiang Wang
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
You are on page 1/ 6

12/26/21, 10:00 PM Self join - SQLZOO

Self join
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

1.
How many stops are in the database.

SELECT COUNT(*) FROM stops;

Submit SQL Restore default

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

WHERE name = 'Craiglockhart';

Submit SQL Restore default

Correct answer
id
53

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

SELECT id, name

FROM stops JOIN route ON stops.id = route.stop

WHERE num = '4';

Submit SQL Restore default

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

Routes and stops

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.

SELECT company, num, COUNT(*) visits

FROM route

WHERE (stop = 149) OR (stop = 53)

GROUP BY company, num

HAVING visits = 2;

Submit SQL Restore default

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.

SELECT c.company, c.num, c.stop, d.stop

FROM route c JOIN route d ON (c.company = d.company) AND (c.num = d.num)

WHERE c.stop = 53 AND d.stop = 149;

#c and d are temporary table

Submit SQL Restore default

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'

SELECT a.company, a.num, stopa.name, stopb.name

FROM route a JOIN route b ON (a.company = b.company AND a.num = b.num)

JOIN stops stopa ON (a.stop = stopa.id)

JOIN stops stopb ON (b.stop = stopb.id)

WHERE (stopa.name = 'Craiglockhart') AND (stopb.name = 'London Road');

Submit SQL Restore default

Correct answer
company num name name
LRT 4 Craiglockhart London Road
LRT 45 Craiglockhart London Road

Using a self join

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

SELECT DISTINCT a.company, a.num

FROM route a JOIN route b ON (a.company = b.company) AND (a.num = b.num)

WHERE (a.stop = 115) AND (b.stop = 137);

Submit SQL Restore default

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'

SELECT a.company, a.num

FROM route a JOIN route b ON (a.company = b.company) AND (a.num = b.num)

JOIN stops stopa ON (stopa.id = a.stop)

JOIN stops stopb ON (stopb.id = b.stop)

WHERE (stopa.name = 'Craiglockhart') AND (stopb.name = 'Tollcross');

Submit SQL Restore default

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.

SELECT stopb.name, b.company, b.num

FROM route a JOIN route b ON (a.company = b.company) AND (a.num = b.num)

JOIN stops stopa ON stopa.id = a.stop

JOIN stops stopb ON stopb.id = b.stop

WHERE stopa.name = 'Craiglockhart';

Submit SQL Restore default

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

FROM route r1 JOIN route r2 ON (r1.num = r2.num) AND (r1.company = r2.company)

JOIN stops s1 ON r2.stop = s1.id

JOIN route r3 ON s1.id = r3.stop

JOIN route r4 ON (r3.num = r4.num) AND (r3.company = r4.company)

WHERE (r1.stop = (SELECT id FROM stops WHERE name = 'Craiglockhart')) AND (r4.stop = (SELECT id FROM stops WHERE name = 'Lochend'))

ORDER BY r1.num, s1.name, r4.num;

Submit SQL Restore default

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

Clear your results

Self join Quiz

Retrieved from "http:///sqlzoo.net/w/index.php?title=Self_join&oldid=39559"

This page was last edited on 17 December 2018, at 12:09.

https://sqlzoo.net/wiki/Self_join 6/6

You might also like