Skip to content

Commit 72a9e64

Browse files
committed
SQL Exercises
0 parents  commit 72a9e64

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed

basics.sql

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-- 1. Introducing the world table of countries
2+
--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
3+
4+
SELECT population
5+
FROM world
6+
WHERE name = 'Germany'
7+
8+
9+
-- 2. Scandinavia
10+
-- 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'.
11+
12+
SELECT name, population
13+
FROM world
14+
WHERE name IN ('Sweden', 'Norway', 'Denmark');
15+
16+
17+
18+
-- 3. Just the right size
19+
-- 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.
20+
21+
SELECT name, area
22+
FROM world
23+
WHERE area BETWEEN 200000 AND 250000

select_from_world.sql

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
-- 1. Introduction
2+
-- Read the notes about this table. Observe the result of running this SQL command to show the name, continent and population of all countries.
3+
4+
SELECT name, continent, population
5+
FROM world
6+
7+
8+
9+
-- 2. Large Countries
10+
-- 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.
11+
12+
SELECT name
13+
FROM world
14+
WHERE population >= 200000000
15+
16+
17+
18+
-- 3. Per capita GDP
19+
-- Give the name and the per capita GDP for those countries with a population of at least 200 million.
20+
21+
select name, gdp/population
22+
from world
23+
where population > 200000000
24+
25+
26+
27+
-- 4. South America In millions
28+
-- 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.
29+
30+
select name, population/1000000
31+
from world
32+
where continent = 'South America'
33+
34+
35+
36+
37+
-- 5. France, Germany, Italy
38+
-- Show the name and population for France, Germany, Italy
39+
40+
select name, population
41+
from world
42+
where name in ('France', 'Germany', 'Italy')
43+
44+
45+
46+
-- 6. United
47+
-- Show the countries which have a name that includes the word 'United'
48+
49+
select name
50+
from world
51+
where name like 'United%'
52+
53+
54+
55+
-- 7. Two ways to be big
56+
-- 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.
57+
58+
select name, population, area
59+
from world
60+
where area > 3000000 or population > 250000000
61+
62+
63+
64+
65+
-- 8. One or the other (but not both)
66+
-- 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.
67+
-- Australia has a big area but a small population, it should be included.
68+
-- Indonesia has a big population but a small area, it should be included.
69+
-- China has a big population and big area, it should be excluded.
70+
-- United Kingdom has a small population and a small area, it should be excluded.
71+
72+
73+
select name, population, area
74+
from world
75+
where (area > 3000000 or population > 250000000)
76+
and not (area > 3000000 and population > 250000000)
77+
78+
79+
80+
-- 9. rounding
81+
-- 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.
82+
83+
84+
SELECT name, round(population/1000000,2) as Population , round(gdp/1000000000, 2) as GDP
85+
FROM world
86+
WHERE continent = 'South America'
87+
88+
89+
90+
-- 10. Trillion dollar economies
91+
-- 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.
92+
93+
SELECT name, round(gdp/population, -3)
94+
FROM world
95+
WHERE gdp > 1000000000000
96+
97+
98+
99+
-- 11. Name and capital have the same length
100+
-- Greece has capital Athens.
101+
-- Each of the strings 'Greece', and 'Athens' has 6 characters.
102+
-- Show the name and capital where the name and the capital have the same number of characters.
103+
-- You can use the LENGTH function to find the number of characters in a string
104+
105+
106+
select name, capital
107+
from world
108+
where length(name) = length(capital)
109+
110+
111+
112+
-- 12. Matching name and capital
113+
-- The capital of Sweden is Stockholm. Both words start with the letter 'S'. 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.
114+
-- You can use the function LEFT to isolate the first character.
115+
-- You can use <> as the not EQUALS operator.
116+
117+
118+
SELECT name, capital
119+
FROM world
120+
WHERE left(name, 1) = left(capital, 1) xor name = capital
121+
122+
123+
124+
-- 13. All the vowels
125+
-- 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. Find the country that has all the vowels and no spaces in its name.
126+
-- You can use the phrase name not like '%a%' to exclude characters from your results.
127+
-- The query shown misses countries like Bahamas and Belarus because they contain at least one 'a'
128+
129+
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 '% %'

0 commit comments

Comments
 (0)