Skip to content

Commit 29da795

Browse files
committed
Move exercises into separate repositories
1 parent b9c2cc8 commit 29da795

9 files changed

+656
-0
lines changed

Diff for: 01_select_basics.sql

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
-- Spiderman Andew Garfield- NY accent
2+
3+
-- 1. Show the population of Germany
4+
SELECT population
5+
FROM world
6+
WHERE name = 'Germany'
7+
8+
-- 2. Show the per capita gdp: (gdp/population) for each country where the area
9+
-- is over 5,000,000 km^2
10+
SELECT name country, gdp/population per_capita_gdp
11+
FROM world
12+
WHERE area > 5000000
13+
14+
-- 3. Show the name and continent where the area is less then 2000 and the gdp
15+
-- is more than 5000000000
16+
SELECT name country, continent
17+
FROM world
18+
WHERE area < 2000
19+
AND gdp > 5000000000
20+
21+
-- 4. Show the name and the population for 'Denmark', 'Finland', 'Norway',
22+
-- 'Sweden'
23+
SELECT name country, population
24+
FROM world
25+
WHERE name IN ('Denmark', 'Finland', 'Norway', 'Sweden')
26+
27+
-- 5. Show each country that begins with G
28+
SELECT name country
29+
FROM world
30+
WHERE name LIKE 'G%'
31+
32+
-- 6. Show the area in 1000 square km. Show area/1000 instead of area
33+
SELECT name country, area/1000
34+
FROM world
35+
WHERE area BETWEEN 207600 AND 244820

Diff for: 02_select_from_world_tutorial.sql

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
-- 1. Observe the result of running a simple SQL command.
2+
SELECT name country, continent, population
3+
FROM world
4+
5+
-- 2. Show the name for the countries that have a population of at least 200
6+
-- million.
7+
SELECT name country
8+
FROM world
9+
WHERE population>200000000
10+
11+
-- 3. Give the name and the per capita GDP for those countries with a
12+
-- population of at least 200 million.
13+
SELECT name country, gdp/population per_capita_gdp
14+
FROM world
15+
WHERE population > 200E6
16+
17+
-- 4. Show the (name) and (population) in millions for the countries of
18+
-- 'South America' Divide the population by 1000000 to get population in
19+
-- millions.
20+
SELECT name country, population/1000000 pop_millions
21+
FROM world
22+
WHERE continent = 'South America'
23+
24+
-- 5. Show the (name) and (population) for 'France', 'Germany', 'Italy'
25+
SELECT name country, population
26+
FROM world
27+
WHERE name IN ('France', 'Germany', 'Italy')
28+
29+
-- 6. Identify the countries which have names including the word 'United'
30+
SELECT name country
31+
FROM world
32+
WHERE name LIKE '%United%'

Diff for: 03_select_from_nobel_tutorial.sql

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
-- 1. Change the query shown so that it displays Nobel prizes for 1950.
2+
SELECT yr, subject, winner
3+
FROM nobel
4+
WHERE yr = 1950
5+
6+
-- 2. Show who won the 1962 prize for Literature.
7+
SELECT winner winner_1962
8+
FROM nobel
9+
WHERE yr = 1962
10+
AND subject = 'Literature'
11+
12+
-- 3. Show the year and subject that won 'Albert Einstein' his prize.
13+
SELECT yr, subject
14+
FROM nobel
15+
WHERE winner = 'Albert Einstein'
16+
17+
-- 4. Give the name of the 'Peace' winners since the year 2000, including 2000.
18+
SELECT winner winner_peace
19+
FROM nobel
20+
WHERE subject = 'Peace'
21+
AND yr >= 2000
22+
23+
-- 5. Show all details (yr, subject, winner) of the Literature prize winners for
24+
-- 1980 to 1989 inclusive.
25+
SELECT *
26+
FROM nobel
27+
WHERE subject = 'Literature'
28+
AND yr BETWEEN 1980 AND 1989
29+
30+
-- 6. Show all details of the presidential winners: ('Theodore Roosevelt',
31+
-- 'Woodrow Wilson', 'Jed Bartlet', 'Jimmy Carter')
32+
SELECT *
33+
FROM nobel
34+
WHERE winner IN ('Theodore Roosevelt', 'Woodrow Wilson', 'Jed Bartlet',
35+
'Jimmy Carter')
36+
37+
-- 7. Show the winners with first name John
38+
SELECT winner
39+
FROM nobel
40+
WHERE winner LIKE 'John%'
41+
42+
-- 8. In which years was the Physics prize awarded but no Chemistry prize.
43+
-- (WARNING - this question is way too hard for this level, you will need to use
44+
-- sub queries or joins).
45+
SELECT DISTINCT p.yr
46+
FROM nobel p
47+
WHERE p.subject = 'Physics'
48+
AND p.yr NOT IN
49+
(
50+
SELECT c.yr
51+
FROM nobel c
52+
WHERE c.subject = 'Chemistry'
53+
)

Diff for: 04_select_within_select_tutorial.sql

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
-- 1. List each country name where the population is larger than 'Russia'.
2+
SELECT name country
3+
FROM world
4+
WHERE population >
5+
(
6+
SELECT population FROM world
7+
WHERE name='Russia'
8+
)
9+
10+
-- 2. List the name and continent of countries in the continents containing
11+
-- 'Belize', 'Belgium'.
12+
SELECT a.name country, a.continent
13+
FROM world a
14+
WHERE a.continent IN
15+
(
16+
SELECT b.continent
17+
FROM world b
18+
WHERE b.name IN ('Belize', 'Belgium')
19+
)
20+
21+
-- 3. Show the countries in Europe with a per capita GDP greater than
22+
-- 'United Kingdom'.
23+
SELECT a.name country
24+
FROM world a
25+
WHERE a.continent = 'Europe'
26+
AND a.gdp/a.population >
27+
(
28+
SELECT b.gdp/b.population
29+
FROM world b
30+
WHERE b.name = 'United Kingdom'
31+
)
32+
33+
-- 4. Which country has a population that is more than Canada but less than
34+
-- Poland? Show the name and the population.
35+
SELECT a.name country, a.population
36+
FROM world a
37+
WHERE a.population >
38+
(
39+
SELECT b.population
40+
FROM world b
41+
WHERE b.name = 'Canada'
42+
)
43+
AND a.population <
44+
(
45+
SELECT c.population
46+
FROM world c
47+
WHERE c.name = 'Poland'
48+
)
49+
50+
-- 5. Which countries have a GDP greater than any country in Europe? [Give the
51+
-- name only.]
52+
SELECT a.name country
53+
FROM world a
54+
WHERE a.gdp > ALL
55+
(
56+
SELECT b.gdp
57+
FROM world b
58+
WHERE b.continent = 'Europe'
59+
)
60+
61+
-- 6. Find the largest country (by area) in each continent, show the continent,
62+
-- the name and the area:
63+
SELECT continent, name country, area
64+
FROM world x
65+
WHERE area >= ALL
66+
(
67+
SELECT area FROM world y
68+
WHERE y.continent=x.continent
69+
AND area>0
70+
)
71+
72+
-- 7. Find each country that belongs to a continent where all populations are
73+
-- less than 25000000. Show name, continent and population.
74+
SELECT a.name country, a.continent, a.population
75+
FROM world a
76+
WHERE 25000000 > ALL
77+
(
78+
SELECT b.population
79+
FROM world b
80+
WHERE a.continent = b.continent
81+
AND b.population > 0
82+
)
83+
84+
-- 8. Some countries have populations more than three times that of any of their
85+
-- neighbours (in the same continent). Give the countries and continents.
86+
SELECT a.name country, a.continent
87+
FROM world a
88+
WHERE a.population > ALL
89+
(
90+
SELECT b.population*3
91+
FROM world b
92+
WHERE a.continent = b.continent
93+
AND a.name <> b.name
94+
)

Diff for: 05_sum_and_count.sql

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
-- 1. Show the total population of the world.
2+
SELECT SUM(population) pop_world
3+
FROM world
4+
5+
-- 2. List all the continents - just once each.
6+
SELECT DISTINCT continent
7+
FROM world
8+
9+
-- 3. Give the total GDP of Africa
10+
SELECT SUM(gdp) gdp_Africa
11+
FROM world
12+
WHERE continent = 'Africa'
13+
14+
-- 4. How many countries have an area of at least 1000000
15+
SELECT COUNT(name) num_countries
16+
FROM world
17+
WHERE area >= 1000000
18+
19+
-- 5. What is the total population of ('France','Germany','Spain')
20+
SELECT SUM(population) total_pop
21+
FROM world
22+
WHERE name IN ('France','Germany','Spain')
23+
24+
-- 6. For each continent show the continent and number of countries.
25+
SELECT continent, COUNT(name) num_countries
26+
FROM world
27+
GROUP BY continent
28+
29+
-- 7. For each continent show the continent and number of countries with
30+
-- populations of at least 10 million.
31+
SELECT continent, COUNT(name) num_countries
32+
FROM world
33+
WHERE population >= 10E6
34+
GROUP BY continent
35+
36+
-- 8. List the continents with total populations of at least 100 million.
37+
SELECT continent
38+
FROM world
39+
GROUP BY continent
40+
HAVING SUM(population) >= 100E6

Diff for: 06_the_join_operation.sql

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
-- 1. Show matchid and player name for all goals scored by Germany.
2+
-- (teamid = 'GER')
3+
SELECT matchid, player
4+
FROM goal
5+
WHERE teamid = 'GER'
6+
7+
-- 2. Show id, stadium, team1, team2 for game 1012
8+
SELECT id,stadium,team1,team2
9+
FROM game
10+
WHERE id = 1012
11+
12+
-- 3. Show the player, teamid and mdate and for every German goal.
13+
-- (teamid='GER')
14+
SELECT player, teamid, mdate
15+
FROM game
16+
JOIN goal ON (game.id = goal.matchid)
17+
WHERE teamid = 'GER'
18+
19+
-- 4. Show the team1, team2 and player for every goal scored by a player called
20+
-- Mario (player LIKE 'Mario%')
21+
SELECT team1, team2, player
22+
FROM goal
23+
JOIN game ON (game.id = goal.matchid)
24+
WHERE player LIKE 'Mario%'
25+
26+
-- 5. Show (player), (teamid), (coach), (gtime) for all goals scored in the
27+
-- first 10 minutes (gtime<=10)
28+
SELECT player, teamid, coach, gtime
29+
FROM goal
30+
JOIN eteam ON (goal.teamid = eteam.id)
31+
WHERE gtime <= 10
32+
33+
-- 6. List the the dates of the matches and the name of the team in which
34+
-- 'Fernando Santos' was the team1 coach.
35+
SELECT mdate, teamname
36+
FROM game
37+
JOIN eteam ON (game.team1 = eteam.id)
38+
WHERE coach = 'Fernando Santos'
39+
40+
-- 7. List the player for every goal scored in a game where the staium was
41+
-- 'National Stadium, Warsaw'
42+
SELECT player
43+
FROM goal
44+
JOIN game ON (goal.matchid = game.id)
45+
WHERE stadium = 'National Stadium, Warsaw'
46+
47+
-- 8. Show names of all players who scored a goal against Germany.
48+
SELECT DISTINCT player
49+
FROM game
50+
JOIN goal ON goal.matchid = game.id
51+
WHERE (team1 = 'GER' OR team2 = 'GER')
52+
AND teamid <> 'GER'
53+
54+
-- 9. Show teamname and the total number of goals scored.
55+
SELECT teamname, COUNT(player) goals_scored
56+
FROM eteam JOIN goal ON eteam.id = goal.teamid
57+
GROUP BY teamname
58+
59+
-- 10. Show the stadium and the number of goals scored in each stadium.
60+
SELECT stadium, COUNT(player) goals_scored
61+
FROM game
62+
JOIN goal ON game.id = goal.matchid
63+
GROUP BY stadium
64+
65+
-- 11. For every match involving 'POL', show the matchid, date and the number of
66+
-- goals scored.
67+
SELECT matchid, mdate, COUNT(player) goals_scored
68+
FROM game
69+
JOIN goal ON goal.matchid = game.id
70+
WHERE (team1 = 'POL' OR team2 = 'POL')
71+
GROUP BY goal.matchid
72+
73+
-- 12. For every match where 'GER' scored, show matchid, match date and the
74+
-- number of goals scored by 'GER'
75+
SELECT matchid, mdate, COUNT(player)
76+
FROM game
77+
JOIN goal ON game.id = goal.matchid
78+
WHERE teamid = 'GER'
79+
GROUP BY game.id
80+
81+
-- 13. List every match with the goals scored by each team as shown.
82+
-- Sort your result by mdate, matchid, team1 and team2.
83+
SELECT mdate,
84+
team1,
85+
SUM(CASE WHEN teamid=team1 THEN 1 ELSE 0 END) score1,
86+
team2,
87+
SUM(CASE WHEN teamid=team2 THEN 1 ELSE 0 END) score2
88+
FROM game JOIN goal ON goal.matchid = game.id
89+
GROUP BY game.id
90+
ORDER BY mdate, matchid, team1, team2

0 commit comments

Comments
 (0)