Skip to content

Commit 572464d

Browse files
committed
09 Self Join
1 parent c198042 commit 572464d

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

09_self_join.sql

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
-- How many stops are in the database.
2+
SELECT
3+
count(name)
4+
FROM
5+
stops
6+
7+
-- Find the id value for the stop 'Craiglockhart'
8+
SELECT id
9+
FROM stops
10+
WHERE name = 'Craiglockhart'
11+
12+
13+
-- Give the id and the name for the stops on the '4' 'LRT' service.
14+
SELECT id, name
15+
FROM stops JOIN route ON id = route.stop
16+
WHERE route.num = '4' AND route.company = 'LRT'
17+
18+
-- 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.
19+
SELECT company, num, COUNT(*)
20+
FROM route WHERE stop=149 OR stop=53
21+
GROUP BY company, num
22+
HAVING COUNT(*) >= 2;
23+
24+
-- 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.
25+
SELECT a.company, a.num, a.stop, b.stop
26+
FROM route a JOIN route b ON
27+
(a.company=b.company AND a.num=b.num)
28+
WHERE a.stop=53 AND b.stop = 149;
29+
30+
-- 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'
31+
SELECT a.company, a.num, stopa.name, stopb.name
32+
FROM route a JOIN route b ON
33+
(a.company=b.company AND a.num=b.num)
34+
JOIN stops stopa ON (a.stop=stopa.id)
35+
JOIN stops stopb ON (b.stop=stopb.id)
36+
WHERE stopa.name='Craiglockhart' AND stopb.name = 'London Road';
37+
38+
-- Give a list of all the services which connect stops 115 and 137 ('Haymarket' and 'Leith')
39+
SELECT DISTINCT a.company, a.num
40+
FROM route a JOIN route b ON a.company=b.company AND a.num=b.num
41+
WHERE a.stop=115 AND b.stop = 137 OR b.stop=115 AND b.stop = 137;
42+
43+
-- Give a list of the services which connect the stops 'Craiglockhart' and 'Tollcross'
44+
SELECT DISTINCT a.company, a.num
45+
FROM route a JOIN route b ON a.company = b.company AND a.num = b.num
46+
JOIN stops ax ON ax.id = a.stop
47+
JOIN stops bs ON bs.id = b.stop
48+
WHERE ax.name = 'Craiglockhart' AND bs.name = 'Tollcross' OR bs.name = 'Craiglockhart' AND ax.name = 'Tollcross';
49+
50+
51+
-- 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.
52+
SELECT sb.name, a.company, a.num FROM route a
53+
JOIN route b ON a.company = b.company AND a.num = b.num
54+
JOIN stops ON a.stop = stops.id
55+
JOIN stops sb ON b.stop = sb.id
56+
WHERE stops.name = 'Craiglockhart';
57+
58+
-- Find the routes involving two buses that can go from Craiglockhart to Lochend.
59+
-- Show the bus no. and company for the first bus, the name of the stop for the transfer,
60+
-- and the bus no. and company for the second bus.
61+
SELECT
62+
a.num AS a_num,
63+
a.company AS a_company,
64+
layover.name AS layover_city,
65+
b.num AS b_num,
66+
b.company AS b_company
67+
FROM
68+
(
69+
SELECT
70+
r.*
71+
FROM
72+
route r
73+
WHERE
74+
EXISTS
75+
(
76+
SELECT
77+
''
78+
FROM
79+
route r1
80+
INNER JOIN
81+
stops s
82+
ON ( r1.stop = s.id
83+
AND s.name = 'Craiglockhart' )
84+
WHERE
85+
r.num = r1.num
86+
AND r.company = r1.company
87+
)
88+
)
89+
a
90+
INNER JOIN
91+
stops layover
92+
ON ( a.stop = layover.id )
93+
INNER JOIN
94+
(
95+
SELECT
96+
r.*
97+
FROM
98+
route r
99+
WHERE
100+
EXISTS
101+
(
102+
SELECT
103+
''
104+
FROM
105+
route r1
106+
INNER JOIN
107+
stops s
108+
ON ( r1.stop = s.id
109+
AND s.name = 'Lochend' )
110+
WHERE
111+
r.num = r1.num
112+
AND r.company = r1.company
113+
)
114+
)
115+
b
116+
ON ( layover.id = b.stop )

0 commit comments

Comments
 (0)