├── 0_solution.sql ├── 1_solution.sql ├── 2_solutions.sql ├── 3_solutions.sql ├── 4_soutions.sql ├── 5_solutions.sql ├── 6_solutions.sql ├── 7_solutions.sql ├── 8_solutions.sql ├── 9_solutions.sql ├── README.md └── README.md~ /0_solution.sql: -------------------------------------------------------------------------------- 1 | --1. Show the population of Germany 2 | 3 | SELECT population FROM world 4 | WHERE name = 'Germany'; 5 | 6 | --2. Show the name and per capita gdp: gdp/population for each country where the area is over 5,000,000 km2 7 | 8 | SELECT name, gdp/population FROM world 9 | WHERE area > 5000000; 10 | 11 | --3. Show the name and continent where the area is less than 2000 and the gdp is more than 5000000000 12 | SELECT name , continent 13 | FROM world 14 | WHERE area < 2000 15 | AND gdp > 5000000000; 16 | 17 | --4. Show the name and the population for 'Denmark', 'Finland', 'Norway', 'Sweden' 18 | 19 | SELECT name, population FROM world 20 | WHERE name IN ('Finland', 'Norway', 21 | 'Denmark', 'Sweden'); 22 | 23 | --5. Show each country that begins with G 24 | 25 | SELECT name FROM world 26 | WHERE name LIKE 'G%'; 27 | 28 | --6. Show the area in 1000 square km. Show area/1000 instead of area 29 | 30 | SELECT name, area/1000 FROM world 31 | WHERE area BETWEEN 200000 AND 250000; 32 | -------------------------------------------------------------------------------- /1_solution.sql: -------------------------------------------------------------------------------- 1 | --1. Find the country that start with Y 2 | 3 | SELECT name FROM world 4 | WHERE name LIKE 'Y%'; 5 | 6 | --2. Find the countries that end with y 7 | 8 | SELECT name FROM world 9 | WHERE name LIKE '%y'; 10 | 11 | --3. Find the countries that contain the letter x 12 | 13 | SELECT name FROM world 14 | WHERE name LIKE '%x%'; 15 | 16 | --4. Find the countries that end with land 17 | 18 | SELECT name FROM world 19 | WHERE name LIKE '%land'; 20 | 21 | --5. Find the countries that start with C and end with ia 22 | 23 | SELECT name FROM world 24 | WHERE name LIKE 'C%ia'; 25 | 26 | --6. Find the country that has oo in the name 27 | 28 | SELECT name FROM world 29 | WHERE name LIKE '%oo%'; 30 | 31 | --7. Find the countries that have three or more a in the name 32 | 33 | SELECT name FROM world 34 | WHERE name LIKE '%a%a%a%'; 35 | 36 | --8. Find the countries that have "t" as the second character. 37 | 38 | SELECT name FROM world 39 | WHERE name LIKE '_t%' 40 | ORDER BY name; 41 | 42 | --9. Find the countries that have two "o" characters separated by two others. 43 | 44 | SELECT name FROM world 45 | WHERE name LIKE '%o__o%'; 46 | 47 | --10. Find the countries that have exactly four characters. 48 | 49 | SELECT name FROM world 50 | WHERE name LIKE '____'; 51 | 52 | --11. Find the country where the name is the capital city. 53 | 54 | SELECT name 55 | FROM world 56 | WHERE name = capital; 57 | 58 | --12. Find the country where the capital is the country plus "City". 59 | 60 | SELECT name 61 | FROM world 62 | WHERE capital = concat(name, ' City'); 63 | 64 | --13. Find the capital and the name where the capital includes the name of the country. 65 | 66 | SELECT capital,name FROM world WHERE capital LIKE concat('%', name, '%'); 67 | 68 | --14. Find the capital and the name where the capital is an extension of name of the country. 69 | 70 | SELECT name, capital FROM world WHERE capital LIKE concat('%', name, '%') AND capital > name; 71 | 72 | --15. Show the name and the extension where the capital is an extension of name of the country. 73 | 74 | SELECT name, REPLACE(capital, name, '') FROM world WHERE capital LIKE concat('%', name, '%') AND capital > name; 75 | -------------------------------------------------------------------------------- /2_solutions.sql: -------------------------------------------------------------------------------- 1 | --1. Read the notes about this table. Observe the result of running a simple SQL command. 2 | 3 | SELECT name, continent, population FROM world; 4 | 5 | --2. Show the name for the countries that have a population of at least 200 million. 6 | 7 | SELECT name FROM world 8 | WHERE population>=200000000; 9 | 10 | --3. Give the name and the per capita GDP for those countries with a population of at least 200 million. 11 | 12 | SELECT name, gdp/population FROM world WHERE population>200000000; 13 | 14 | --4. Show the name and population in millions for the countries of the continent 'South America'. 15 | 16 | SELECT name, population/1000000 FROM world WHERE continent = 'South America'; 17 | 18 | --5. Show the name and population for France, Germany, Italy 19 | 20 | SELECT name, population FROM world WHERE name IN ('France', 'Germany', 'Italy'); 21 | 22 | --6. Show the countries which have a name that includes the word 'United' 23 | 24 | SELECT name FROM world WHERE name LIKE '%united%'; 25 | 26 | --7. Show the countries that are big by area or big by population. Show name, population and area. 27 | 28 | SELECT name, population, area FROM world WHERE area > 3000000 OR population > 250000000; 29 | 30 | --8. Show the countries that are big by area or big by population but not both. Show name, population and area. 31 | 32 | SELECT name, population, area FROM world WHERE (area > 3000000 AND population < 250000000) OR (population > 250000000 AND area < 3000000); 33 | 34 | --9. For South America show population in millions and GDP in billions to 2 decimal places. 35 | 36 | SELECT name, ROUND(population/1000000, 2), ROUND(gdp/1000000000, 2) FROM world WHERE continent = 'South America'; 37 | 38 | --10. Show per-capita GDP for the trillion dollar countries to the nearest $1000. 39 | 40 | SELECT name, ROUND(gdp/population, -3) FROM world WHERE gdp > 1000000000000; 41 | 42 | --11. Show the name and the continent - but substitute Australasia for Oceania - for countries beginning with N. 43 | 44 | SELECT name, CASE WHEN continent='Oceania' THEN 'Australasia' 45 | ELSE continent END 46 | FROM world 47 | WHERE name LIKE 'N%'; 48 | 49 | --12. Show the name and the continent - but substitute Eurasia for Europe and Asia; substitute America - for each country in North America or South America or Caribbean. Show countries beginning with A or B 50 | 51 | SELECT name, CASE 52 | WHEN continent IN ('Europe', 'Asia') THEN 'Eurasia' 53 | WHEN continent IN ('North America', 'South America', 'Caribbean') THEN 'America' 54 | ELSE continent END 55 | FROM world 56 | WHERE name LIKE 'A%' OR name LIKE 'B%'; 57 | 58 | --13. Put the continents right...Oceania becomes Australasia Countries in Eurasia and Turkey go to Europe/Asia. Caribbean islands starting with 'B' go to North America, other Caribbean islands go to South America. Show the name, the original continent and the new continent of all countries. 59 | 60 | SELECT name, continent, CASE 61 | WHEN continent = 'Oceania' THEN 'Australasia' 62 | WHEN continent IN ('Eurasia', 'Turkey') THEN 'Europe/Asia' 63 | WHEN continent = 'Caribbean' AND name LIKE 'B%' THEN 'North America' 64 | WHEN continent = 'Caribbean' THEN 'South America' 65 | ELSE continent END 66 | FROM world 67 | ORDER BY name; 68 | -------------------------------------------------------------------------------- /3_solutions.sql: -------------------------------------------------------------------------------- 1 | --1. Change the query shown so that it displays Nobel prizes for 1950. 2 | 3 | SELECT yr, subject, winner 4 | FROM nobel 5 | WHERE yr = 1950; 6 | 7 | --2. Show who won the 1962 prize for Literature. 8 | 9 | SELECT winner 10 | FROM nobel 11 | WHERE yr = 1962 12 | AND subject = 'Literature'; 13 | 14 | --3. Show the year and subject that won 'Albert Einstein' his prize. 15 | 16 | SELECT yr, subject FROM nobel WHERE winner = 'Albert Einstein'; 17 | 18 | --4. Give the name of the 'Peace' winners since the year 2000, including 2000. 19 | 20 | SELECT winner FROM nobel WHERE subject = 'Peace' AND yr >= 2000; 21 | 22 | --5. Show all details (yr, subject, winner) of the Literature prize winners for 1980 to 1989 inclusive. 23 | 24 | SELECT * FROM nobel WHERE subject = 'Literature' AND yr >= 1980 AND yr <= 1989; 25 | 26 | --6. Show all details of the presidential winners 27 | 28 | SELECT * FROM nobel 29 | WHERE winner IN ('Theodore Roosevelt', 30 | 'Woodrow Wilson', 31 | 'Jimmy Carter'); 32 | 33 | --7. Show the winners with first name John 34 | 35 | SELECT winner FROM nobel WHERE winner LIKE 'John%'; 36 | 37 | --8. Show the Physics winners for 1980 together with the Chemistry winners for 1984. 38 | 39 | SELECT * FROM nobel WHERE subject = 'Physics' AND yr = 1980 OR subject = 'Chemistry' AND yr = 1984; 40 | 41 | --9. Show the winners for 1980 excluding the Chemistry and Medicine 42 | 43 | SELECT * FROM nobel WHERE yr = 1980 AND subject NOT IN ('Chemistry', 'Medicine'); 44 | 45 | --10. Show who won a 'Medicine' prize in an early year (before 1910, not including 1910) together with winners of a 'Literature' prize in a later year (after 2004, including 2004) 46 | 47 | SELECT * FROM nobel WHERE subject = 'Medicine' AND yr < 1910 OR subject = 'Literature' AND yr >= 2004; 48 | 49 | --11. Find all details of the prize won by PETER GRÜNBERG 50 | 51 | SELECT * FROM nobel WHERE winner = 'Peter Grünberg'; 52 | 53 | --12.Find all details of the prize won by EUGENE O'NEILL 54 | 55 | SELECT * FROM nobel WHERE winner = 'Eugene O''Neill'; 56 | 57 | --13. List the winners, year and subject where the winner starts with Sir. Show the the most recent first, then by name order. 58 | 59 | Select winner, yr, subject FROM nobel WHERE winner LIKE 'Sir%' ORDER BY yr DESC, winner; 60 | 61 | --14. Show the 1984 winners ordered by subject and winner name; but list Chemistry and Physics last. 62 | 63 | SELECT winner, subject 64 | FROM nobel 65 | WHERE yr=1984 66 | ORDER BY subject IN ('Physics','Chemistry'), subject,winner; 67 | 68 | -------------------------------------------------------------------------------- /4_soutions.sql: -------------------------------------------------------------------------------- 1 | --1. List each country name where the population is larger than 'Russia'. 2 | 3 | SELECT name FROM world 4 | WHERE population > 5 | (SELECT population FROM world 6 | WHERE name='Russia'); 7 | 8 | --2. Show the countries in Europe with a per capita GDP greater than 'United Kingdom'. 9 | 10 |  SELECT name FROM world WHERE gdp/population > (SELECT gdp/population FROM world WHERE name = 'United Kingdom'); 11 | 12 | --3. List the name and continent of countries in the continents containing either Argentina or Australia. Order by name of the country. 13 | 14 | SELECT name, continent FROM world WHERE continent IN (SELECT continent FROM world WHERE name IN ('Argentina', 'Australia')) ORDER BY name; 15 | 16 | --4. Which country has a population that is more than Canada but less than Poland? Show the name and the population. 17 | 18 | SELECT name, population FROM world WHERE population > (SELECT population FROM world WHERE name = 'Canada') AND population < (SELECT population FROM world WHERE name = 'Poland'); 19 | 20 | --5. Show the name and the population of each country in Europe. Show the population as a percentage of the population of Germany. 21 | 22 | SELECT name, CONCAT(ROUND(population/(SELECT population FROM world WHERE name = 'Germany')*100), '%') FROM world WHERE continent = 'Europe'; 23 | 24 | --6. Which countries have a GDP greater than every country in Europe? [Give the name only.] (Some countries may have NULL gdp values) 25 | 26 | SELECT name FROM world WHERE gdp > ALL(SELECT gdp FROM world WHERE gdp > 0 AND continent = 'Europe'); 27 | 28 | --7. Find the largest country (by area) in each continent, show the continent, the name and the area: 29 | 30 | SELECT continent, name, area FROM world x 31 | WHERE area >= ALL(SELECT area FROM world y WHERE x.continent = y.continent AND y.area>0); 32 | 33 | --8. List each continent and the name of the country that comes first alphabetically. 34 | 35 | SELECT continent, name FROM world x WHERE name <= ALL(SELECT name FROM world y WHERE x.continent = y.continent); 36 | 37 | --9. Find the continents where all countries have a population <= 25000000. Then find the names of the countries associated with these continents. Show name, continent and population. 38 | 39 | SELECT name, continent, population FROM world WHERE continent IN (SELECT continent FROM world x WHERE 25000000 >= (SELECT MAX(population) FROM world y WHERE x.continent = y.continent)); 40 | or 41 | SELECT y.name, y.continent, y.population 42 | FROM world AS y 43 | JOIN 44 | (SELECT continent,max(population) 45 | FROM world 46 | GROUP BY continent 47 | HAVING max(population) <= 25000000) AS x 48 | ON y.continent = x.continent 49 | 50 | --10. Some countries have populations more than three times that of any of their neighbours (in the same continent). Give the countries and continents. 51 | SELECT name, continent FROM world x 52 | WHERE population > ALL(SELECT 3*population FROM world y WHERE x.continent = y.continent AND x.name <> y.name) 53 | -------------------------------------------------------------------------------- /5_solutions.sql: -------------------------------------------------------------------------------- 1 | --1. Show the total population of the world. 2 | 3 | SELECT SUM(population) 4 | FROM world; 5 | 6 | --2. List all the continents - just once each. 7 | 8 | SELECT DISTINCT continent FROM world; 9 | 10 | --3. Give the total GDP of Africa 11 | SELECT SUM(gdp) FROM world WHERE continent = 'Africa'; 12 | 13 | --4. How many countries have an area of at least 1000000 14 | SELECT COUNT(*) FROM world WHERE area >= 1000000; 15 | 16 | --5. What is the total population of ('France','Germany','Spain') 17 | 18 | SELECT SUM(population) FROM world WHERE name IN ('France','Germany','Spain'); 19 | 20 | --6. For each continent show the continent and number of countries. 21 | 22 | SELECT continent, COUNT(*) FROM world GROUP BY continent; 23 | 24 | --7. For each continent show the continent and number of countries with populations of at least 10 million. 25 | 26 | SELECT continent, COUNT(*) FROM world WHERE population >= 10000000 GROUP BY continent; 27 | 28 | --8. List the continents that have a total population of at least 100 million. 29 | 30 | SELECT continent FROM world x WHERE (SELECT SUM(population) FROM world y WHERE x.continent = y.continent) >= 100000000 GROUP BY continent; 31 | 32 | -------------------------------------------------------------------------------- /6_solutions.sql: -------------------------------------------------------------------------------- 1 | --1. Show matchid and player name for all goals scored by Germany. teamid = 'GER' 2 | 3 | SELECT matchid, player FROM goal 4 | WHERE teamid = 'GER'; 5 | 6 | --2. Show id, stadium, team1, team2 for game 1012 7 | 8 | SELECT id, stadium, team1, team2 FROM game WHERE id = 1012; 9 | 10 | --3. Show the player, teamid and mdate and for every German goal. teamid='GER' 11 | 12 | SELECT goal.player, goal.teamid, game.mdate FROM game JOIN goal ON game.id = goal.matchid WHERE teamid='GER'; 13 | 14 | --4. Show the team1, team2 and player for every goal scored by a player called Mario player LIKE 'Mario%' 15 | 16 | SELECT game.team1, game.team2, goal.player FROM goal JOIN game ON game.id = goal.matchid WHERE goal.player LIKE 'Mario%'; 17 | 18 | --5. Show player, teamid, coach, gtime for all goals scored in the first 10 minutes gtime<=10 19 | 20 | SELECT player, teamid, coach, gtime FROM goal JOIN eteam ON goal.teamid = eteam.id WHERE gtime <= 10; 21 | 22 | --6. List the the dates of the matches and the name of the team in which 'Fernando Santos' was the team1 coach. 23 | 24 | SELECT mdate, teamname FROM eteam JOIN game ON 25 | (eteam.id = game.team1) WHERE coach = 'Fernando Santos'; 26 | 27 | --7. List the player for every goal scored in a game where the stadium was 'National Stadium, Warsaw' 28 | 29 | SELECT player FROM game JOIN goal ON id = matchid WHERE stadium = 'National Stadium, Warsaw'; 30 | 31 | --8. Instead show the name of all players who scored a goal against Germany. 32 | 33 | SELECT DISTINCT player 34 | FROM game JOIN goal ON matchid = id 35 | WHERE (team1='GER' OR team2='GER') AND teamid != 'GER'; 36 | 37 | --9. Show teamname and the total number of goals scored. 38 | 39 | SELECT teamname, COUNT(*) FROM goal JOIN eteam ON eteam.id = goal.teamid GROUP BY teamname; 40 | 41 | --10. Show the stadium and the number of goals scored in each stadium. 42 | 43 | SELECT stadium, COUNT(*) FROM goal JOIN game ON matchid = id GROUP BY stadium; 44 | 45 | --11. For every match involving 'POL', show the matchid, date and the number of goals scored. 46 | 47 | SELECT matchid,mdate, COUNT(*) 48 | FROM game JOIN goal ON matchid = id 49 | WHERE (team1 = 'POL' OR team2 = 'POL') GROUP BY matchid; 50 | 51 | --12. For every match where 'GER' scored, show matchid, match date and the number of goals scored by 'GER' 52 | 53 | SELECT matchid, mdate, COUNT(*) FROM goal JOIN game ON id = matchid WHERE (team1 = 'GER' OR team2 = 'GER') AND teamid = 'GER' GROUP BY matchid; 54 | 55 | --13. List every match with the goals scored by each team as shown. 56 | 57 | SELECT DISTINCT mdate, team1, 58 | SUM(CASE WHEN teamid=team1 THEN 1 ELSE 0 END) score1, team2, SUM(CASE WHEN teamid=team2 THEN 1 ELSE 0 END) score2 59 | FROM game JOIN goal ON matchid = id 60 | GROUP BY mdate,team1,team2 61 | ORDER BY mdate,team1,team2; -------------------------------------------------------------------------------- /7_solutions.sql: -------------------------------------------------------------------------------- 1 | --1. List the films where the yr is 1962 [Show id, title] 2 | 3 | SELECT id, title 4 | FROM movie 5 | WHERE yr=1962; 6 | 7 | --2. Give year of 'Citizen Kane'. 8 | 9 | SELECT yr FROM movie WHERE title = 'Citizen Kane'; 10 | 11 | --3. List all of the Star Trek movies, include the id, title and yr (all of these movies include the words Star Trek in the title). Order results by year. 12 | 13 | SELECT id, title, yr FROM movie WHERE title LIKE '%Star Trek%' ORDER BY yr; 14 | 15 | --4. What are the titles of the films with id 11768, 11955, 21191 16 | 17 | SELECT title FROM movie WHERE id IN (11768,11955, 21191); 18 | 19 | --5. What id number does the actress 'Glenn Close' have? 20 | 21 | SELECT id FROM actor WHERE name = 'Glenn Close'; 22 | 23 | --6. What is the id of the film 'Casablanca' 24 | 25 | SELECT id FROM movie WHERE title = 'Casablanca'; 26 | 27 | --7. Obtain the cast list for 'Casablanca'. 28 | 29 | SELECT name FROM actor JOIN casting ON id=actorid WHERE movieid = 11768; 30 | 31 | --8. Obtain the cast list for the film 'Alien' 32 | 33 | SELECT name FROM actor JOIN casting ON id = actorid WHERE movieid = (SELECT id FROM movie WHERE title = 'Alien'); 34 | 35 | --9. List the films in which 'Harrison Ford' has appeared 36 | 37 | SELECT title FROM movie JOIN casting ON movieid = id WHERE actorid = (SELECT id FROM actor WHERE name = 'Harrison Ford'); 38 | 39 | --10. List the films where 'Harrison Ford' has appeared - but not in the starring role. [Note: the ord field of casting gives the position of the actor. If ord=1 then this actor is in the starring role] 40 | 41 | SELECT title FROM movie JOIN casting ON movieid = id WHERE actorid = (SELECT id FROM actor WHERE name = 'Harrison Ford') AND ord != 1; 42 | 43 | --11. List the films together with the leading star for all 1962 films. 44 | 45 | SELECT title, name FROM (actor JOIN casting ON id = actorid) JOIN movie ON movie.id = movieid WHERE ord = 1 AND yr = 1962 GROUP BY title; 46 | 47 | --12. Which were the busiest years for 'John Travolta', show the year and the number of movies he made each year for any year in which he made more than 2 movies. 48 | 49 | SELECT yr, COUNT(*) FROM (actor JOIN casting ON actorid = actor.id) JOIN movie ON movieid = movie.id WHERE name = 'John Travolta' GROUP BY yr HAVING COUNT(*) > 2; 50 | 51 | --13. List the film title and the leading actor for all of the films 'Julie Andrews' played in. 52 | 53 | SELECT title, name 54 | FROM (movie JOIN casting ON movie.id=movieid) JOIN actor on actor.id=actorid 55 | WHERE ord=1 AND title IN(SELECT title 56 | FROM (movie JOIN casting ON movie.id=movieid) JOIN actor on actor.id=actorid 57 | WHERE name='Julie Andrews') AND movie.id IN(SELECT movie.id FROM(movie JOIN casting ON movie.id=movieid) JOIN actor on actor.id=actorid WHERE name='Julie Andrews') 58 | ORDER BY name; 59 | 60 | --14. Obtain a list, in alphabetical order, of actors who've had at least 30 starring roles. 61 | 62 | SELECT name FROM (actor JOIN casting ON actorid = actor.id) JOIN movie ON movieid = movie.id WHERE ord = 1 AND actor.id = actorid GROUP BY name HAVING count(*) >= 30; 63 | 64 | --15. List the films released in the year 1978 ordered by the number of actors in the cast. 65 | 66 | SELECT title, COUNT(actorid) FROM (movie JOIN casting ON movieid = movie.id) JOIN actor ON actor.id = actorid WHERE yr = 1978 GROUP BY title ORDER BY COUNT(actorid) DESC, movieid; 67 | 68 | --16. List all the people who have worked with 'Art Garfunkel'. 69 | SELECT name FROM (actor JOIN casting ON actorid = actor.id) JOIN movie ON movieid = movie.id WHERE movieid IN (SELECT movieid FROM (actor JOIN casting ON actorid = actor.id) JOIN movie ON movieid = movie.id WHERE name = 'Art Garfunkel') AND name != 'Art Garfunkel'; 70 | 71 | -------------------------------------------------------------------------------- /8_solutions.sql: -------------------------------------------------------------------------------- 1 | --1. List the teachers who have NULL for their department. 2 | 3 | SELECT teacher.name FROM teacher WHERE dept IS NULL; 4 | 5 | --2. Note the INNER JOIN misses the teachers with no department and the departments with no teacher. 6 | 7 | SELECT teacher.name, dept.name 8 | FROM teacher JOIN dept 9 | ON (teacher.dept=dept.id); 10 | 11 | --3. Use a different JOIN so that all teachers are listed. 12 | 13 | SELECT teacher.name, dept.name FROM teacher LEFT JOIN dept ON (teacher.dept = dept.id); 14 | 15 | --4. Use a different JOIN so that all departments are listed. 16 | 17 | SELECT teacher.name, dept.name FROM teacher RIGHT JOIN dept ON (teacher.dept = dept.id); 18 | 19 | --5. Use COALESCE to print the mobile number. Use the number '07986 444 2266' if there is no number given. Show teacher name and mobile number or '07986 444 2266' 20 | 21 | SELECT name, COALESCE(mobile, '07986 444 2266') AS mobile FROM teacher; 22 | 23 | --6. Use the COALESCE function and a LEFT JOIN to print the teacher name and department name. Use the string 'None' where there is no department. 24 | 25 | SELECT teacher.name, COALESCE(dept.name, 'None') AS department FROM teacher LEFT JOIN dept ON dept.id = teacher.dept; 26 | 27 | --7. Use COUNT to show the number of teachers and the number of mobile phones. 28 | 29 | SELECT COUNT(teacher.name), COUNT(mobile) FROM teacher; 30 | 31 | --8. Use COUNT and GROUP BY dept.name to show each department and the number of staff. Use a RIGHT JOIN to ensure that the Engineering department is listed. 32 | 33 | SELECT dept.name, COUNT(teacher.name) FROM teacher RIGHT JOIN dept ON dept.id = teacher.dept GROUP BY dept.name; 34 | 35 | --9. Use CASE to show the name of each teacher followed by 'Sci' if the teacher is in dept 1 or 2 and 'Art' otherwise. 36 | 37 | SELECT name, CASE WHEN dept IN (1, 2) THEN 'Sci' ELSE 'Art' END FROM teacher; 38 | 39 | --10. Use CASE to show the name of each teacher followed by 'Sci' if the teacher is in dept 1 or 2, show 'Art' if the teacher's dept is 3 and 'None' otherwise. 40 | 41 | SELECT name, CASE WHEN dept IN (1, 2) THEN 'Sci' WHEN dept = 3 THEN 'Art' ELSE 'None' END from teacher; -------------------------------------------------------------------------------- /9_solutions.sql: -------------------------------------------------------------------------------- 1 | --1. How many stops are in the database. 2 | 3 | SELECT COUNT(id) AS stops FROM stops; 4 | 5 | --2 Find the id value for the stop 'Craiglockhart' 6 | 7 | SELECT stop FROM route JOIN stops ON id = stop WHERE stops.name = 'Craiglockhart' GROUP BY stop; 8 | 9 | --3. Give the id and the name for the stops on the '4' 'LRT' service. 10 | 11 | SELECT DISTINCT stop, stops.name FROM route JOIN stops ON stop = stops.id WHERE num = 4 AND company = 'LRT'; 12 | 13 | --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. 14 | 15 | SELECT company, num, COUNT(*) 16 | FROM route WHERE stop=149 OR stop=53 17 | GROUP BY company, num HAVING COUNT(*) = 2; 18 | 19 | --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. 20 | 21 | SELECT a.company, a.num, a.stop, b.stop 22 | FROM route a JOIN route b ON 23 | (a.company=b.company AND a.num=b.num) 24 | WHERE a.stop=53 AND b.stop = 149; 25 | 26 | --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' 27 | 28 | SELECT a.company, a.num, stopa.name, stopb.name 29 | FROM route a JOIN route b ON 30 | (a.company=b.company AND a.num=b.num) 31 | JOIN stops stopa ON (a.stop=stopa.id) 32 | JOIN stops stopb ON (b.stop=stopb.id) 33 | WHERE stopa.name='Craiglockhart' AND stopb.name='London Road'; 34 | 35 | --7. Give a list of all the services which connect stops 115 and 137 ('Haymarket' and 'Leith') 36 | 37 | SELECT DISTINCT a.company, a.num FROM route a JOIN route b ON 38 | (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='Haymarket' AND stopb.name='Leith'; 42 | 43 | --8. Give a list of the services which connect the stops 'Craiglockhart' and 'Tollcross' 44 | 45 | SELECT DISTINCT a.company, a.num FROM route a JOIN route b ON 46 | (a.company=b.company AND a.num=b.num) 47 | JOIN stops stopa ON (a.stop=stopa.id) 48 | JOIN stops stopb ON (b.stop=stopb.id) 49 | WHERE stopa.name='Craiglockhart' AND stopb.name='Tollcross'; 50 | 51 | --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. 52 | 53 | SELECT stopb.name, a.company, a.num FROM route a JOIN route b ON 54 | (a.company=b.company AND a.num=b.num) 55 | JOIN stops stopa ON (a.stop=stopa.id) 56 | JOIN stops stopb ON (b.stop=stopb.id) 57 | WHERE stopa.name='Craiglockhart' AND a.company = 'LRT' ; 58 | 59 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SQL Zoo 2 | Answers to all tutorials from SQLZoo; content from: http://sqlzoo.net/wiki/SQL_Tutorial. 3 | -------------------------------------------------------------------------------- /README.md~: -------------------------------------------------------------------------------- 1 | # SQL_Zoo 2 | --------------------------------------------------------------------------------