Guest User

Untitled

a guest
Jul 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. /* Self join * /
  2. /* How many stops are in the database */
  3. SELECT COUNT(*)
  4. FROM stops
  5.  
  6. /* Find the id value for the stop Craiglockhart */
  7. SELECT id
  8. FROM stops
  9. WHERE name = 'Craiglockhart'
  10.  
  11. /* Give the id and the name for the stops on the '4' 'LRT' service. */
  12. SELECT id, name
  13. FROM stops JOIN route ON id = stop
  14. WHERE company = 'LRT' AND num = 4
  15.  
  16. /* Restricting output to two routes (London Road or Craiglockhart) */
  17. SELECT company, num, COUNT(*)
  18. FROM route
  19. WHERE stop = 149 or stop = 53
  20. GROUP BY company, num
  21. HAVING COUNT(*) = 2
  22.  
  23. /* Execute the self join shown and observe that b.stop gives all the places
  24. you can get to from Craiglockhart, without changing routes. Change the query
  25. so that it shows the services from Craiglockhart to London Road. */
  26. SELECT a.company, a.num, a.stop, b.stop
  27. FROM route a JOIN route b ON
  28. (a.company=b.company AND a.num=b.num)
  29. WHERE a.stop=53 AND
  30. b.stop = (SELECT id FROM stops WHERE name = 'London Road')
  31.  
  32. /* The query shown is similar to the previous one, however by joining two
  33. copies of the stops table, we can refer to stops by name rather than by number.
  34. Change the query so that the services between Craiglockhart and London Road
  35. are shown. If you are tired of these places, try Fairmilehead against Tollcross. */
  36.  
  37. SELECT a.company, a.num, stopA.name, stopB.name
  38. FROM route a JOIN route b ON (a.company = b.company AND a.num = b.num)
  39. JOIN stops stopA ON (a.stop = stopA.id)
  40. JOIN stops stopB ON (b.stop = stopB.id)
  41. WHERE stopA.name = 'Craiglockhart' AND stopB.name = 'London Road'
  42.  
  43. /* Give a list of all the services which connect stops 115 and 137 */
  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 stopA ON (a.stop = stopA.id)
  47. JOIN stops stopB ON (b.stop = stopB.id)
  48. WHERE stopA.name = 'Haymarket' AND stopB.name = 'Leith'
  49.  
  50. /* Give a list of the service which connects the stops Craiglockhart and Tollcross */
  51. SELECT DISTINCT a.company, a.num
  52. FROM route a JOIN route b ON (a.company = b.company AND a.num = b.num)
  53. JOIN stops stopA ON (a.stop = stopA.id)
  54. JOIN stops stopB ON (b.stop = stopB.id)
  55. WHERE stopA.name = 'Craiglockhart' AND stopB.name = 'Tollcross'
  56.  
  57. /* Give a distinct list of the stops which may be reached from Craiglockhart
  58. by taking one bus, including Craiglockhart itself, offered by the LRT company.
  59. Include the company and bus no. of the relevant services. */
  60. SELECT stopA.name, a.company, a.num
  61. FROM route a
  62. JOIN route b ON (a.num = b.num AND a.company = b.company)
  63. JOIN stops stopA ON (a.stop = stopA.id)
  64. JOIN stops stopB ON (b.stop = stopB.id)
  65. WHERE stopB.name = 'Craiglockhart'
Add Comment
Please, Sign In to add comment