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