1a. List each country name where the population is larger than 'Russia'.
SELECT name FROM bbc WHERE population> (SELECT population FROM bbc WHERE name='Russia')
1b. List the name and region of countries in the regions containing 'India', 'Iran'.
select name ,region from bbc where region in (SELECT region from bbc where name in ('India','Iran'))
1c. Show the European countries with a per capita GDP greater than 'United Kingdom'.
select name from bbc where region = (select region from bbc where name = 'United Kingdom') and gdp/population > (Select gdp/population from bbc where name = 'United Kingdom')
1d. Which country has a population that is more than Canada but less than Algeria?
select name from bbc where population > (Select population from bbc where name = 'canada') and population <(select population from bbc where name = 'algeria')
2a. Which countries have a GDP greater than any European country? [Give the name only.]
select name from bbc where gdp> (select max(gdp) from bbc where region = 'europe')
3a. Find the largest country in each region:
SELECT region, name, population FROM bbc x
WHERE population >= ALL
(SELECT population FROM bbc y
WHERE y.region=x.region
AND population>0)
Thursday, September 13, 2007
Friday, September 7, 2007
BBC Country Profile: Aggregate functions
1a. Show the total population of the world.
Select sum(population) from bbc;
1b. List all the regions - just once each.
Select distinct(region) from bbc;
1c. Give the total GDP of Africa
Select sum(gdp) from bbc
where region = 'Africa';
1d. How many countries have an area of at least 1000000
select count(region) from bbc where area >= 1000000;
1e. What is the total population of ('France','Germany','Spain')
select sum(population) from bbc
where name in('France','Germany','Spain');
Select sum(population) from bbc;
1b. List all the regions - just once each.
Select distinct(region) from bbc;
1c. Give the total GDP of Africa
Select sum(gdp) from bbc
where region = 'Africa';
1d. How many countries have an area of at least 1000000
select count(region) from bbc where area >= 1000000;
1e. What is the total population of ('France','Germany','Spain')
select sum(population) from bbc
where name in('France','Germany','Spain');
GROUP BY
In a GROUP BY statement only distinct values are shown for the column in the GROUP BY.
This example shows the continents hosting the Olympics with the count of the number of games held.
SELECT continent, COUNT(yr) FROM games
GROUP BY continent;
This example shows the continents hosting the Olympics with the count of the number of games held.
SELECT continent, COUNT(yr) FROM games
GROUP BY continent;
BBC table
name | region | area | population | gdp |
---|---|---|---|---|
Afghanistan | Asia | 652225 | 26000000 | |
Albania | Europe | 28728 | 3200000 | 6656000000 |
Algeria | Middle East | 2400000 | 32900000 | 75012000000 |
Andorra | Europe | 468 | 64000 | |
Angola | Africa | 1250000 | 14500000 | 14935000000 |
Antigua and Barbuda | Americas | 442 | 77000 | 770000000 |
Argentina | South America | 2800000 | 39300000 | 146196000000 |
Armenia | Europe | 29743 | 3000000 | 3360000000 |
Australia | Australasia | 7700000 | 20300000 | 546070000000 |
Austria | Europe | 83871 | 8100000 | 261630000000 |
Azerbaijan | Europe | 86600 | 8500000 | |
Bahamas | Americas | 13939 | 321000 | 4789320000 |
Bahrain | Middle East | 717 | 754000 | 9357140000 |
Bangladesh | Asia | 143998 | 152600000 | 67144000000 |
Barbados | Americas | 430 | 272000 | 2518720000 |
Belarus | Europe | 207595 | 9800000 | 20776000000 |
Belgium | Europe | 30528 | 10300000 | 319609000000 |
Belize | Americas | 22965 | 266000 | |
Benin | Africa | 112622 | 7100000 | 3763000000 |
Bhutan | Asia | 38364 | 2400000 | 1824000000 |
Bolivia | Americas | 1100000 | 9100000 | |
Bosnia-Hercegovina | Europe | 51129 | 4200000 | 8568000000 |
Botswana | Africa | 581730 | 1800000 | 7812000000 |
Brazil | South America | 8550000 | 182800000 | 564852000000 |
Brunei | Asia | 5765 | 374000 | |
Bulgaria | Europe | 110994 | 7800000 | 21372000000 |
Burkina Faso | Africa | 274200 | 13800000 | 4968000000 |
Burma | Asia | 676552 | 50700000 | |
Burundi | Africa | 27816 | 7300000 | |
Cambodia | Asia | 181035 | 14800000 | 4736000000 |
Cameroon | Africa | 465458 | 16600000 | 13280000000 |
Canada | North America | 9900000 | 32000000 | 908480000000 |
Cape Verde | Africa | 4033 | 482000 | 853140000 |
Central African Republic | Africa | 622984 | 3900000 | |
Chad | Africa | 1280000 | 9100000 | 2366000000 |
Chile | South America | 756096 | 16200000 | 79542000000 |
China | Asia | 9600000 | 1300000000 | 1677000000000 |
Colombia | Americas | 1140000 | 45600000 | |
Comoros | Africa | 1862 | 812000 | |
Costa Rica | Americas | 51100 | 4300000 | |
Croatia | Europe | 56594 | 4400000 | 28996000000 |
Cuba | Americas | 110860 | 11300000 | |
Cyprus | Europe | 9250 | 807000 | 14187060000 |
Czech Republic | Europe | 78866 | 10200000 | 93330000000 |
Democratic Republic of Congo | Africa | 2340000 | 56000000 | 6720000000 |
Denmark | Europe | 43098 | 5400000 | 219510000000 |
Djibouti | Africa | 23200 | 721000 | |
Dominica | Americas | 751 | 71000 | 259150000 |
Dominican Republic | Americas | 48072 | 9000000 | |
East Timor | Asia | 14609 | 857000 | |
Results truncated... |
Simple use of SELECT and WHERE
attribute-list
-
- This is usually a comma separated list of attributes (field names)
- Expressions involving these attributes may be used. The normal mathematical operators
+, -, *, /
may be used on numeric values. String values may be concatenated using||
- To select all attributes use *
- The attributes in this case are:
name
,region
,area
,population
andgdp
-
table-name
- In these examples the table is always
bbc
.
- In these examples the table is always
-
condition
-
- This is a boolean expression which each row must satisfy.
- Operators which may be used include
AND
,OR
,NOT
, >, >=, =, <, <= - The
LIKE
operator permits strings to be compared using 'wild cards'. The symbols _ and % are used to represent a single character or a sequence of characters. Note thatMS Access SQL
uses?
and*
instead of_
and%
. - The
IN
operator allows an item to be tested against a list of values. - There is a
BETWEEN
operator for checking ranges.
A common mistake made with boolean expressions
Boolean expressions often read like "normal" English however the syntax is not as rich. For example we might say name equals 'France' or 'Germany'
however the corresponding boolean expression is name='France' OR name='Germany'
Subscribe to:
Posts (Atom)