Skip to content

Commit f4a7882

Browse files
committed
select within select tutorial and fix upper cases
1 parent 6e993f3 commit f4a7882

File tree

3 files changed

+189
-62
lines changed

3 files changed

+189
-62
lines changed

select_from_nobel.sql

+32-32
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-- 1. nobel Nobel Laureates
1+
-- 1. Nobel Laureates
22
-- We continue practicing simple SQL queries on a single table. This tutorial is concerned with a table of Nobel prize winners:
33

44
SELECT yr, subject, winner
@@ -19,27 +19,27 @@ AND subject = 'literature'
1919

2020
-- 3. Albert Einstein
2121

22-
select yr, subject
23-
from nobel
24-
where winner = 'Albert Einstein'
22+
SELECT yr, subject
23+
FROM nobel
24+
WHERE winner = 'Albert Einstein'
2525

2626

2727

2828
-- 4. Recent Peace Prizes
2929
-- Give the name of the 'peace' winners since the year 2000, including 2000.
3030

31-
select winner
32-
from nobel
33-
where yr >= 2000 and subject = 'peace'
31+
SELECT winner
32+
FROM nobel
33+
WHERE yr >= 2000 AND subject = 'peace'
3434

3535

3636

3737
-- 5. Literature in the 1980's
3838
-- Show all details (yr, subject, winner) of the literature prize winners for 1980 to 1989 inclusive.
3939

40-
select yr, subject, winner
41-
from nobel
42-
where (yr between 1980 and 1989) and (subject = 'literature')
40+
SELECT yr, subject, winner
41+
FROM nobel
42+
WHERE (yr BETWEEN 1980 AND 1989) AND (subject = 'literature')
4343

4444

4545

@@ -65,9 +65,9 @@ WHERE winner IN (
6565
-- 7. John
6666
-- Show the winners with first name John
6767

68-
select winner
69-
from nobel
70-
where winner like 'John%'
68+
SELECT winner
69+
FROM nobel
70+
WHERE winner LIKE 'John%'
7171

7272

7373

@@ -76,28 +76,28 @@ where winner like 'John%'
7676
-- Show the year, subject, and name of physics winners for 1980 together with the chemistry winners for 1984.
7777

7878

79-
select *
80-
from nobel
81-
where (yr = 1980 and subject = 'physics') or (yr = 1984 and subject = 'chemistry')
79+
SELECT *
80+
FROM nobel
81+
WHERE (yr = 1980 and subject = 'physics') OR (yr = 1984 and subject = 'chemistry')
8282

8383

8484

8585
-- 9. Exclude Chemists and Medics
8686
-- Show the year, subject, and name of winners for 1980 excluding chemistry and medicine
8787

88-
select *
89-
from nobel
90-
where yr = 1980 and not subject in ('chemistry', 'medicine')
88+
SELECT *
89+
FROM nobel
90+
WHERE yr = 1980 AND NOT subject IN ('chemistry', 'medicine')
9191

9292

9393

9494
-- 10. Early Medicine, Late Literature
9595
-- 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)
9696

9797

98-
select yr, subject, winner
99-
from nobel
100-
where (yr < 1910 and subject = 'Medicine') or (yr >= 2004 and subject = 'Literature')
98+
SELECT yr, subject, winner
99+
FROM nobel
100+
WHERE (yr < 1910 and subject = 'Medicine') OR (yr >= 2004 AND subject = 'Literature')
101101

102102

103103

@@ -115,20 +115,20 @@ WHERE winner LIKE 'peter gr%nberg'
115115
-- 12. Apostrophe
116116
-- Find all details of the prize won by EUGENE O'NEILL
117117

118-
select *
119-
from nobel
120-
where winner = 'Eugene O''neill'
118+
SELECT *
119+
FROM nobel
120+
WHERE winner = 'Eugene O''neill'
121121

122122

123123

124124
-- 13. Knights of the realm
125125
-- List the winners, year and subject where the winner starts with Sir. Show the the most recent first, then by name order.
126126

127127

128-
select winner, yr, subject
129-
from nobel
130-
where winner like 'Sir%'
131-
order by yr DESC, winner
128+
SELECT winner, yr, subject
129+
FROM nobel
130+
WHERE winner like 'Sir%'
131+
ORDER BY yr DESC, winner
132132

133133

134134

@@ -138,6 +138,6 @@ order by yr DESC, winner
138138
SELECT winner, subject
139139
FROM nobel
140140
WHERE yr=1984
141-
order by
142-
case when subject IN ('physics', 'chemistry') then 1 else 0 END,
143-
subject, winner
141+
ORDER BY
142+
CASE WHEN subject IN ('physics', 'chemistry') THEN 1 ELSE 0 END,
143+
subject, winner

select_from_world.sql

+30-30
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
- 1. Introduction
1+
-- 1. Introduction
22
-- Read the notes about this table. Observe the result of running this SQL command to show the name, continent and population of all countries.
33

44
SELECT name, continent, population
@@ -18,17 +18,17 @@ WHERE population >= 200000000
1818
-- 3. Per capita GDP
1919
-- Give the name and the per capita GDP for those countries with a population of at least 200 million.
2020

21-
select name, gdp/population
22-
from world
21+
SELECT name, gdp/population
22+
FROM world
2323
where population > 200000000
2424

2525

2626

2727
-- 4. South America In millions
2828
-- 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.
2929

30-
select name, population/1000000
31-
from world
30+
SELECT name, population/1000000
31+
FROM world
3232
where continent = 'South America'
3333

3434

@@ -37,27 +37,27 @@ where continent = 'South America'
3737
-- 5. France, Germany, Italy
3838
-- Show the name and population for France, Germany, Italy
3939

40-
select name, population
41-
from world
42-
where name in ('France', 'Germany', 'Italy')
40+
SELECT name, population
41+
FROM world
42+
WHERE name IN ('France', 'Germany', 'Italy')
4343

4444

4545

4646
-- 6. United
4747
-- Show the countries which have a name that includes the word 'United'
4848

49-
select name
50-
from world
51-
where name like 'United%'
49+
SELECT name
50+
FROM world
51+
WHERE name LIKE 'United%'
5252

5353

5454

5555
-- 7. Two ways to be big
5656
-- 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.
5757

58-
select name, population, area
59-
from world
60-
where area > 3000000 or population > 250000000
58+
SELECT name, population, area
59+
FROM world
60+
WHERE area > 3000000 OR population > 250000000
6161

6262

6363

@@ -70,18 +70,18 @@ where area > 3000000 or population > 250000000
7070
-- United Kingdom has a small population and a small area, it should be excluded.
7171

7272

73-
select name, population, area
74-
from world
75-
where (area > 3000000 or population > 250000000)
76-
and not (area > 3000000 and population > 250000000)
73+
SELECT name, population, area
74+
FROM world
75+
WHERE (area > 3000000 OR population > 250000000)
76+
AND NOT (area > 3000000 AND population > 250000000)
7777

7878

7979

8080
-- 9. rounding
8181
-- 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.
8282

8383

84-
SELECT name, round(population/1000000,2) as Population , round(gdp/1000000000, 2) as GDP
84+
SELECT name, ROUND(population/1000000,2) AS Population , ROUND(gdp/1000000000, 2) AS GDP
8585
FROM world
8686
WHERE continent = 'South America'
8787

@@ -103,9 +103,9 @@ WHERE gdp > 1000000000000
103103
-- You can use the LENGTH function to find the number of characters in a string
104104

105105

106-
select name, capital
107-
from world
108-
where length(name) = length(capital)
106+
SELECT name, capital
107+
FROM world
108+
WHERE length(name) = length(capital)
109109

110110

111111

@@ -127,11 +127,11 @@ WHERE left(name, 1) = left(capital, 1) xor name = capital
127127
-- The query shown misses countries like Bahamas and Belarus because they contain at least one 'a'
128128

129129

130-
select name
131-
from world
132-
where name like '%a%' and
133-
name like '%e%' and
134-
name like '%i%' and
135-
name like '%o%' and
136-
name like '%u%' and
137-
name not like '% %'
130+
SELECT name
131+
FROM world
132+
WHERE name LIKE '%a%' AND
133+
name LIKE '%e%' AND
134+
name LIKE '%i%' AND
135+
name LIKE '%o%' AND
136+
name LIKE '%u%' AND
137+
name NOT LIKE '% %'

select_within_select_tutorial.sql

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
-- Bigger than Russia
2+
-- 1. List each country name where the population is larger than that of 'Russia'.
3+
4+
SELECT name
5+
FROM world
6+
WHERE population > (SELECT population
7+
FROM world
8+
WHERE name='Russia')
9+
10+
11+
-- Richer than UK
12+
-- 2. Show the countries in Europe with a per capita GDP greater than 'United Kingdom'.
13+
14+
SELECT name
15+
FROM world
16+
WHERE continent = 'Europe'
17+
AND gdp/population > (SELECT gdp/population
18+
FROM world
19+
WHERE name = 'United Kingdom')
20+
21+
22+
-- Neighbours of Argentina and Australia
23+
-- 3. List the name and continent of countries in the continents containing either Argentina or Australia. Order by name of the country.
24+
25+
SELECT name, continent FROM world
26+
WHERE continent IN (SELECT continent
27+
FROM world
28+
WHERE name IN ('Argentina', 'Australia'))
29+
order by name asc
30+
31+
32+
-- Between Canada and Poland
33+
-- 4. Which country has a population that is more than United Kingdom but less than Germany? Show the name and the population.
34+
35+
SELECT name, population
36+
FROM world
37+
WHERE population > (SELECT population
38+
FROM world
39+
WHERE name = 'United Kingdom')
40+
AND population < (SELECT population
41+
FROM world
42+
WHERE name = 'Germany')
43+
ORDER BY name ASC
44+
45+
46+
-- Percentages of Germany
47+
-- 5. 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.
48+
49+
/*
50+
51+
Show the name and the population of each country in Europe. Show the population as a percentage of the population of Germany.
52+
53+
The format should be Name, Percentage for example:
54+
... ... ... ...
55+
name percentage
56+
Albania 3%
57+
Andorra 0%
58+
Austria 11%
59+
... ... ... ...
60+
61+
Decimal places
62+
Percent symbol %
63+
64+
*/
65+
66+
SELECT name, CONCAT(ROUND(population / (SELECT population
67+
FROM world
68+
WHERE name = 'Germany')*100,0),'%') AS percentage
69+
FROM world WHERE continent = 'Europe'
70+
71+
72+
73+
-- Bigger than every country in Europe
74+
-- 6. Which countries have a GDP greater than every country in Europe? [Give the name only.] (Some countries may have NULL gdp values)
75+
76+
SELECT name
77+
FROM world
78+
WHERE gdp > ALL(SELECT gdp
79+
FROM world
80+
WHERE gdp > 0 AND continent = 'Europe')
81+
82+
83+
84+
85+
-- Largest in each continent
86+
-- 7. Find the largest country (by area) in each continent, show the continent, the name and the area:
87+
88+
SELECT continent, name, area
89+
FROM world x
90+
WHERE area >= ALL (SELECT area
91+
FROM world y
92+
WHERE y.continent=x.continent AND area>0)
93+
94+
95+
96+
-- First country of each continent (alphabetically)
97+
-- 8. List each continent and the name of the country that comes first alphabetically.
98+
99+
SELECT continent, name FROM world x
100+
WHERE name <= ALL (SELECT name
101+
FROM world y
102+
WHERE y.continent=x.continent)
103+
104+
105+
106+
107+
-- Difficult Questions That Utilize Techniques Not Covered In Prior Sections
108+
-- 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.
109+
110+
SELECT name, continent, population
111+
FROM world x
112+
WHERE 25000000 >= ALL(SELECT population
113+
FROM world y
114+
WHERE x.continent = y.continent
115+
AND y.population>0);
116+
117+
118+
119+
-- Three time bigger
120+
-- 10. Some countries have populations more than three times that of all of their neighbours (in the same continent). Give the countries and continents.
121+
122+
SELECT name, continent
123+
FROM world x
124+
WHERE population >= ALL(SELECT population*3
125+
FROM world y
126+
WHERE x.continent = y.continent
127+
and y.name != x.name)

0 commit comments

Comments
 (0)