My solutions to all of the main 9 and 4 additional topics posted on the sqlzoo.net tutorial.
All answers were 💯 percent correct and generated correct feedback on the day they were uploaded.
Feel free to use and if u have any suggestions don't hesitate to contact me . 😃
- SELECT basics
- SELECT from world
- SELECT from nobel
- SELECT in SELECT
- SUM and COUNT
- JOIN
- More JOIN
- Using NULL
- Self JOIN
Additionally:
- The example uses a WHERE clause to show the population of 'France'. Note that strings (pieces of text that are data) should be in 'single quotes'; Modify it to show the population of Germany
SELECT population
FROM world
WHERE name = 'Germany';
- Checking a list: The word IN allows us to check if an item is in a list. The example shows the name and population for the countries 'Brazil', 'Russia', 'India' and 'China'. Show the name and the population for 'Sweden', 'Norway' and 'Denmark'.
SELECT name, population
FROM world
WHERE name IN ('Sweden', 'Norway', 'Denmark');
- Which countries are not too small and not too big? BETWEEN allows range checking (range specified is inclusive of boundary values). The example below shows countries with an area of 250,000-300,000 sq. km. Modify it to show the country and the area for countries with an area between 200,000 and 250,000.
SELECT name, area
FROM world
WHERE area BETWEEN 200000 AND 250000;
- Read the notes about this table. Observe the result of running this SQL command to show the name, continent and population of all countries.
SELECT name, continent, population
FROM world;
- How to use WHERE to filter records. Show the name for the countries that have a population of at least 200 million. 200 million is 200000000, there are eight zeros.
SELECT name
FROM world
WHERE population > 200000000;
- Give the name and the per capita GDP for those countries with a population of at least 200 million.
SELECT name, gdp / population
FROM world
WHERE population >= 200000000;
- Show the name and population in millions for the countries of the continent 'South America'. Divide the population by 1000000 to get population in millions.
SELECT name, population / 1000000
FROM world
WHERE continent = 'South America';
- Show the name and population for France, Germany, Italy
SELECT name, population
FROM world
WHERE name IN ('France', 'Germany', 'Italy');
- Show the countries which have a name that includes the word 'United'
SELECT name
FROM world
WHERE name LIKE '%United%';
- Two ways to be big: A country is big if it has an area of more than 3 million sq km or it has a population of more than 250 million. Show the countries that are big by area or big by population. Show name, population and area.
SELECT name, population, area
FROM world
WHERE area > 3000000
OR population > 250000000;
-
Exclusive OR (XOR). Show the countries that are big by area (more than 3 million) or big by population (more than 250 million) but not both. Show name, population and area.
- Australia has a big area but a small population, it should be included.
- Indonesia has a big population but a small area, it should be included.
- China has a big population and big area, it should be excluded. United Kingdom has a small population and a small area, it should be excluded.
SELECT name, population, area
FROM world
WHERE ((population > 250000000) OR (area > 3000000))
AND NOT ((population > 250000000) AND (area > 3000000));
COMMENT: 'XOR' operator is not supported in sqlzoo.net interpreter
- Show the name and population in millions and the GDP in billions for the countries of the continent 'South America'. Use the ROUND function to show the values to two decimal places. For South America show population in millions and GDP in billions both to 2 decimal places.
SELECT name, ROUND(population / 1000000, 2), ROUND(gdp / 1000000000, 2)
FROM world
WHERE continent = 'South America';
- Show the name and per-capita GDP for those countries with a GDP of at least one trillion (1000000000000; that is 12 zeros). Round this value to the nearest 1000. Show per-capita GDP for the trillion dollar countries to the nearest $1000.
SELECT name, ROUND((gdp / population), -3)
FROM world
WHERE gdp >= 1000000000000;
-
Greece has capital Athens. Each of the strings 'Greece', and 'Athens' has 6 characters. Show the name and capital where the name and the capital have the same number of characters.
- You can use the LENGTH function to find the number of characters in a string
SELECT name, capital
FROM world
WHERE LENGTH(name) = LENGTH(capital);
-
The capital of Sweden is Stockholm. Both words start with the letter 'S'. Show the name and the capital where the first letters of each match. Don't include countries where the name and the capital are the same word.
- You can use the function LEFT to isolate the first character.
- You can use <> as the NOT EQUALS operator.
SELECT name, capital
FROM world
WHERE (LEFT(name,1) = LEFT (capital,1))
AND (name <> capital);
-
Equatorial Guinea and Dominican Republic have all of the vowels (a e i o u) in the name. They don't count because they have more than one word in the name. Find the country that has all the vowels and no spaces in its name.
- You can use the phrase name NOT LIKE '%a%' to exclude characters from your results.
- The query shown misses countries like Bahamas and Belarus because they contain at least one 'a'
SELECT name
FROM world
WHERE name LIKE '%a%'
AND name LIKE '%e%'
AND name LIKE '%i%'
AND name LIKE '%o%'
AND name LIKE '%u%'
AND name NOT LIKE '% %';
- Change the query shown so that it displays Nobel prizes for 1950.
SELECT yr, subject, winner
FROM nobel
WHERE yr = 1950;
- Show who won the 1962 prize for Literature.
SELECT winner
FROM nobel
WHERE yr = 1962
AND subject = 'Literature';
- Show the year and subject that won 'Albert Einstein' his prize.
SELECT yr, subject
FROM nobel
WHERE winner = 'Albert Einstein';
- Give the name of the 'Peace' winners since the year 2000, including 2000.
SELECT winner
FROM nobel
WHERE subject = 'Peace'
AND yr >= 2000;
- Show all details (yr, subject, winner) of the Literature prize winners for 1980 to 1989 inclusive.
SELECT winner
FROM nobel
WHERE subject = 'Peace'
AND yr >= 2000;
-
Show all details of the presidential winners:
- Theodore Roosevelt
- Woodrow Wilson
- Jimmy Carter
- Barack Obama
SELECT *
FROM nobel
WHERE winner IN ('Theodore Roosevelt', 'Woodrow Wilson', 'Jimmy Carter', 'Barack Obama');
- Show the winners with first name John
SELECT winner
FROM nobel
WHERE winner LIKE 'John%';
- Show the year, subject, and name of Physics winners for 1980 together with the Chemistry winners for 1984.
SELECT yr, subject, winner
FROM nobel
WHERE subject = 'Physics' AND yr = 1980
OR subject = 'Chemistry' AND yr = 1984;
- Show the year, subject, and name of winners for 1980 excluding Chemistry and Medicine
SELECT yr, subject, winner
FROM nobel
WHERE yr = 1980
AND subject NOT IN ('Chemistry', 'Medicine');
- Show year, subject, and name of people 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)
SELECT yr, subject, winner
FROM nobel
WHERE subject = 'Medicine' AND yr < 1910
OR subject = 'Literature' AND yr >= 2004;
- Find all details of the prize won by PETER GRÜNBERG
SELECT *
FROM nobel
WHERE winner = 'PETER GRÜNBERG';
- Find all details of the prize won by EUGENE O'NEILL
SELECT *
FROM nobel
WHERE winner = 'EUGENE O''NEILL';
- Knights in order. List the winners, year and subject where the winner starts with Sir. Show the the most recent first, then by name order.
SELECT winner, yr, subject
FROM nobel
WHERE winner LIKE 'Sir%'
ORDER BY yr DESC, winner;
- The expression subject IN ('Chemistry','Physics') can be used as a value - it will be 0 or 1. Show the 1984 winners and subject ordered by subject and winner name; but list Chemistry and Physics last.
SELECT winner, subject
FROM nobel
WHERE yr = 1984
ORDER BY CASE WHEN subject IN ('Physics', 'Chemistry') THEN 1 ELSE 0 END, subject, winner;
- List each country name where the population is larger than that of 'Russia'.
SELECT name
FROM world
WHERE population >
(SELECT population
FROM world
WHERE name = 'Russia');
- Show the countries in Europe with a per capita GDP greater than 'United Kingdom'.
SELECT name
FROM world
WHERE continent = 'Europe'
AND gdp / population >
(SELECT gdp / population
FROM world
WHERE name = 'United Kingdom');
- List the name and continent of countries in the continents containing either Argentina or Australia. Order by name of the country.
SELECT name, continent
FROM world
WHERE continent IN (SELECT continent FROM world WHERE name IN ('Argentina', 'Australia'))
ORDER BY name;
- Which country has a population that is more than Canada but less than Poland? Show the name and the population.
SELECT name, population
FROM world
WHERE population > (SELECT population FROM world WHERE name = 'Canada')
AND population < (SELECT population FROM world WHERE name = 'Poland')
- Germany (population 80 million) has the largest population of the countries in Europe. Austria (population 8.5 million) has 11% of the population of Germany. Show the name and the population of each country in Europe. Show the population as a percentage of the population of Germany. The format should be Name, Percentage
SELECT name,
CONCAT(CONVERT(DECIMAL, population / (SELECT population FROM world WHERE name = 'Germany') * 100),
'%') AS percentage
FROM world
WHERE continent = 'Europe';
COMMENT: ROUND(x, 0) gives a lot of '0' after the decimal point you can get rid of by using
additional CAST(x as INT) on it, but I think that the solution above is more transparent.
Even that, I also present the solution below:
SELECT name,
CONCAT(CAST(ROUND((population / (SELECT population FROM world WHERE name = 'Germany') * 100), 0) AS INT),
'%') AS percentage
FROM world
WHERE continent = 'Europe';
- Which countries have a GDP greater than every country in Europe? [Give the name only.] (Some countries may have NULL gdp values)
SELECT name
FROM world
WHERE gdp > ALL (SELECT gdp FROM world WHERE gdp > 0 AND continent = 'Europe');
- Find the largest country (by area) in each continent, show the continent, the name and the area:
SELECT continent, name, area
FROM world x
WHERE area >= ALL (SELECT area FROM world y WHERE y.continent = x.continent AND area > 0);
- List each continent and the name of the country that comes first alphabetically.
SELECT continent, name
FROM world x
WHERE name <= ALL (SELECT name FROM world y WHERE x.continent = y.continent);
- 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.
SELECT name, continent, population
FROM world x
WHERE 25000000 > ALL (SELECT population FROM world y WHERE x.continent = y.continent);
- Some countries have populations more than three times that of any of their neighbours (in the same continent). Give the countries and continents.
SELECT name, continent
FROM world x
WHERE population > ALL (SELECT 3 * population FROM world y WHERE x.continent = y.continent AND x.name <> y.name);
- Show the total population of the world.
SELECT SUM(population)
FROM world;
- List all the continents - just once each.
SELECT DISTINCT continent
FROM world;
- Give the total GDP of Africa
SELECT SUM(gdp)
FROM world
WHERE continent = 'Africa';
- How many countries have an area of at least 1000000
SELECT COUNT(*)
FROM world
WHERE area > 1000000;
- What is the total population of ('Estonia', 'Latvia', 'Lithuania')
SELECT SUM(population)
FROM world
WHERE name IN ('Estonia', 'Latvia', 'Lithuania');
- For each continent show the continent and number of countries.
SELECT continent, COUNT(*)
FROM world
GROUP BY continent;
- For each continent show the continent and number of countries with populations of at least 10 million.
SELECT continent, COUNT(*)
FROM world
WHERE population >= 10000000
GROUP BY continent;
- List the continents that have a total population of at least 100 million.
SELECT continent
FROM world
GROUP BY continent
HAVING SUM(population) >= 100000000;
- The first example shows the goal scored by a player with the last name 'Bender'. The * says to list all the columns in the table - a shorter way of saying matchid, teamid, player, gtime. Modify it to show the matchid and player name for all goals scored by Germany. To identify German players, check for: teamid = 'GER'
SELECT matchid, player
FROM goal
WHERE teamid = 'GER';
- From the previous query you can see that Lars Bender's scored a goal in game 1012. Now we want to know what teams were playing in that match. Notice that the matchid column in the goal table corresponds to the id column in the game table. We can look up information about game 1012 by finding that row in the game table. Show id, stadium, team1, team2 for just game 1012
SELECT id, stadium, team1, team2
FROM game
WHERE id = 1012;
-
You can combine the two steps into a single query with a JOIN.
SELECT * FROM game JOIN goal ON (id = matchid)
The FROM clause says to merge data from the goal table with that from the game table. The ON says how to figure out which rows in game go with which rows in goal - the matchid from goal must match id from game. (If we wanted to be more clear/specific we could say ON (game.id=goal.matchid). The code below shows the player (from the goal) and stadium name (from the game table) for every goal scored. Modify it to show the player, teamid, stadium and mdate for every German goal.
SELECT player, teamid, stadium, mdate
FROM game
JOIN goal ON (id = matchid)
WHERE teamid = 'GER';
- Use the same JOIN as in the previous question. Show the team1, team2 and player for every goal scored by a player called Mario player LIKE 'Mario%'
SELECT team1, team2, player
FROM game
JOIN goal ON (id = matchid)
WHERE player LIKE 'Mario%';
- The table eteam gives details of every national team including the coach. You can JOIN goal to eteam using the phrase goal JOIN eteam on teamid=id. Show player, teamid, coach, gtime for all goals scored in the first 10 minutes gtime< =10
SELECT player, teamid, coach, gtime
FROM goal
JOIN eteam ON (teamid = id)
WHERE gtime <= 10;
- To JOIN game with eteam you could use either game JOIN eteam ON (team1=eteam.id) or game JOIN eteam ON ( team2=eteam.id). Notice that because id is a column name in both game and eteam you must specify eteam.id instead of just id. List the dates of the matches and the name of the team in which 'Fernando Santos' was the team1 coach.
SELECT mdate, teamname
FROM game
JOIN eteam ON (team1 = eteam.id)
WHERE coach = 'Fernando Santos';
- List the player for every goal scored in a game where the stadium was 'National Stadium, Warsaw'
SELECT player
FROM goal
JOIN game ON (id = matchid)
WHERE stadium = 'National Stadium, Warsaw';
- The example query shows all goals scored in the Germany-Greece quarterfinal. Instead show the name of all players who scored a goal against Germany.
SELECT DISTINCT player
FROM game
JOIN goal ON (id = matchid)
WHERE (team1 = 'GER' OR team2 = 'GER')
AND teamid != 'GER';
- Show teamname and the total number of goals scored.
SELECT teamname, COUNT(*)
FROM eteam
JOIN goal ON (id = teamid)
GROUP BY teamname;
- Show the stadium and the number of goals scored in each stadium.
SELECT stadium, COUNT(*)
FROM game
JOIN goal ON (id = matchid)
GROUP BY stadium;
- For every match involving 'POL', show the matchid, date and the number of goals scored.
SELECT matchid, mdate, COUNT(*)
FROM game
JOIN goal ON (id = matchid)
WHERE team1 = 'POL'
OR team2 = 'POL'
GROUP BY mdate, matchid;
- For every match where 'GER' scored, show matchid, match date and the number of goals scored by 'GER'
SELECT matchid, mdate, COUNT(*)
FROM goal
JOIN game ON (matchid = id)
WHERE teamid = 'GER'
GROUP BY matchid, mdate;
- List every match with the goals scored by each team as shown. This will use "CASE WHEN" which has not been explained in any previous exercises. Notice in the query given every goal is listed. If it was a team1 goal then a 1 appears in score1, otherwise there is a 0. You could SUM this column to get a count of the goals scored by team1. Sort your result by mdate, matchid, team1 and team2.
SELECT mdate,
team1,
SUM(CASE WHEN teamid = team1 THEN 1 ELSE 0 END) AS score1,
team2,
SUM(CASE WHEN teamid = team2 THEN 1 ELSE 0 END) AS score2
FROM game
LEFT JOIN goal ON (id = matchid)
GROUP BY mdate, matchid, team1, team2
ORDER BY mdate, matchid, team1, team2;
- List the films where the yr is 1962 [Show id, title]
SELECT id, title
FROM movie
WHERE yr = 1962;
- Give year of 'Citizen Kane'.
SELECT yr
FROM movie
WHERE title = 'Citizen Kane';
- 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.
SELECT id, title, yr
FROM movie
WHERE title LIKE '%Star Trek%'
ORDER BY yr;
- What id number does the actor 'Glenn Close' have?
SELECT id
FROM actor
WHERE name = 'Glenn Close';
- What is the id of the film 'Casablanca'
SELECT id
FROM movie
WHERE title = 'Casablanca';
- Obtain the cast list for 'Casablanca'. Use movieid=11768, (or whatever value you got from the previous question)
SELECT name
FROM casting
JOIN actor ON (actorid = id)
WHERE movieid = 27;
- Obtain the cast list for the film 'Alien'
SELECT name
FROM casting
JOIN actor ON (actorid = actor.id)
JOIN movie ON (movieid = movie.id)
WHERE title = 'Alien';
- List the films in which 'Harrison Ford' has appeared
SELECT title
FROM casting
JOIN movie ON (movieid = movie.id)
JOIN actor ON (actorid = actor.id)
WHERE name = 'Harrison Ford';
- 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]
SELECT title
FROM casting
JOIN movie ON (movieid = movie.id)
JOIN actor ON (actorid = actor.id)
WHERE name = 'Harrison Ford'
AND ord != 1;
- List the films together with the leading star for all 1962 films.
SELECT title, name
FROM casting
JOIN movie ON (movieid = movie.id)
JOIN actor ON (actorid = actor.id)
WHERE ord = 1
AND yr = 1962;
- Which were the busiest years for 'Rock Hudson', show the year and the number of movies he made each year for any year in which he made more than 2 movies.
SELECT yr, COUNT(*)
FROM movie
JOIN casting ON (movie.id = movieid)
JOIN actor ON (actorid = actor.id)
WHERE name = 'Rock Hudson'
GROUP BY yr
HAVING COUNT(*) > 2;
- List the film title and the leading actor for all of the films 'Julie Andrews' played in.
SELECT title, name
FROM casting
JOIN movie ON (movieid = movie.id)
JOIN actor ON (actorid = actor.id)
WHERE ord = 1
AND movie.id IN
(SELECT movie.id
FROM movie
JOIN casting ON (movie.id = movieid)
JOIN actor ON (actorid = actor.id)
WHERE actor.name = 'Julie Andrews');
- Obtain a list, in alphabetical order, of actors who've had at least 15 starring roles.
SELECT name
FROM casting
JOIN actor ON (actorid = actor.id)
WHERE ord = 1
GROUP BY name
HAVING COUNT(*) >= 15
ORDER BY name;
- List the films released in the year 1978 ordered by the number of actors in the cast, then by title.
SELECT title, COUNT(*)
FROM casting
JOIN movie ON (movieid = movie.id)
WHERE yr = 1978
GROUP BY movieid, title
ORDER BY COUNT(*) DESC, title;
- List all the people who have worked with 'Art Garfunkel'.
SELECT DISTINCT name
FROM actor
JOIN casting ON (id = actorid)
WHERE name != 'Art Garfunkel'
AND movieid IN (SELECT movieid
FROM casting
JOIN actor ON (actorid = id)
WHERE name = 'Art Garfunkel');
- List the teachers who have NULL for their department.
SELECT name
FROM teacher
WHERE dept IS NULL;
- Note the INNER JOIN misses the teachers with no department and the departments with no teacher.
SELECT teacher.name, dept.name
FROM teacher
INNER JOIN dept ON (dept = dept.id);
- Use a different JOIN so that all teachers are listed.
SELECT teacher.name, dept.name
FROM teacher
LEFT JOIN dept ON (dept = dept.id);
- Use a different JOIN so that all departments are listed.
SELECT teacher.name, dept.name
FROM teacher
RIGHT JOIN dept ON (dept = dept.id);
- 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'
SELECT name, COALESCE(mobile, '07986 444 2266')
FROM teacher;
- 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.
SELECT teacher.name, COALESCE(dept.name, 'None')
FROM teacher
LEFT JOIN dept ON (dept = dept.id);
- Use COUNT to show the number of teachers and the number of mobile phones.
SELECT COUNT(name), COUNT(mobile)
FROM teacher;
- 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.
SELECT dept.name, COUNT(teacher.name)
FROM teacher
RIGHT JOIN dept ON (dept = dept.id)
GROUP BY dept.name;
- Use CASE to show the name of each teacher followed by 'Sci' if the teacher is in dept 1 or 2 and 'Art' otherwise.
SELECT name,
CASE WHEN dept IN (1, 2) THEN 'Sci' ELSE 'Art' END
FROM teacher;
- 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.
SELECT name,
CASE WHEN dept IN (1, 2) THEN 'Sci' WHEN dept = 3 THEN 'Art' ELSE 'None' END
FROM teacher;
- How many stops are in the database.
SELECT COUNT(*)
FROM stops;
- Find the id value for the stop 'Craiglockhart'
SELECT id
FROM stops
WHERE name = 'Craiglockhart';
- Give the id and the name for the stops on the '4' 'LRT' service.
SELECT id, name
FROM stops
JOIN route ON (id = stop)
WHERE num = '4'
AND company = 'LRT';
- 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.
SELECT company, num, COUNT(*)
FROM route
WHERE stop = 149
OR stop = 53
GROUP BY company, num
HAVING COUNT(*) = 2;
- 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.
SELECT a.company, a.num, a.stop, b.stop
FROM route AS a
JOIN route AS b ON (a.num = b.num)
WHERE a.stop = 53
AND b.stop = 149;
- 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'
SELECT a.company, a.num, stopa.name, stopb.name
FROM route AS a
JOIN route AS b ON (a.num = b.num)
JOIN stops AS stopa ON (a.stop = stopa.id)
JOIN stops AS stopb ON (b.stop = stopb.id)
WHERE stopa.name = 'Craiglockhart'
AND stopb.name = 'London Road';
- Give a list of all the services which connect stops 115 and 137 ('Haymarket' and 'Leith')
SELECT DISTINCT a.company, a.num
FROM route AS a
JOIN route AS b ON (a.num = b.num)
WHERE a.stop = 115
AND b.stop = 137;
- Give a list of the services which connect the stops 'Craiglockhart' and 'Tollcross'
SELECT a.company, a.num
FROM route AS a
JOIN route AS b ON (a.num = b.num)
JOIN stops AS stopa ON (a.stop = stopa.id)
JOIN stops AS stopb ON (b.stop = stopb.id)
WHERE stopa.name = 'Craiglockhart'
AND stopb.name = '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.
SELECT DISTINCT stopb.name, b.company, b.num
FROM route AS a
JOIN route AS b ON (a.num = b.num)
JOIN stops AS stopa ON (a.stop = stopa.id)
JOIN stops AS stopb ON (b.stop = stopb.id)
WHERE a.company = b.company
AND stopa.name = 'Craiglockhart';
- Find the routes involving two buses that can go from Craiglockhart to Lochend. Show the bus no. and company for the first bus, the name of the stop for the transfer, and the bus no. and company for the second bus.
SELECT a.num, a.company, stopb.name, c.num, c.company
FROM route AS a
JOIN route AS b ON (a.company = b.company)
JOIN route AS c ON (b.stop = c.stop)
JOIN route AS d ON (c.company = d.company)
JOIN stops AS stopa ON (a.stop = stopa.id)
JOIN stops AS stopb ON (b.stop = stopb.id)
JOIN stops AS stopc ON (c.stop = stopc.id)
JOIN stops AS stopd ON (d.stop = stopd.id)
WHERE a.num = b.num
AND c.num = d.num
AND stopb.id = stopc.id
AND stopa.name = 'Craiglockhart'
AND stopd.name = 'Lochend';
1.Find the country that start with Y
SELECT name
FROM world
WHERE name LIKE 'Y%';
- Find the countries that end with y
SELECT name
FROM world
WHERE name LIKE '%y';
- Find the countries that contain the letter x
SELECT name
FROM world
WHERE name LIKE '%x%';
- Find the countries that end with land
SELECT name
FROM world
WHERE name LIKE '%land';
- Find the countries that start with C and end with ia
SELECT name
FROM world
WHERE name LIKE 'C%ia';
- Find the country that has oo in the name
SELECT name
FROM world
WHERE name LIKE '%oo%'
- Find the countries that have three or more a in the name
SELECT name
FROM world
WHERE name LIKE '%a%a%a%';
- Find the countries that have "t" as the second character.
SELECT name
FROM world
WHERE name LIKE '_t%';
- Find the countries that have two "o" characters separated by two others.
SELECT name
FROM world
WHERE name LIKE '%o__o%';
- Find the countries that have exactly four characters.
SELECT name
FROM world
WHERE name LIKE '____';
- Find the country where the name is the capital city.
SELECT name
FROM world
WHERE name = capital;
- Find the country where the capital is the country plus "City".
SELECT name
FROM world
WHERE capital = CONCAT(name, ' City');
- Find the capital and the name where the capital includes the name of the country.
SELECT capital, name
FROM world
WHERE capital LIKE CONCAT('%', name, '%');
- Find the capital and the name where the capital is an extension of name of the country.
SELECT capital, name
FROM world
WHERE capital LIKE CONCAT(name, '%')
AND capital <> name;
- Show the name and the extension where the capital is an extension of name of the country.
SELECT name, REPLACE(capital, name, '')
FROM world
WHERE capital LIKE CONCAT(name, '%')
AND capital <> name;
-
The example shows the number who responded for:
- question 1
- at 'Edinburgh Napier University'
- studying '(8) Computer Science'
Show the the percentage who STRONGLY AGREE
SELECT A_STRONGLY_AGREE
FROM nss
WHERE question = 'Q01'
AND institution = 'Edinburgh Napier University'
AND subject = '(8) Computer Science';
- Show the institution and subject where the score is at least 100 for question 15.
SELECT institution, subject
FROM nss
WHERE score >= 100
AND question = 'Q15';
- Show the institution and score where the score for '(8) Computer Science' is less than 50 for question 'Q15'
SELECT institution, score
FROM nss
WHERE question = 'Q15'
AND score < 50
AND subject = '(8) Computer Science';
- Show the subject and total number of students who responded to question 22 for each of the subjects '(8) Computer Science' and '(H) Creative Arts and Design'.
SELECT subject, SUM(response)
FROM nss
WHERE question = 'Q22'
AND subject IN ('(8) Computer Science', '(H) Creative Arts and Design')
GROUP BY subject;
- Show the subject and total number of students who A_STRONGLY_AGREE to question 22 for each of the subjects '(8) Computer Science' and '(H) Creative Arts and Design'.
SELECT subject,
SUM(response * A_STRONGLY_AGREE / 100)
FROM nss
WHERE question = 'Q22'
AND subject IN ('(8) Computer Science', '(H) Creative Arts and Design')
GROUP BY subject;
- Show the percentage of students who A_STRONGLY_AGREE to question 22 for the subject '(8) Computer Science' show the same figure for the subject '(H) Creative Arts and Design'. Use the ROUND function to show the percentage without decimal places.
SELECT subject,
ROUND(SUM(response * A_STRONGLY_AGREE) / SUM(response), 0)
FROM nss
WHERE question = 'Q22'
AND subject IN ('(8) Computer Science', '(H) Creative Arts and Design')
GROUP BY subject;
- Show the average scores for question 'Q22' for each institution that include 'Manchester' in the name. The column score is a percentage - you must use the method outlined above to multiply the percentage by the response and divide by the total response. Give your answer rounded to the nearest whole number.
SELECT institution,
ROUND(SUM(response * score) / SUM(response), 0)
FROM nss
WHERE question = 'Q22'
AND institution LIKE '%Manchester%'
GROUP BY institution;
- Show the institution, the total sample size and the number of computing students for institutions in Manchester for ' Q01'.
SELECT institution,
SUM(sample),
SUM(CASE WHEN subject = '(8) Computer Science' THEN sample ELSE 0 END)
FROM nss
WHERE question = 'Q01'
AND institution LIKE '%Manchester%'
GROUP BY institution;
- Show the lastName, party and votes for the constituency 'S14000024' in 2017.
SELECT lastName, party, votes
FROM ge
WHERE constituency = 'S14000024'
AND yr = 2017
ORDER BY votes DESC;
- Show the party and RANK for constituency S14000024 in 2017. List the output by party
SELECT party,
votes,
RANK() OVER (ORDER BY votes DESC) as posn
FROM ge
WHERE constituency = 'S14000024 '
AND yr = 2017
ORDER BY party;
- Use PARTITION to show the ranking of each party in S14000021 in each year. Include yr, party, votes and ranking (the party with the most votes is 1).
SELECT yr,
party,
votes,
RANK() OVER (PARTITION BY yr ORDER BY votes DESC) as posn
FROM ge
WHERE constituency = 'S14000021'
ORDER BY party, yr;
- Use PARTITION BY constituency to show the ranking of each party in Edinburgh in 2017. Order your results so the winners are shown first, then ordered by constituency.
SELECT constituency,
party,
votes,
RANK() OVER (PARTITION BY constituency ORDER BY votes desc) as posn
FROM ge
WHERE constituency BETWEEN 'S14000021' AND 'S14000026'
AND yr = 2017
ORDER BY posn, constituency;
- Show the parties that won for each Edinburgh constituency in 2017.
SELECT constituency, party
FROM (SELECT constituency,
party,
votes,
RANK() OVER (PARTITION BY constituency ORDER BY votes DESC) as posn
FROM ge
WHERE constituency BETWEEN 'S14000021' AND 'S14000026'
AND yr = 2017) x
WHERE x.posn = 1;
COMMENT: or without RANK OVER() below
SELECT constituency, party
FROM ge x
WHERE constituency BETWEEN 'S14000021' AND 'S14000026'
AND yr = 2017
AND votes > ALL
(SELECT votes
FROM ge y
WHERE y.constituency = x.constituency
AND y.party <> x.party
AND yr = 2017);
- Show how many seats for each party in Scotland in 2017.
SELECT party, COUNT(*)
FROM (SELECT constituency,
party,
votes,
RANK() OVER (PARTITION BY constituency ORDER BY votes DESC) as posn
FROM ge
WHERE constituency LIKE 'S%'
AND yr = 2017) x
WHERE x.posn = 1
GROUP BY x.party;
- Modify the query to show data from Spain
SELECT name, DAY (whn),
confirmed, deaths, recovered
FROM covid
WHERE name = 'Spain'
AND MONTH (whn) = 3
ORDER BY whn;
- Modify the query to show confirmed for the day before.
SELECT name, DAY (whn), confirmed,
LAG(confirmed, 1) OVER (PARTITION by name ORDER BY whn)
FROM covid
WHERE name = 'Italy' AND MONTH (whn) = 3
ORDER BY whn;
- Show the number of new cases for each day, for Italy, for March.
SELECT name,
DAY (whn) AS day,
confirmed - LAG(confirmed, 1) OVER (PARTITION BY name ORDER BY whn) AS newcases
FROM covid
WHERE name = 'Italy'
AND MONTH (whn) = 3
ORDER BY whn;
-
Show the number of new cases in Italy for each week - show Monday only.
BUG: 'DATE_FORMAT' is not a recognized built-in function name. It doesn't even show what the correct answer is.
-
Show the number of new cases in Italy for each week - show Monday only.
BUG: 'DATE_FORMAT' is not a recognized built-in function name. It doesn't even show what the correct answer is.
-
The query shown shows the number of confirmed cases together with the world ranking for cases. United States has the highest number, Spain is number 2... Notice that while Spain has the second highest confirmed cases, Italy has the second highest number of deaths due to the virus. Include the ranking for the number of deaths in the table.
SELECT name,
confirmed,
RANK() OVER (ORDER BY confirmed DESC) AS rc, deaths,
RANK() OVER (ORDER BY deaths DESC) AS rd
FROM covid
WHERE whn = '2020-04-20'
ORDER BY confirmed DESC;
-
The query shown includes a JOIN t the world table so we can access the total population of each country and calculate infection rates (in cases per 100,000). Show the infect rate ranking for each country. Only include countries with a population of at least 10 million.
BUG: just error, no query works
-
For each country that has had at last 1000 new cases in a single day, show the date of the peak number of new cases.
BUG: 'DATE_FORMAT' is not a recognized built-in function name. It doesn't even show what the correct answer is.