Skip to content

Instantly share code, notes, and snippets.

@ChildrenofkoRn
Created April 21, 2023 22:51
Show Gist options
  • Save ChildrenofkoRn/30cde0eec22b570716e32ebd1dc98f5c to your computer and use it in GitHub Desktop.
Save ChildrenofkoRn/30cde0eec22b570716e32ebd1dc98f5c to your computer and use it in GitHub Desktop.
SQLZoo.net solutions 2023.04.22
-- Select SQL Engine: MySQL
--
--
-- ==================================================================
-- SELECT basics
-- ==================================================================
-- 1. Modify it to show the population of Germany
SELECT population
FROM world
WHERE name = 'Germany'
-- 2. Show the name and the population for 'Sweden', 'Norway' and 'Denmark'.
SELECT name, population
FROM world
WHERE name IN ('Sweden', 'Norway', 'Denmark')
-- 3. 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
-- ==================================================================
-- SELECT Quiz
-- ==================================================================
3, 5, 5, 3, 3, 3, 3
-- ==================================================================
-- SELECT from WORLD Tutorial
-- ==================================================================
-- 1. 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. 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
-- 4. 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'
-- 5. Show the name and population for France, Germany, Italy
SELECT name, population
FROM world
WHERE name IN ('France', 'Germany', 'Italy')
-- 6. Show the countries which have a name that includes the word 'United'
SELECT name
FROM world
WHERE name LIKE '%United%'
-- 7. Show the countries that are big by area or big by population. 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.
SELECT name, population, area
FROM world
WHERE area > 3000000 XOR population > 250000000
/* MsSQL:
SELECT name, population, area
FROM world
WHERE (area > 3000000 AND population <= 250000000)
OR (area <= 3000000 AND population > 250000000)
*/
-- 9. 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 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
-- 11. Show the name and capital where the name and the capital have the same number of characters.
SELECT name, capital
FROM world
WHERE LENGTH(name) = LENGTH(capital)
-- 12. 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.
SELECT name, capital
FROM world
WHERE LEFT(name,1) = LEFT(capital,1) AND name <> capital
-- 13. Find the country that has all the vowels and no spaces in its name.
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 '% %';
-- ==================================================================
-- BBC QUIZ
-- ==================================================================
5, 4, 2, 4, 2, 4, 3
-- ==================================================================
-- SELECT from Nobel Tutorial
-- ==================================================================
-- 1. Change the query shown so that it displays Nobel prizes for 1950.
SELECT yr, subject, winner
FROM nobel
WHERE yr = 1950
-- 2. Show who won the 1962 prize for literature.
SELECT winner
FROM nobel
WHERE yr = 1962
AND subject = 'literature'
-- 3. Show the year and subject that won 'Albert Einstein' his prize.
SELECT yr, subject
FROM nobel
WHERE winner = 'Albert Einstein'
-- 4. Give the name of the 'peace' winners since the year 2000, including 2000.
SELECT winner
FROM nobel
WHERE yr >= 2000 AND subject LIKE '%peace%'
-- 5. Show all details (yr, subject, winner) of the literature prize winners for 1980 to 1989 inclusive.
SELECT yr, subject, winner
FROM nobel
WHERE yr BETWEEN 1980 AND 1989 AND subject = 'literature'
-- 6. Show all details of the presidential winners:
-- Theodore Roosevelt
-- Thomas Woodrow Wilson
-- Jimmy Carter
-- Barack Obama
SELECT yr, subject, winner
FROM nobel
WHERE winner IN ('Theodore Roosevelt',
'Woodrow Wilson',
'Jimmy Carter',
'Barack Obama')
-- 7. Show the winners with first name John
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 (subject = 'physics' AND yr = 1980) OR
(subject = 'chemistry' AND yr = 1984)
-- 9. 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')
-- 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)
-- 11. Find all details of the prize won by PETER GRÜNBERG
SELECT *
FROM nobel
WHERE winner = 'PETER GRÜNBERG'
-- 12. Find all details of the prize won by EUGENE O'NEILL
SELECT *
FROM nobel
WHERE winner = 'EUGENE O''NEILL'
-- 13. 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%'
-- 14. 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 subject IN ('physics','chemistry'), subject, winner
-- ==================================================================
-- Nobel Quiz
-- ==================================================================
5, 3, 2, 3, 3, 3, 4
-- ==================================================================
-- SELECT within SELECT Tutorial
-- ==================================================================
-- 1. 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')
-- 2. 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')
-- 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
-- 4. Which country has a population that is more than United Kingdom but less than Germany? 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')
-- 5. Show the name and the population of each country in Europe.
-- Show the population as a percentage of the population of Germany.
SELECT name, CONCAT(ROUND(population/((SELECT population FROM world
WHERE name = 'Germany')/100)), '%') AS 'percentage'
FROM world
WHERE continent = 'Europe'
-- 6. 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 > (SELECT MAX(gdp) FROM world WHERE continent = 'Europe')
AND continent != 'Europe'
-- 7. 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)
-- OR
SELECT continent, name, area
FROM world x
WHERE area = (SELECT MAX(area) FROM world y
WHERE y.continent = x.continent
AND area > 0)
-- 8. List each continent and the name of the country that comes first alphabetically.
SELECT continent, name
FROM world x
WHERE name = (SELECT name FROM world y
WHERE y.continent = x.continent
ORDER BY name
LIMIT 1)
-- 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.
SELECT name, continent, population
FROM world x
WHERE 25000000 > ALL (SELECT population FROM world y
WHERE y.continent = x.continent AND y.population > 0)
-- 10. Some countries have populations more than three times that of all of their neighbours (in the same continent). Give the countries and continents.
SELECT name, continent
FROM world x
WHERE population / 3 > (SELECT MAX(population) FROM world y
WHERE y.continent = x.continent AND y.name != x.name)
-- ==================================================================
-- Nested SELECT Quiz
-- ==================================================================
3, 2, 1, 4, 2, 2, 2
-- ==================================================================
-- SUM and COUNT
-- ==================================================================
-- 1. Show the total population of the world.
SELECT SUM(population)
FROM world
-- 2. List all the continents - just once each.
SELECT DISTINCT(continent)
FROM world
-- 3. Give the total GDP of Africa
SELECT SUM(GDP)
FROM world
WHERE continent = 'Africa'
-- 4. How many countries have an area of at least 1000000
SELECT COUNT(name)
FROM world
WHERE area >= 1000000
-- 5. What is the total population of ('Estonia', 'Latvia', 'Lithuania')
SELECT SUM(population )
FROM world
WHERE name in ('Estonia', 'Latvia', 'Lithuania')
-- 6. For each continent show the continent and number of countries.
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 continent, COUNT(name)
FROM world
WHERE population >= 10000000
GROUP BY continent
-- 8. List the continents that have a total population of at least 100 million.
SELECT continent
FROM world
GROUP BY continent
HAVING SUM(population) >= 100000000
-- ==================================================================
-- SUM and COUNT Quiz
-- ==================================================================
3, 1, 4, 5, 2, 5, 4, 4
-- ==================================================================
-- The JOIN operation
-- =================================================================='
-- 1. 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'
-- OR
SELECT matchid, player FROM goal
JOIN eteam ON goal.teamid = eteam.id
WHERE eteam.teamname = 'Germany'
-- 2. Show id, stadium, team1, team2 for just game 1012
SELECT id, stadium, team1, team2
FROM game
WHERE id = 1012
-- 3. Modify it to show the player, teamid, stadium and mdate for every German goal.
SELECT player, teamid, stadium, mdate
FROM game
JOIN goal ON (game.id = goal.matchid)
JOIN eteam ON (goal.teamid = eteam.id)
WHERE eteam.teamname = 'Germany'
-- 4. 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 (game.id = goal.matchid)
WHERE goal.player LIKE 'Mario%'
-- 5. 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 (goal.teamid = eteam.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.
SELECT mdate, teamname
FROM game
JOIN eteam ON (game.team1 = eteam.id)
WHERE eteam.coach = 'Fernando Santos' AND
team1 = eteam.id
-- 7. List the player for every goal scored in a game where the stadium was 'National Stadium, Warsaw'
SELECT player
FROM game
JOIN goal ON (game.id = goal.matchid)
WHERE stadium = 'National Stadium, Warsaw'
-- 8. Instead show the name of all players who scored a goal against Germany.
SELECT player
FROM game JOIN goal ON matchid = id
WHERE (team1 = 'GER' OR team2 = 'GER') AND teamid != 'GER' GROUP BY player
-- 9. Show teamname and the total number of goals scored.
SELECT teamname, COUNT(teamname)
FROM eteam JOIN goal ON id = teamid
GROUP BY teamname
-- 10. Show the stadium and the number of goals scored in each stadium.
SELECT stadium, COUNT(stadium)
FROM game JOIN goal ON id = matchid
GROUP BY stadium
-- 11. For every match involving 'POL', show the matchid, date and the number of goals scored.
SELECT matchid,mdate, COUNT(matchid)
FROM game JOIN goal ON matchid = id
WHERE (team1 = 'POL' OR team2 = 'POL')
GROUP BY matchid, mdate
-- 12. For every match where 'GER' scored, show matchid, match date and the number of goals scored by 'GER'
SELECT matchid, mdate, COUNT(matchid)
FROM game JOIN goal ON matchid = id
WHERE teamid = 'GER'
GROUP BY matchid, mdate
-- 12. 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.
-- 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
-- For the answer to be accepted in MySQL, omit sorting.
-- ==================================================================
-- JOIN Quiz
-- ==================================================================
4, 3, 1, 1, 2, 3, 2
-- ==================================================================
-- Using Null
-- ==================================================================
-- 1. List the teachers who have NULL for their department.
SELECT name FROM teacher
WHERE dept IS NULL
-- 2. 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 (teacher.dept = dept.id)
-- 3. Use a different JOIN so that all teachers are listed.
SELECT teacher.name, dept.name
FROM teacher LEFT JOIN dept
ON (teacher.dept = dept.id)
-- 4. Use a different JOIN so that all departments are listed.
SELECT teacher.name, dept.name
FROM teacher RIGHT JOIN dept
ON (teacher.dept = dept.id)
-- 5. Show teacher name and mobile number or '07986 444 2266'
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)
-- 7. Use COUNT to show the number of teachers and the number of mobile phones.
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)
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
FROM teacher
-- OR
SELECT name,
CASE WHEN dept IN (1, 2) THEN 'Sci'
ELSE 'Art' END
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
FROM teacher
-- ==================================================================
-- Using Null Quiz
-- ==================================================================
5, 3, 5, 2, 1, 1
-- ==================================================================
-- Self join
-- ==================================================================
-- 1. How many stops are in the database.
SELECT COUNT(*) AS COUNT FROM stops
-- 2. Find the id value for the stop 'Craiglockhart'
SELECT id FROM stops WHERE name = 'Craiglockhart'
-- 3. 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 answer is not accepted because of the sorting returned by MySQL.
-- 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 AND b.stop = 149
-- 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' AND stopb.name = 'London Road'
-- 7. Give a list of all the services which connect stops 115 and 137 ('Haymarket' and 'Leith')
SELECT DISTINCT a.company, a.num
FROM route a
JOIN route b
ON a.company = b.company AND a.num = b.num
WHERE a.stop = 115 AND b.stop = 137
-- OR
SELECT DISTINCT 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 = 'Haymarket' AND stopb.name = 'Leith'
-- 8. Give a list of the services which connect the stops 'Craiglockhart' and 'Tollcross'
SELECT 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'
AND stopb.name = 'Tollcross'
-- 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 stop_x.name, x.company, x.num
FROM route x
JOIN route z ON x.num = z.num AND x.company = z.company
JOIN stops stop_x ON x.stop = stop_x.id
JOIN stops stop_z ON z.stop = stop_z.id
WHERE stop_z.name = 'Craiglockhart'
-- 10. 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, stops.name, d.num, d.company
FROM route a JOIN route b
ON a.company = b.company AND a.num = b.num
JOIN stops ON b.stop = stops.id
JOIN route c ON c.stop = stops.id
JOIN route d ON c.company = d.company AND c.num = d.num
WHERE a.stop = (SELECT id FROM stops WHERE name = 'Craiglockhart')
AND d.stop = (SELECT id FROM stops WHERE name = 'Lochend')
ORDER BY a.num, stops.name, d.num
-- ==================================================================
-- Self join Quiz
-- ==================================================================
3, 5, 4
-- ==================================================================
-- ==================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment