Solutions to exercise set tutorials 0-9 from SQL Zoo using MySQL
.
Some simple queries to get you started
name | continent | area | population | gdp |
---|---|---|---|---|
Afghanistan | Asia | 652230 | 25500100 | 20343000000 |
Albania | Europe | 28748 | 2831741 | 12960000000 |
Algeria | Africa | 2381741 | 37100000 | 188681000000 |
Andorra | Europe | 468 | 78115 | 3712000000 |
Angola | Africa | 1246700 | 20609294 | 100990000000 |
.... |
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.
Solution:
SELECT name, area FROM world
WHERE area BETWEEN 200000 AND 250000;
Some pattern matching queries.
name | continent |
---|---|
Afghanistan | Asia |
Albania | Europe |
Algeria | Africa |
Andorra | Europe |
Angola | Africa |
.... |
You can use WHERE
name LIKE
'B%' to find the countries that start with "B". (The % is a wild-card it can match any characters)
SELECT name FROM world
WHERE name LIKE 'Y%';
SELECT name FROM world
WHERE name LIKE '%y';
Luxembourg has an x - so does one other country. List them both.
SELECT name FROM world
WHERE name LIKE '%x%';
Iceland, Switzerland end with land - but are there others?
SELECT name FROM world
WHERE name LIKE '%land';
Columbia starts with a C and ends with ia - there are two more like this.
SELECT name FROM world
WHERE name LIKE 'C%ia';
Greece has a double e - who has a double o?
SELECT name FROM world
WHERE name LIKE '%oo%';
Bahamas has three a - who else?
SELECT name FROM world
WHERE name LIKE '%a%a%a%';
India and Angola have an n as the second character. You can use the underscore (_) as a single character wildcard.
SELECT name FROM world
WHERE name LIKE '_t%'
ORDER BY name;
Lesotho and Moldova both have two o characters separated by two other characters.
SELECT name FROM world
WHERE name LIKE '%o__o%';
Cuba and Togo have four character names.
SELECT name FROM world
WHERE name LIKE '____';
The capital of Luxembourg is Luxembourg. Show all the countries where the capital is the same as the name of the country
SELECT name
FROM world
WHERE name = capital;
The capital of Mexico is Mexico City. Show all the countries where the capital has the country together with the word "City".
SELECT name
FROM world
WHERE capital LIKE CONCAT(name, ' ', 'City');
SELECT capital, name
FROM world
WHERE capital LIKE concat('%', name, '%');
You should include Mexico City as it is longer than Mexico. You should not include Luxembourg as the capital is the same as the country.
SELECT capital, name
FROM world
WHERE (capital LIKE CONCAT(name, '%')) AND (capital != name);
For Monaco-Ville the name is Monaco and the extension is ** -Ville**. You can use the SQL function REPLACE
.
SELECT name, REPLACE(capital, name ,'')
FROM world
WHERE (capital LIKE CONCAT(name, '%')) AND (capital != name);
In which we query the World country profile 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;
2. 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;
3. 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;
Divide the population by 1000000 to get population in millions.
SELECT name, population/1000000
FROM world
WHERE continent = 'South America';
SELECT name, population
FROM world
WHERE name in ('France', 'Germany', 'Italy');
SELECT name
FROM world
WHERE name LIKE '%United%';
- Show name, population and area.
- 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.
SELECT name, population, area
FROM world
WHERE (area > 3000000) OR (population > 250000000);
8. 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 (area > 3000000) XOR (population > 250000000);
9. 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';
10. 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/1000,0)*1000
FROM world
WHERE gdp >= 1000000000000;
- Greece has capital Athens. Each of the strings 'Greece', and 'Athens' has 6 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);
- Don't include countries where the name and the capital are the same word.
- The capital of Sweden is Stockholm. Both words start with the letter 'S'.
- 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.
- You can use the phrase name NOT LIKE '%a%' to exclude characters from your results.
SELECT name
FROM world
WHERE name NOT LIKE '% %'
AND name LIKE '%a%'
AND name LIKE '%e%'
AND name LIKE '%i%'
AND name LIKE '%o%'
AND name LIKE '%u%'
Additional practice of the basic features using a table of Nobel Prize winners.
yr | subject | winner | ||
---|---|---|---|---|
1960 | Chemistry | Willard F. Libby | ||
1960 | Literature | Saint-John Perse | ||
1960 | Medicine | Sir Frank Macfarlane Burnet | ||
1960 | Medicine | Peter Madawar | ||
... |
SELECT yr, subject, winner
FROM nobel
WHERE yr = 1950;
SELECT winner
FROM nobel
WHERE yr = 1962 AND subject = 'literature';
SELECT yr, subject
FROM nobel
WHERE winner = 'Albert Einstein';
SELECT winner
FROM nobel
WHERE (subject = 'peace') AND (yr >= 2000);
5. Show all details (yr, subject, winner) of the literature prize winners for 1980 to 1989 inclusive.
SELECT yr, subject, winner
FROM nobel
WHERE (subject = 'literature') AND (yr BETWEEN 1980 AND 1989);
- Theodore Roosevelt
- Thomas Woodrow Wilson
- Jimmy Carter
- Barack Obama
SELECT *
FROM nobel
WHERE winner IN ('Theodore Roosevelt',
'Woodrow Wilson',
'Jimmy Carter',
'Barack Obama')
SELECT winner FROM nobel
WHERE winner LIKE 'John %'
8. 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 ((yr = 1980) AND (subject = 'physics')) OR
((yr = 1984) AND (subject = 'chemistry'));
SELECT yr, subject, winner FROM nobel
WHERE (yr = 1980) AND ((subject != 'chemistry') AND (subject != 'medicine'));
10. 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);
- The u in his name has an umlaut.
- You may find this link useful https://en.wikipedia.org/wiki/%C3%9C#Keyboarding
SELECT *
FROM nobel
WHERE winner LIKE 'PETER GRรNBERG';
- You can't put a single quote in a quote string directly.
- You can use two single quotes within a quoted string.
SELECT *
FROM nobel
WHERE winner LIKE 'EUGENE O''NEILL';
13. 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;
14. Show the 1984 winners and subject ordered by subject and winner name; but list chemistry and physics last.
- The expression subject IN ('chemistry','physics') can be used as a value - it will be 0 or 1.
SELECT winner, subject
FROM nobel
WHERE yr = 1984
ORDER BY subject IN ('chemistry', 'physics'), subject, winner
In which we form queries using other queries.
- world(name, continent, area, population, gdp)
SELECT name
FROM world
WHERE population >
(SELECT population FROM world
WHERE name='Russia');
- The per capita GDP is the gdp/population
SELECT name
FROM world
WHERE gdp/population >
(SELECT gdp/population
FROM world
WHERE name = 'United Kingdom') AND
(continent = 'Europe');
3. 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;
- Show the name and the population.
SELECT name, population
FROM world
WHERE population > (
SELECT population FROM world
WHERE name = 'United Kingdom') AND
population < (
SELECT population FROM world
WHERE name = 'Germany');
- Show the population as a percentage of the population of Germany.
- 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.
- You can use the function ROUND to remove the decimal places.
- You can use the function
CONCAT
to add the percentage symbol.
SELECT name,
CONCAT(ROUND(100*(population/(
SELECT population
FROM world
WHERE name = 'Germany')
), 0),'%') as percentage
FROM world
WHERE continent = 'Europe';
- Give the name only.
- Some countries may have
NULL
gdp values.
SELECT name
FROM world
WHERE gdp > ALL(SELECT gdp
FROM world
WHERE (Continent = 'Europe') AND
(gdp>0)
);
- A correlated (or synchronized) subquery works like a nested loop: the subquery only has access to rows related to a single record at a time in the outer query. The technique relies on table aliases to identify two different uses of the same table, one in the outer query and the other in the subquery.
- One way to interpret the line in the
WHERE
clause that references the two table is โโฆ where the correlated values are the sameโ. - In the example provided, you would say โselect the country details from world where the population is greater than or equal to the population of all countries where the continent is the sameโ.
- In the solution below, we would say: 'select the country details from world where the area is greater than equal to the area of all countries where the continent is the same'
SELECT continent, name, area
FROM world x
WHERE area >= ALL(
SELECT area FROM world y
WHERE y.continent=x.continent)
- Nested
SELECT
is picking all the countries with the same continent - Outer
SELECT
is keeping the countries where the name is less than the country names in the NestedSELECT
, so it will filter down to the lowest, or first alphabetical
SELECT continent, name
FROM world x
WHERE name <= ALL(
SELECT name
FROM world y
WHERE y.continent = x.continent
);
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.
- Starting from inner most
SELECT
... - Select all the country populations that are on the same continent
- Keep only those distinct countries whose populations are <= 25000000
- Get the country details of those countries that are in those continents
SELECT name, continent, population
FROM world z
WHERE z.continent IN (
SELECT DISTINCT continent
FROM world x
WHERE 25000000 >
ALL(SELECT population
FROM world y
WHERE x.continent = y.continent)
);
10. Some countries have populations more than three times that of all of their neighbours (in the same continent). Give the countries and continents.
- Starting from inner most
SELECT
... - Select all other countries in the same continent and multiply those populations by 3
- For each country, keep only those country details whose populations are greater than that entire list
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)
);
In which we apply aggregate functions. more the same
- world(name, continent, area, population, gdp)
SELECT SUM(population)
FROM world;
SELECT DISTINCT continent
FROM world;
SELECT continent
FROM world
GROUP BY continent;
SELECT SUM(gdp) as total_GDP
FROM world
GROUP BY continent
HAVING continent = 'Africa'
SELECT COUNT(name)
FROM world
WHERE area >= 1000000;
SELECT SUM(population)
FROM world
WHERE name in ('Estonia', 'Latvia', 'Lithuania');
SELECT continent, COUNT(name)
FROM world
GROUP BY continent;
7. For each continent show the continent and number of countries with populations of at least 10 million.
SELECT t1.continent, COUNT(t1.name)
FROM (SELECT continent, name
FROM world
WHERE population >= 10000000
) t1
GROUP BY t1.continent;
SELECT continent
FROM world
GROUP BY continent
HAVING SUM(population) >= 100000000;
In which we join two tables; game and goals. previously music tutorial
id | mdate | stadium | team1 | team2 |
---|---|---|---|---|
1001 | 8 June 2012 | National Stadium, Warsaw | POL | GRE |
1002 | 8 June 2012 | Stadion Miejski (Wroclaw) | RUS | CZE |
1003 | 12 June 2012 | Stadion Miejski (Wroclaw) | GRE | CZE |
1004 | 12 June 2012 | National Stadium, Warsaw | POL | RUS |
... |
matchid | teamid | player | gtime | |
---|---|---|---|---|
1001 | POL | Robert Lewandowski | 17 | |
1001 | GRE | Dimitris Salpingidis | 51 | |
1002 | RUS | Alan Dzagoev | 15 | |
1002 | RUS | Roman Pavlyuchenko | 82 | |
... |
id | teamname | coach | ||
---|---|---|---|---|
POL | Poland | Franciszek Smuda | ||
RUS | Russia | Dick Advocaat | ||
CZE | Czech Republic | Michal Bilek | ||
GRE | Greece | Fernando Santos | ||
... |
- game(id (#), mdate, stadium, team1, team2)
- goal(matchid (#), teamid (POL), player, gtime)
- eteam(id (POL), teamname, coach)
- To identify German players, check for: teamid = 'GER'
SELECT matchid, player
FROM goal
WHERE teamid LIKE '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 in the that the column matchid 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.
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
gameJOIN
goalON
(id=matchid) - The
FROM
clause says to merge data from the goal table with that from the game table. TheON
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)
SELECT player, teamid, stadium, mdate
FROM game
JOIN goal ON (id=matchid)
HAVING teamid = 'GER';
4. Show the team1, team2 and player for every goal scored by a player called Mario player LIKE
'Mario%'.
- Use the same
JOIN
as in the previous question.
SELECT team1, team2, player
FROM game
JOIN goal ON (id=matchid)
HAVING player LIKE 'Mario%';
- The table eteam gives details of every national team including the coach. You can
JOIN
goal to eteam using the phrase goalJOIN
eteam on teamid=id
SELECT player, teamid, coach, gtime
FROM goal
JOIN eteam ON teamid=id
WHERE gtime<=10;
6. List the dates of the matches and the name of the team in which 'Fernando Santos' was the team1 coach.
- To
JOIN
game with eteam you could use either gameJOIN
eteam ON (team1=eteam.id) or gameJOIN
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
SELECT mdate, eteam.teamname
FROM game
JOIN eteam on game.team1 = eteam.id
WHERE eteam.coach = 'Fernando Santos'
SELECT player
FROM goal
JOIN game on goal.matchid=game.id
WHERE stadium = 'National Stadium, Warsaw';
- Select goals scored only by non-German players in matches where GER was the id of either team1 or team2.
- You can use teamid!='GER' to prevent listing German players.
- You can use
DISTINCT
to stop players being listed twice
SELECT DISTINCT player
FROM game
JOIN goal ON goal.matchid = game.id
WHERE goal.teamid!='GER' AND
(game.team1 = 'GER' OR game.team2 = 'GER');
- You should
COUNT(*)
in theSELECT
line andGROUP BY
teamname
SELECT teamname, COUNT(player)
FROM goal
JOIN eteam ON eteam.id=goal.teamid
GROUP BY teamname;
SELECT stadium, COUNT(player)
FROM goal
JOIN game ON goal.matchid = game.id
GROUP BY stadium;
SELECT t.matchid, t.mdate, COUNT(t.player)
FROM (SELECT matchid, mdate, player
FROM game
JOIN goal ON game.id = goal.matchid
WHERE (team1 = 'POL' OR team2 = 'POL')) AS t
GROUP BY t.matchid, t.mdate
12. For every match where 'GER' scored, show matchid, match date and the number of goals scored by 'GER'
SELECT matchid, mdate, COUNT(*)
FROM (SELECT *
FROM goal
JOIN game ON goal.matchid = game.id
HAVING teamid = 'GER') AS t
GROUP BY matchid, mdate
- 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.
mdate | team1 | score1 | team2 | score2 |
---|---|---|---|---|
1 July 2012 | ESP | 4 | ITA | 0 |
10 June 2012 | ESP | 1 | ITA | 1 |
10 June 2012 | IRL | 1 | CRO | 3 |
... |
SELECT t.mdate, t.team1, sum(t.score1) as score1,
t.team2, sum(t.score2) as score2
FROM (SELECT mdate, team1,
CASE WHEN goal.teamid=team1 THEN 1
ELSE 0
END AS score1,
team2,
CASE WHEN goal.teamid=team2 THEN 1
ELSE 0
END AS score2
FROM game JOIN goal ON game.id = goal.matchid) as t
GROUP BY t.mdate, t.team1, t.team2
ORDER BY t.mdate, t.team1, t.team2;
SELECT DISTINCT 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 JOIN goal ON goal.matchid = game.id
GROUP BY mdate,team1,team2
ORDER BY mdate,team1,team2;
In which we join actors to movies in the Movie Database.
- movie(id (PK, 10212), title (A Kind of Loving), yr, director (FK), budget, gross)
- actor(id (PK), name)
- casting(movieid (PK,FK), actorid (PK,FK), ord)
SELECT id, title
FROM movie
WHERE yr=1962;
SELECT yr
FROM movie
WHERE title = 'Citizen Kane';
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.
SELECT id, title, yr
FROM movie
WHERE title LIKE '%Star Trek%'
ORDER BY yr;
SELECT id FROM actor WHERE name = 'Glenn Close';
SELECT id FROM movie WHERE title = 'Casablanca';
- The cast list is the names of the actors who were in the movie.
- Use movieid=11768, (or whatever value you got from the previous question)
SELECT name
FROM casting
JOIN actor ON casting.actorid = actor.id
WHERE movieid=11768;
SELECT name
FROM casting
JOIN actor ON casting.actorid = actor.id
JOIN movie ON movie.id = casting.movieid
WHERE title = 'Alien';
SELECT title
FROM movie
JOIN casting ON movie.id = casting.movieid
JOIN actor ON casting.actorid = actor.id
WHERE name = 'Harrison Ford';
- 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 movie
JOIN casting ON movie.id = casting.movieid
JOIN actor ON casting.actorid = actor.id
WHERE name = 'Harrison Ford' AND ord > 1;
SELECT title, name
FROM movie
JOIN casting ON movie.id = casting.movieid
JOIN actor ON casting.actorid = actor.id
WHERE yr = 1962 AND ord = 1;
11. 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(title)
FROM movie
JOIN casting ON movie.id=movieid
JOIN actor ON actorid=actor.id
WHERE name = 'Rock Hudson'
GROUP BY yr
HAVING COUNT(title) > 2;
- Did you get "Little Miss Marker twice"?
- Julie Andrews starred in the 1980 remake of Little Miss Marker and not the original(1934).
- Title is not a unique field, create a table of IDs in your subquery
-- All the movieids of movies with Julie Andrews
SELECT movieid
FROM casting
WHERE actorid IN (
SELECT id FROM actor
WHERE name='Julie Andrews');
--Hence
SELECT title, name
FROM movie
JOIN casting ON movie.id=movieid
JOIN actor ON actorid=actor.id
WHERE ord = 1 AND movieid IN (
SELECT movieid
FROM casting
WHERE actorid IN (
SELECT id FROM actor
WHERE name='Julie Andrews'));
SELECT name
FROM actor
JOIN casting ON actorid = actor.id
JOIN movie ON movieid = movie.id
WHERE ord = 1 AND actor.id = casting.actorid
GROUP BY name HAVING count(*) >= 15;
14. List the films released in the year 1978 ordered by the number of actors in the cast, then by title.
SELECT title, COUNT(actorid)
FROM casting
JOIN movie ON casting.movieid = movie.id
WHERE yr = 1978
GROUP BY title
ORDER BY COUNT(actorid) DESC, title;
SELECT name
FROM actor
JOIN casting ON actorid = actor.id
WHERE name != 'Art Garfunkel' AND movieid IN (
SELECT movieid
FROM movie
JOIN casting ON movieid = movie.id
JOIN actor ON actorid = actor.id
WHERE actor.name = 'Art Garfunkel');
In which we look at teachers in departments. previously Scottish Parliament
id | dept | name | phone | mobile |
---|---|---|---|---|
101 | 1 | Shrivell | 2753 | 07986 555 1234 |
102 | 1 | Throd | 2754 | 07122 555 1920 |
103 | 1 | Splint | 2293 | |
104 | Spiregrain | 3287 | ||
105 | 2 | Cutflower | 3212 | 07996 555 6574 |
106 | Deadyawn | 3345 | ||
... |
id | name |
---|---|
1 | Computing |
2 | Design |
3 | Engineering |
... |
- teacher(id, dept, name, phone, mobile)
- dept(id, name)
SELECT name
FROM teacher
WHERE dept IS NULL;
SELECT teacher.name, dept.name
FROM teacher
INNER JOIN dept
ON (teacher.dept=dept.id);
SELECT teacher.name, dept.name
FROM teacher
RIGHT JOIN dept
ON (teacher.dept=dept.id);
COALESCE
takes any number of arguments and returns the first value that is not null.COALESCE(x,y,z)
= x if x is notNULL
COALESCE(x,y,z)
= y if x isNULL
and y is notNULL
COALESCE(x,y,z)
= z if x and y areNULL
but z is notNULL
COALESCE(x,y,z)
=NULL
if x and y and z are allNULL
SELECT name, COALESCE(mobile, '07986 444 2266' )
FROM teacher;
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.
SELECT teacher.name, COALESCE(dept.name, 'None')
FROM teacher
LEFT JOIN dept on teacher.dept = dept.id;
SELECT COUNT(name), COUNT(mobile)
FROM teacher;
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.
SELECT dept.name, COUNT(teacher.name) AS staff
FROM teacher
RIGHT JOIN dept ON teacher.dept = dept.id
GROUP BY dept.name;
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.
SELECT
name,
CASE
WHEN dept = 1 OR dept = 2 THEN 'Sci'
ELSE 'Art'
END AS subject
FROM teacher;
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.
SELECT
name,
CASE
WHEN dept = 1 OR dept = 2 THEN 'Sci'
WHEN dept = 3 THEN 'Art'
ELSE 'None'
END AS subject
FROM teacher;
In which we look at a survey and deal with some more complex calculations.
- nss(
- ukprn, varchar(8)
- institution, varchar(100)
- subject, varchar(60)
- level, varchar(50)
- question, varchar(10)
- A_STRONGLY_DISAGREE, int(11)
- A_DISAGREE, int(11)
- A_NEUTRAL, int(11)
- A_AGREE, int(11)
- A_STRONGLY_AGREE, int(11)
- A_NEUTRAL, int(11)
- CI_MIN, int(11)
- score, int(11)
- CI_MAX, int(11)
- response, int(11)
- sample, int(11)
- aggregate, char(1) )
- 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';
SELECT institution, subject
FROM nss
WHERE question='Q15'
AND score >= 100;
3. 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 subject='(8) Computer Science'
AND score < 50;
4. 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'.
- You will need to use
SUM
over the response column andGROUP BY
subject
SELECT subject, SUM(response)
FROM nss
WHERE question='Q22' AND (
subject='(8) Computer Science' OR
subject='(H) Creative Arts and Design')
GROUP BY subject;
5. 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'.
- The A_STRONGLY_AGREE column is a percentage. To work out the total number of students who strongly agree you must multiply this percentage by the number who responded (response) and divide by 100 - take the
SUM
of that.
SELECT subject, sum(A_STRONGLY_AGREE*response/100) AS total
FROM nss
WHERE question='Q22' AND (
subject='(8) Computer Science' OR
subject='(H) Creative Arts and Design')
GROUP by subject;
6. 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))
FROM nss
WHERE question='Q22'
AND (subject='(8) Computer Science' OR
subject='(H) Creative Arts and Design')
GROUP BY subject;
7. 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(score*response)/SUM(response)) AS avg_score
FROM nss
WHERE question='Q22'
AND (institution LIKE '%Manchester%')
GROUP BY institution;
8. Show the institution, the total sample size and the number of computing students for institutions in Manchester for 'Q01'.
SELECT nss.institution, SUM(nss.sample) as total, T.comp as compsci
FROM nss
JOIN (SELECT institution, SUM(sample), SUM(sample) as comp
FROM nss WHERE question='Q01'
AND (institution LIKE '%Manchester%')
AND (subject='(8) Computer Science')
GROUP BY institution) T
ON nss.institution = T.institution
WHERE nss.question='Q01'
AND (nss.institution LIKE '%Manchester%')
GROUP BY nss.institution;
In which we examine UK general election results.
In which we measure the impact of COVID-19
Notes on the data: This data was assembled based on work done by Rodrigo Pombo based on John Hopkins University, based on World Health Organisation. The data was assembled 21st April 2020 - there are no plans to keep this data set up to date.
The SQL Window functions include LAG, LEAD, RANK and NTILE. These functions operate over a "window" of rows - typically these are rows in the table that are in some sense adjacent.
Here is the correct query showing the cases for the day before:
SELECT name, DAY(whn), confirmed,
LAG(confirmed, 1) OVER (partition by name ORDER BY whn) AS lag
FROM covid
WHERE name = 'Italy'
AND MONTH(whn) = 3
ORDER BY whn
Notice how the values in the LAG column match the value of the row diagonally above and to the left.
name | DAY(whn) | confirmed | dbf |
---|---|---|---|
Italy | 1 | 1694 | null |
Italy | 2 | 2036 | 1694 |
Italy | 3 | 2502 | 2036 |
Italy | 4 | 3089 | 2502 |
Italy | 5 | 3858 | 3089 |
Italy | 6 | 4636 | 3858 |
Italy | 7 | 5883 | 4636 |
Italy | 8 | 7375 | 5883 |
Italy | 9 | 9172 | 7375 |
Italy | 10 | 10149 | 9172 |
... |
In which we join Edinburgh bus routes to Edinburgh bus routes.
stops |
---|
id |
name |
route |
---|
num |
company |
pos |
stop |
- stops(id (1), name (Aberlady))
- route(num (124), company ('LRT'), pos (9), stop (1))
SELECT COUNT(id) FROM stops;
SELECT id FROM stops WHERE name = 'Craiglockhart';
SELECT id, name
FROM stops
JOIN route on stops.id = route.stop
WHERE num = '4' AND company = 'LRT';
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.
SELECT company, num, COUNT(*)
FROM route WHERE stop=149 OR stop=53
GROUP BY company, num
HAVING COUNT(*) = 2;
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.
-- SELECT a.company, a.num, a.stop, b.stop
-- FROM route a JOIN route b ON
-- (a.company=b.company AND a.num=b.num)
-- WHERE a.stop=53
SELECT a.company, a.num, a.stop, b.stop
FROM route a JOIN route b ON
(a.company=b.company AND a.num=b.num)
WHERE a.stop=53
AND b.stop=(SELECT id FROM stops WHERE name='London Road');
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'
-- SELECT a.company, a.num, stopa.name, stopb.name
-- FROM route a JOIN route b ON
-- (a.company=b.company AND a.num=b.num)
-- JOIN stops stopa ON (a.stop=stopa.id)
-- JOIN stops stopb ON (b.stop=stopb.id)
-- WHERE stopa.name='Craiglockhart'
SELECT a.company, a.num, stopa.name, stopb.name
FROM route a JOIN route b ON
(a.company=b.company AND a.num=b.num)
JOIN stops stopa ON (a.stop=stopa.id)
JOIN stops stopb ON (b.stop=stopb.id)
WHERE stopa.name='Craiglockhart'
AND stopb.name='London Road';
SELECT DISTINCT T.company, T.num
FROM (
SELECT a.company AS company, a.num AS num
FROM route a JOIN route b ON
(a.company=b.company AND a.num=b.num)
JOIN stops stopa ON (a.stop=stopa.id)
JOIN stops stopb ON (b.stop=stopb.id)
WHERE stopa.name='Haymarket'
AND stopb.name='Leith') T;
SELECT a.company AS company, a.num AS num
FROM route a JOIN route b ON
(a.company=b.company AND a.num=b.num)
JOIN stops stopa ON (a.stop=stopa.id)
JOIN stops stopb ON (b.stop=stopb.id)
WHERE stopa.name='Craiglockhart'
AND stopb.name='Tollcross'
and stopb.name!='Craiglockhart';
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.
SELECT stopb.name, a.company, a.num
FROM route a JOIN route b ON
(a.company=b.company AND a.num=b.num)
JOIN stops stopa ON (a.stop=stopa.id)
JOIN stops stopb ON (b.stop=stopb.id)
WHERE stopa.name='Craiglockhart';