-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathselect-basics.sql
64 lines (55 loc) · 1.79 KB
/
select-basics.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
/*
First section of sqlzoo, SELECT basics
*/
--#1
/*
The example shows the population of 'France'. Strings should be in 'single quotes';
Show the population of Germany
*/
SELECT population
FROM world
WHERE name = 'Germany'
--#2
/*
The query shows the population density population/area for each country where the area is over 5,000,000 km2.
Show the name and per capita gdp: gdp/population for each country where the area is over 5,000,000 km2
*/
SELECT name, gdp/population
FROM world
WHERE area > 5000000
--#3
/*
Where to find some very small, very rich countries.
We use AND to ensure that two or more conditions hold true.
The example shows the countries where the population is small and the gdp is high.
Show the name and continent where the area is less than 2000 and the gdp is more than 5000000000
*/
SELECT name , continent
FROM world
WHERE area < 2000
AND gdp > 5000000000
--#4
/*
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 'Ireland', 'Iceland' and 'Denmark'
Show the name and the population for 'Denmark', 'Finland', 'Norway', 'Sweden'
*/
SELECT name, population
FROM world
WHERE name IN ('Norway', 'Sweden', 'Finland',
'Denmark')
--#5
/*
What are the countries beginning with G? The word LIKE permits pattern matching - % is the wildcard. The examples shows countries beginning with D
Show each country that begins with G
*/
SELECT name
FROM world
WHERE name LIKE 'G%'
--#6
/*
Which countries are not too small and not too big? Show the country and the area for countries with an area between 200000 and 250000. BETWEEN allows range checking - note that it is inclusive.
Show the area in 1000 square km. Show area/1000 instead of area
*/
SELECT name, area/1000
FROM world
WHERE area BETWEEN 200000 AND 250000