You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-- 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
+
WHEREroute.num='4'ANDroute.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=149OR stop=53
21
+
GROUP BY company, num
22
+
HAVINGCOUNT(*) >=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
+
SELECTa.company, a.num, a.stop, b.stop
26
+
FROM route a JOIN route b ON
27
+
(a.company=b.companyANDa.num=b.num)
28
+
WHEREa.stop=53ANDb.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'
-- 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
+
SELECTsb.name, a.company, a.numFROM route a
53
+
JOIN route b ONa.company=b.companyANDa.num=b.num
54
+
JOIN stops ONa.stop=stops.id
55
+
JOIN stops sb ONb.stop=sb.id
56
+
WHEREstops.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.
0 commit comments