├── 1_select_basics.sql ├── 2_select_from_world.sql ├── 3_select_from_nobel.sql ├── 4_select_within_select.sql ├── 5_sum_and_count.sql ├── 6_join.sql ├── 7_more_join_operations.sql ├── 8_using_null.sql ├── 9_self_join.sql └── README.md /1_select_basics.sql: -------------------------------------------------------------------------------- 1 | -- #1 Show the population of Germany 2 | SELECT population 3 | FROM world 4 | WHERE name = 'Germany'; 5 | 6 | -- #2. Show the per capita gdp: gdp/population for each country where the 7 | -- # area is over 5,000,000 km2 8 | SELECT name, gdp/population 9 | FROM world 10 | WHERE area > 5000000; 11 | 12 | -- #3. Show the name and continent where the area is less then 2000 and the 13 | -- # gdp is more than 5000000000 14 | SELECT name, continent 15 | FROM world 16 | WHERE area < 2000 17 | AND gdp > 5000000000; 18 | 19 | -- #4. Show the name and the population for 'Denmark', 'Finland', 'Norway', 20 | -- # 'Sweden' 21 | SELECT name, population 22 | FROM world 23 | WHERE name IN ('Denmark', 'Finland', 'Norway', 'Sweden'); 24 | 25 | -- #5. Show each country that begins with G. 26 | SELECT name 27 | FROM world 28 | WHERE name LIKE 'G%'; 29 | 30 | -- #6. Show the area in 1000 square km. Show area/1000 instead of area 31 | SELECT name, area/1000 32 | FROM world 33 | WHERE area BETWEEN 207600 AND 244820; -------------------------------------------------------------------------------- /2_select_from_world.sql: -------------------------------------------------------------------------------- 1 | -- # 1. Read the notes about this table. Observe the result of running 2 | -- # a simple SQL command. 3 | SELECT name, continent, population 4 | FROM world; 5 | 6 | -- # 2. Show the name for the countries that have a population of at 7 | -- # least 200 million. (200 million is 200000000, there are eight 8 | -- # zeros) 9 | SELECT name 10 | FROM world 11 | WHERE population > 200000000; 12 | 13 | -- # 3. Give the name and the per capita GDP for those countries with a 14 | -- # population of at least 200 million. 15 | SELECT name, gdp/population 16 | FROM world 17 | WHERE population > 200000000; 18 | 19 | -- # 4. Show the name and population in millions for the countries of 20 | -- # 'South America' Divide the population by 1000000 to get population 21 | -- # in millions. 22 | SELECT name, population/1000000 23 | FROM world 24 | WHERE continent = 'South America'; 25 | 26 | -- # 5. Show the name and population for 'France', 'Germany', 'Italy' 27 | SELECT name, population 28 | FROM world 29 | WHERE name IN ('France', 'Germany', 'Italy'); 30 | 31 | -- # 6. Identify the countries which have names including the word 32 | -- # 'United' 33 | SELECT name 34 | FROM world 35 | WHERE name LIKE 'United%'; -------------------------------------------------------------------------------- /3_select_from_nobel.sql: -------------------------------------------------------------------------------- 1 | -- # 1. Change the query shown so that it displays Nobel prizes for 2 | -- # 1950. 3 | SELECT yr, subject, winner 4 | FROM nobel 5 | WHERE yr = 1950; 6 | 7 | -- # 2. Show who won the 1962 prize for Literature. 8 | SELECT winner 9 | FROM nobel 10 | WHERE yr = 1962 11 | AND subject = 'Literature'; 12 | 13 | -- # 3. Show the year and subject that won 'Albert Einstein' his prize. 14 | SELECT yr, subject 15 | FROM nobel 16 | WHERE winner = 'Albert Einstein'; 17 | 18 | -- # 4. Give the name of the 'Peace' winners since the year 2000, 19 | -- # including 2000. 20 | SELECT winner 21 | FROM nobel 22 | WHERE subject = 'Peace' 23 | AND yr >= 2000; 24 | 25 | -- # 5. Show all details (yr, subject, winner) of the Literature prize 26 | -- # winners for 1980 to 1989 inclusive. 27 | SELECT * 28 | FROM nobel 29 | WHERE subject = 'Literature' 30 | AND yr BETWEEN 1980 AND 1989; 31 | 32 | -- # 6. Show all details of the presidential winners: ('Theodore 33 | -- # Roosevelt', 'Woodrow Wilson', 'Jed Bartlet', 'Jimmy Carter') 34 | SELECT yr, subject, winner 35 | FROM nobel 36 | WHERE winner IN ('Theodore Roosevelt', 'Woodrow Wilson', 'Jed Bartlet', 'Jimmy Carter'); 37 | 38 | -- # 7. Show the winners with first name John 39 | SELECT winner 40 | FROM nobel 41 | WHERE winner LIKE 'John%'; 42 | 43 | -- # 8. In which years was the Physics prize awarded but no Chemistry prize. 44 | SELECT DISTINCT yr FROM nobel 45 | WHERE subject = 'Physics' 46 | AND yr NOT IN 47 | (SELECT yr 48 | FROM nobel 49 | WHERE subject = 'Chemistry') -------------------------------------------------------------------------------- /4_select_within_select.sql: -------------------------------------------------------------------------------- 1 | -- # 1. List each country name where the population is larger than 2 | -- # 'Russia'. 3 | SELECT name 4 | FROM world 5 | WHERE population > ( 6 | SELECT population 7 | FROM world 8 | WHERE name = 'Russia'); 9 | 10 | -- # 2. List the name and continent of countries in the continents 11 | -- # containing 'Belize', 'Belgium'. 12 | SELECT name, continent 13 | FROM world 14 | WHERE continent IN ( 15 | SELECT continent 16 | FROM world 17 | WHERE name IN ('Belize', 'Belgium')); 18 | 19 | -- # 3. Show the countries in Europe with a per capita GDP greater than 20 | -- # 'United Kingdom'. 21 | SELECT name 22 | FROM world 23 | WHERE continent = 'Europe' 24 | AND gdp/population > ( 25 | SELECT gdp/population 26 | FROM world 27 | WHERE name = 'United Kingdom'); 28 | 29 | -- # 4. Which country has a population that is more than Canada but 30 | -- # less than Poland? Show the name and the population. 31 | SELECT name, population 32 | FROM world 33 | WHERE population > ( 34 | SELECT population 35 | FROM world 36 | WHERE name = 'Canada') 37 | AND population < ( 38 | SELECT population 39 | FROM world 40 | WHERE name = 'Poland'); 41 | 42 | -- # 5. Which countries have a GDP greater than any country in Europe? 43 | -- # [Give the name only.] 44 | SELECT name 45 | FROM world 46 | WHERE gdp > ( 47 | SELECT MAX(gdp) 48 | FROM world 49 | WHERE continent = 'Europe'); 50 | 51 | -- # 6. Find the largest country (by area) in each continent, show the 52 | -- # continent, the name and the area. 53 | SELECT x.continent, x.name, x.area 54 | FROM world AS x 55 | WHERE x.area = ( 56 | SELECT MAX(y.area) 57 | FROM world AS y 58 | WHERE x.continent = y.continent) 59 | 60 | -- # 7. Find each country that belongs to a continent where all 61 | -- # populations are less than 25000000. Show name, continent and 62 | -- # population. 63 | SELECT x.name, x.continent, x.population 64 | FROM world AS x 65 | WHERE 25000000 > ALL ( 66 | SELECT y.population 67 | FROM world AS y 68 | WHERE x.continent = y.continent); 69 | 70 | -- # 8. Some countries have populations more than three times that of 71 | -- # any of their neighbours (in the same continent). Give the 72 | -- # countries and continents. 73 | SELECT x.name, x.continent 74 | FROM world AS x 75 | WHERE x.population/3 > ALL ( 76 | SELECT y.population 77 | FROM world AS y 78 | WHERE x.continent = y.continent 79 | AND x.name != y.name); -------------------------------------------------------------------------------- /5_sum_and_count.sql: -------------------------------------------------------------------------------- 1 | -- # 1. Show the total population of the world. 2 | SELECT SUM(population) 3 | FROM world; 4 | 5 | -- # 2. List all the continents - just once each. 6 | SELECT DISTINCT continent 7 | FROM world; 8 | 9 | -- # 3. Give the total GDP of Africa 10 | SELECT SUM(gdp) 11 | FROM world 12 | WHERE continent = 'Africa'; 13 | 14 | -- # 4. How many countries have an area of at least 1000000 15 | SELECT COUNT(name) 16 | FROM world 17 | WHERE area >= 1000000; 18 | 19 | -- # 5. What is the total population of ('France','Germany','Spain') 20 | SELECT SUM(population) 21 | FROM world 22 | WHERE name IN ('France', 'Germany', 'Spain'); 23 | 24 | -- # 6. For each continent show the continent and number of countries. 25 | SELECT continent, COUNT(name) 26 | FROM world 27 | GROUP BY continent; 28 | 29 | -- # 7. For each continent show the continent and number of countries 30 | -- # with populations of at least 10 million. 31 | SELECT continent, COUNT(name) 32 | FROM world 33 | WHERE population >= 10000000 34 | GROUP BY continent; 35 | 36 | -- # 8. List the continents with total populations of at least 100 37 | -- # million. 38 | SELECT continent 39 | FROM world 40 | GROUP BY continent 41 | HAVING SUM(population) >= 100000000; -------------------------------------------------------------------------------- /6_join.sql: -------------------------------------------------------------------------------- 1 | -- # 1. Show matchid and player name for all goals scored by 2 | -- # Germany. teamid = 'GER'. 3 | SELECT matchid, player 4 | FROM goal 5 | WHERE teamid = 'GER'; 6 | 7 | -- # 2. Show id, stadium, team1, team2 for game 1012. 8 | SELECT id, stadium, team1, team2 9 | FROM game 10 | WHERE id = 1012; 11 | 12 | -- # 3. Show the player, teamid and mdate and for every German 13 | -- # goal. teamid='GER'. 14 | SELECT goal.player, goal.teamid, game.mdate 15 | FROM goal 16 | JOIN game 17 | ON goal.matchid = game.id 18 | WHERE goal.teamid = 'GER'; 19 | 20 | -- # 4. Show the team1, team2 and player for every goal scored by a 21 | -- # player called Mario player LIKE 'Mario%' 22 | SELECT game.team1, game.team2, goal.player 23 | FROM game 24 | JOIN goal 25 | ON goal.matchid = game.id 26 | WHERE goal.player LIKE 'Mario%'; 27 | 28 | -- # 5. Show player, teamid, coach, gtime for all goals scored in the 29 | -- # first 10 minutes gtime<=10 30 | SELECT goal.player, goal.teamid, eteam.coach, goal.gtime 31 | FROM goal 32 | JOIN eteam 33 | ON eteam.id = goal.teamid 34 | WHERE goal.gtime <= 10; 35 | 36 | -- # 6. List the the dates of the matches and the name of the team in 37 | -- # which 'Fernando Santos' was the team1 coach. 38 | SELECT game.mdate, eteam.teamname 39 | FROM game 40 | JOIN eteam 41 | ON eteam.id = game.team1 42 | WHERE eteam.coach = 'Fernando Santos'; 43 | 44 | -- # 7. List the player for every goal scored in a game where the 45 | -- # staium was 'National Stadium, Warsaw' 46 | SELECT goal.player 47 | FROM goal 48 | JOIN game 49 | ON goal.matchid = game.id 50 | WHERE game.stadium = 'National Stadium, Warsaw'; 51 | 52 | -- # 8. Show names of all players who scored a goal against Germany. 53 | SELECT DISTINCT goal.player 54 | FROM goal 55 | JOIN game 56 | ON goal.matchid = game.id 57 | WHERE goal.teamid != 'GER' 58 | AND (game.team1 = 'GER' OR game.team2 = 'GER'; 59 | 60 | -- # 9. Show teamname and the total number of goals scored. 61 | SELECT eteam.teamname, COUNT(*) 62 | FROM eteam 63 | JOIN goal 64 | ON eteam.id = goal.teamid 65 | GROUP BY eteam.teamname; 66 | 67 | -- # 10. Show the stadium and the number of goals scored in each 68 | -- # stadium. 69 | SELECT game.stadium, COUNT(*) 70 | FROM game 71 | JOIN goal 72 | ON game.id = goal.matchid 73 | GROUP BY game.stadium; 74 | 75 | -- # 11. For every match involving 'POL', show the matchid, date and 76 | -- # the number of goals scored. 77 | SELECT game.id, game.mdate, COUNT(*) 78 | FROM game 79 | JOIN goal 80 | ON game.id = goal.matchid 81 | WHERE (game.team1 = 'POL' OR game.team2 = 'POL') 82 | GROUP BY game.id, game.mdate 83 | ORDER BY game.id; 84 | 85 | -- # 12. For every match where 'GER' scored, show matchid, match date 86 | -- # and the number of goals scored by 'GER' 87 | SELECT game.id, game.mdate, COUNT(*) 88 | FROM game 89 | JOIN goal 90 | ON goal.matchid = game.id 91 | WHERE goal.teamid = 'GER' 92 | GROUP BY game.id; 93 | 94 | -- # 13. List every match with the goals scored by each team as shown. 95 | SELECT game.mdate, 96 | game.team1, 97 | SUM(CASE WHEN goal.teamid = game.team1 98 | THEN 1 99 | ELSE 0 100 | END) AS score1, 101 | game.team2, 102 | SUM(CASE WHEN goal.teamid = game.team2 103 | THEN 1 104 | ELSE 0 105 | END) AS score2 106 | FROM game 107 | JOIN goal 108 | ON (game.id = goal.matchid) 109 | GROUP BY game.id 110 | ORDER BY game.mdate, goal.matchid -------------------------------------------------------------------------------- /7_more_join_operations.sql: -------------------------------------------------------------------------------- 1 | -- # 1. List the films where the yr is 1962 [Show id, title] 2 | SELECT id, title 3 | FROM movie 4 | WHERE yr = 1962; 5 | 6 | -- # 2. Give year of 'Citizen Kane'. 7 | SELECT yr 8 | FROM movie 9 | WHERE title = 'Citizen Kane'; 10 | 11 | -- # 3. List all of the Star Trek movies, include the id, title and yr 12 | -- # (all of these movies include the words Star Trek in the 13 | -- # title). Order results by year. 14 | SELECT id, title, yr 15 | FROM movie 16 | WHERE title LIKE '%Star Trek%' 17 | ORDER BY yr; 18 | 19 | -- # 4. What are the titles of the films with id 11768, 11955, 21191. 20 | SELECT title 21 | FROM movie 22 | WHERE id IN (11768, 11955, 21191); 23 | 24 | -- # 5. What id number does the actor 'Glenn Close' have? 25 | SELECT id 26 | FROM actor 27 | WHERE name = 'Glenn Close'; 28 | 29 | -- # 6. What is the id of the film 'Casablanca' 30 | SELECT id 31 | FROM movie 32 | WHERE title = 'Casablanca'; 33 | 34 | -- # 7. Obtain the cast list for 'Casablanca'. Use the id value that 35 | -- # you obtained in the previous question. 36 | SELECT actor.name 37 | FROM actor 38 | JOIN casting 39 | ON casting.actorid = actor.id 40 | WHERE casting.movieid = 11768; 41 | 42 | -- # 8. Obtain the cast list for the film 'Alien'. 43 | SELECT actor.name 44 | FROM actor 45 | JOIN casting 46 | ON casting.actorid = actor.id 47 | JOIN movie 48 | ON movie.id = casting.movieid 49 | WHERE movie.title = 'Alien'; 50 | 51 | -- # 9. List the films in which 'Harrison Ford' has appeared 52 | SELECT movie.title 53 | FROM movie 54 | JOIN casting 55 | ON casting.movieid = movie.id 56 | JOIN actor 57 | ON actor.id = casting.actorid 58 | WHERE actor.name = 'Harrison Ford'; 59 | 60 | -- # 10. List the films where 'Harrison Ford' has appeared - but not in 61 | -- # the star role. [Note: the ord field of casting gives the position 62 | -- # of the actor. If ord=1 then this actor is in the starring role] 63 | SELECT movie.title 64 | FROM movie 65 | JOIN casting 66 | ON casting.movieid = movie.id 67 | JOIN actor 68 | ON actor.id = casting.actorid 69 | WHERE actor.name = 'Harrison Ford' 70 | AND casting.ord != 1; 71 | 72 | -- # 11. List the films together with the leading star for all 1962 films. 73 | SELECT movie.title, actor.name 74 | FROM movie 75 | JOIN casting 76 | ON casting.movieid = movie.id 77 | JOIN actor 78 | ON actor.id = casting.actorid 79 | WHERE movie.yr = 1962 80 | AND casting.ord = 1; 81 | 82 | -- # 12. Which were the busiest years for 'John Travolta', show the 83 | -- # year and the number of movies he made each year for any year in 84 | -- # which he made at least 2 movies. 85 | SELECT movie.yr, COUNT(*) 86 | FROM movie 87 | JOIN casting 88 | ON casting.movieid = movie.id 89 | JOIN actor 90 | ON actor.id = casting.actorid 91 | WHERE actor.name = 'John Travolta' 92 | GROUP BY movie.yr 93 | HAVING COUNT(movie.title) >= 2; 94 | 95 | -- # 13. List the film title and the leading actor for all of 'Julie 96 | -- # Andrews' films. 97 | SELECT DISTINCT m.title, a.name 98 | FROM (SELECT movie.* 99 | FROM movie 100 | JOIN casting 101 | ON casting.movieid = movie.id 102 | JOIN actor 103 | ON actor.id = casting.actorid 104 | WHERE actor.name = 'Julie Andrews') AS m 105 | JOIN (SELECT actor.*, casting.movieid AS movieid 106 | FROM actor 107 | JOIN casting 108 | ON casting.actorid = actor.id 109 | WHERE casting.ord = 1) as a 110 | ON m.id = a.movieid 111 | ORDER BY m.title; 112 | 113 | -- # 14. Obtain a list of actors in who have had at least 30 starring 114 | -- # roles. 115 | SELECT actor.name 116 | FROM actor 117 | JOIN casting 118 | ON casting.actorid = actor.id 119 | WHERE casting.ord = 1 120 | GROUP BY actor.name 121 | HAVING COUNT(*) >= 30; 122 | 123 | -- # 15. List the 1978 films by order of cast list size. 124 | SELECT movie.title, COUNT(*) 125 | FROM movie 126 | JOIN casting 127 | ON movie.id = casting.movieid 128 | WHERE movie.yr = 1978 129 | GROUP BY movie.id 130 | ORDER BY COUNT(*) DESC 131 | 132 | -- # 16. List all the people who have worked with 'Art Garfunkel'. 133 | SELECT a.name 134 | FROM (SELECT movie.* 135 | FROM movie 136 | JOIN casting 137 | ON casting.movieid = movie.id 138 | JOIN actor 139 | ON actor.id = casting.actorid 140 | WHERE actor.name = 'Art Garfunkel') AS m 141 | JOIN (SELECT actor.*, casting.movieid 142 | FROM actor 143 | JOIN casting 144 | ON casting.actorid = actor.id 145 | WHERE actor.name != 'Art Garfunkel') as a 146 | ON m.id = a.movieid; -------------------------------------------------------------------------------- /8_using_null.sql: -------------------------------------------------------------------------------- 1 | -- # 1. List the teachers who have NULL for their department. 2 | SELECT name 3 | FROM teacher 4 | WHERE dept IS NULL; 5 | 6 | -- # 3. Use a different JOIN so that all teachers are listed. 7 | SELECT teacher.name, dept.name 8 | FROM teacher 9 | LEFT OUTER JOIN dept 10 | ON teacher.dept = dept.id; 11 | 12 | -- # 4. Use a different JOIN so that all departments are listed. 13 | SELECT teacher.name, dept.name 14 | FROM teacher 15 | RIGHT OUTER JOIN dept 16 | ON teacher.dept = dept.id; 17 | 18 | -- # 5. Use COALESCE to print the mobile number. Use the number '07986 19 | -- #444 2266' there is no number given. Show teacher name and mobile 20 | -- #number or '07986 444 2266' 21 | SELECT name, COALESCE(mobile, '07986 444 2266') 22 | FROM teacher; 23 | 24 | -- # 6. Use the COALESCE function and a LEFT JOIN to print the name and 25 | -- #department name. Use the string 'None' where there is no 26 | -- #department. 27 | SELECT name, COALESCE(dept, 'None') 28 | FROM teacher; 29 | 30 | -- # 7. Use COUNT to show the number of teachers and the number of 31 | -- #mobile phones. 32 | SELECT COUNT(name), COUNT(mobile) 33 | FROM teacher; 34 | 35 | -- # 8. Use COUNT and GROUP BY dept.name to show each department and 36 | -- #the number of staff. Use a RIGHT JOIN to ensure that the 37 | -- #Engineering department is listed. 38 | SELECT dept.name ,COUNT(teacher.name) 39 | FROM teacher 40 | RIGHT OUTER JOIN dept 41 | ON teacher.dept = dept.id 42 | GROUP BY dept.name; 43 | 44 | -- #9: Use CASE to show the name of each teacher followed by 'Sci' if 45 | -- #the the teacher is in dept 1 or 2 and 'Art' otherwise. 46 | SELECT teacher.name, 47 | CASE 48 | WHEN teacher.dept IN (1, 2) 49 | THEN 'Sci' 50 | ELSE 'Art' 51 | END 52 | FROM teacher; 53 | 54 | -- #10: Use CASE to show the name of each teacher followed by 'Sci' if 55 | -- #the the teacher is in dept 1 or 2 show 'Art' if the dept is 3 and 56 | -- #'None' otherwise. 57 | SELECT teacher.name, 58 | CASE 59 | WHEN teacher.dept IN (1, 2) 60 | THEN 'Sci' 61 | WHEN teacher.dept = 3 62 | THEN 'Art' 63 | ELSE 'None' 64 | END 65 | FROM teacher; -------------------------------------------------------------------------------- /9_self_join.sql: -------------------------------------------------------------------------------- 1 | -- # 1. How many stops are in the database. 2 | SELECT COUNT(*) 3 | FROM stops; 4 | 5 | -- # 2. Find the id value for the stop 'Craiglockhart' 6 | SELECT id 7 | FROM stops 8 | WHERE name = 'Craiglockhart'; 9 | 10 | -- # 3. Give the id and the name for the stops on the '4' 'LRT' 11 | -- # service. 12 | SELECT stops.id, stops.name 13 | FROM route 14 | JOIN stops 15 | ON route.stop = stops.id 16 | WHERE route.num = 4 17 | AND route.company = 'LRT'; 18 | 19 | -- # 4. The query shown gives the number of routes that visit either 20 | -- # London Road (149) or Craiglockhart (53). Run the query and notice 21 | -- # the two services that link these stops have a count of 2. Add a 22 | -- # HAVING clause to restrict the output to these two routes. 23 | SELECT company, num 24 | FROM route 25 | WHERE stop IN (149, 53) 26 | GROUP BY company, num 27 | HAVING COUNT(*) = 2; 28 | 29 | -- # 5. Execute the self join shown and observe that b.stop gives all 30 | -- # the places you can get to from Craiglockhart. Change the query so 31 | -- # that it shows the services from Craiglockhart to London Road. 32 | SELECT r1.company, r1.num, r1.stop, r2.stop 33 | FROM route AS r1 34 | JOIN route AS r2 35 | ON (r1.company = r2.company) 36 | AND (r1.num = r2.num) 37 | WHERE r1.stop = 53 38 | AND r2.stop = 149; 39 | 40 | -- # 6. The query shown is similar to the previous one, however by 41 | -- # joining two copies of the stops table we can refer to stops by 42 | -- # name rather than by number. Change the query so that the services 43 | -- # between 'Craiglockhart' and 'London Road' are shown. If you are 44 | -- # tired of these places try 'Fairmilehead' against 'Tollcross' 45 | SELECT r1.company, r1.num, s1.name, s2.name 46 | FROM route AS r1 47 | JOIN route AS r2 48 | ON (r1.company, r1.num) = (r2.company, r2.num) 49 | JOIN stops AS s1 50 | ON r1.stop = s1.id 51 | JOIN stops AS s2 52 | ON r2.stop = s2.id 53 | WHERE s1.name = 'Craiglockhart' 54 | AND s2.name = 'London Road'; 55 | 56 | -- # 7. Give a list of all the services which connect stops 115 and 137 57 | -- # ('Haymarket' and 'Leith') 58 | SELECT DISTINCT r1.company, r2.num 59 | FROM route AS r1 60 | JOIN route AS r2 61 | ON (r1.company, r1.num) = (r2.company, r2.num) 62 | WHERE r1.stop = 115 63 | AND r2.stop = 137; 64 | 65 | -- # 8. Give a list of the services which connect the stops 66 | -- # 'Craiglockhart' and 'Tollcross' 67 | SELECT r1.company, r1.num, s1.name, s2.name 68 | FROM route AS r1 69 | JOIN route AS r2 70 | ON (r1.company, r1.num) = (r2.company, r2.num) 71 | JOIN stops AS s1 72 | ON r1.stop = s1.id 73 | JOIN stops AS s2 74 | ON r2.stop = s2.id 75 | WHERE s1.name = 'Craiglockhart' 76 | AND s2.name = 'Tollcross'; 77 | 78 | -- # 9. Give a list of the stops which may be reached from 79 | -- # 'Craiglockhart' by taking one bus. Include the details of the 80 | -- # appropriate service. 81 | SELECT r1.company, r1.num, s1.name, s2.name 82 | FROM route AS r1 83 | JOIN route AS r2 84 | ON (r1.company, r1.num) = (r2.company, r2.num) 85 | JOIN stops AS s1 86 | ON r1.stop = s1.id 87 | JOIN stops AS s2 88 | ON r2.stop = s2.id 89 | WHERE s1.name = 'Craiglockhart'; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SQLZoo 2 | 3 | SQL exercises from http://sqlzoo.net. --------------------------------------------------------------------------------