world
name continent area population gdp
Afghanistan Asia 652230 25500100 20343000000
Albania Europe 28748 2831741 12960000000
Algeria Africa 2381741 37100000 188681000000
Andorra Europe 468 78115 3712000000
Angola Africa 1246700 20609294 100990000000
…
Introducing the world table of countries
Some questions concerning basic SQL statements
name region area population gdp
Afghanistan South Asia 652225 26000000
Albania Europe 28728 3200000 6656000000
Algeria Middle East 2400000 32900000 75012000000
Andorra Europe 468 64000
…
Select the code which produces this table
name population
Bahrain 1234571
Swaziland 1220000
Timor-Leste 1066409
SELECT name, population FROM world WHERE population BETWEEN 1000000 AND 1250000
BETWEEN allows range checking (range specified is inclusive of boundary values).
Pick the result you would obtain from this code:
SELECT name, population
FROM world
WHERE name LIKE “Al%”
Albania 3200000
Algeria 32900000
Table-E
Select the code which shows the countries that end in A or L
SELECT name FROM world WHERE name LIKE ‘%a’ OR name LIKE ‘%l’
“%”occupies the space of the items that we do not show in the checking quotations.
Pick the result from the query
SELECT name,length(name)
FROM world
WHERE length(name)=5 and region=‘Europe’
name length(name)
Italy 5
Malta 5
Spain 5
Pick the result you would obtain from this code:
SELECT name, area*2 FROM world WHERE population = 64000
Andorra 936
Select the code that would show the countries with an area larger than 50000 and a population smaller than 10000000
SELECT name, area, population FROM world WHERE area > 50000 AND population < 10000000
Select the code that shows the population density of China, Australia, Nigeria and France
SELECT name, population/area FROM world WHERE name IN (‘China’, ‘Nigeria’, ‘France’, ‘Australia’)
Checking a list The word IN allows us to check if an item is in a list.
Some points to note:
• There is a table called world with nearly 200 records.
• The first record is Afghanistan in Asia
• The final record is Zimbabwe in Africa.
• The table has eight columns or attributes . These are:
Field Type Null Key Default Notes
name varchar(50) The English name of the country - this is the primary key.
continent varchar(60) Not strictloy continent - but these include all your favourite continents (Europe, Asia etc) plus a few others.
area decimal(10,0) YES null
population decimal(11,0) YES null
gdp decimal(14,0) YES null
capital varchar(60) YES null
tld varchar(5) YES null
flag varchar(255) YES null
An extract from the table world
name region area population gdp
Yemen Middle East 536869 21500000 12255000000
Zambia Africa 752614 11000000 4950000000
Zimbabwe Africa 390759 12900000 6192000000
Footnotes
Per Capita GDP
The per capita GDP is the GDP divided by the population. It is a rough measure of the average salary. A rich country is one with a high per capita GDP.
Original source
This data has been collected from http://www.wikidata.org - it may be a little out of date. The latest version can be found at Some changes have been made to the data - the reader should assume that errors and omissions are due to me and not the most excellent wikidata site.
Area
The area is measured in square kilometers.
GDP
Scientific Notation
Some implementations of SQL may show large numbers in scientific notation. The number 6.23 E9 means 6.23 x 10 9 which is 6.23 x 1,000,000,000 which is 6,230,000,000
A Million is 10 6 which is 1,000,000
A Billion is 10 9 which is one thousand million which is 1,000,000,000
A Trillion is 10 12 , which is one million million which is 1,000,000,000,000.
nobel
yr subject winner
1960 Chemistry Willard F. Libby
1960 Literature Saint-John Perse
1960 Medicine Sir Frank Macfarlane Burnet
1960 Medicine Peter Madawar
…
SELECT yr, subject, winner
FROM nobel
WHERE yr = 1950
SELECT winner
FROM nobel
WHERE yr = 1962
AND subject = ‘Literature’
SELECT yr, subject
FROM nobel
WHERE winner = ‘Albert Einstein’
SELECT winner
FROM nobel
WHERE yr >= 2000 AND subject = ‘Peace’
SELECT *
FROM nobel
WHERE subject = ‘Literature’
AND (yr >= 1980 AND yr <= 1989)
Show all details of the presidential winners:
Theodore Roosevelt
Woodrow Wilson
Jimmy Carter
Barack Obama
SELECT *
FROM nobel
WHERE winner IN (‘Theodore Roosevelt’, ‘Woodrow Wilson’, ‘Jimmy Carter’, ‘Barack Obama’)
Show the winners with first name John.
SELECT winner
FROM nobel
WHERE winner LIKE ‘John%’
SELECT *
FROM nobel
WHERE (yr = 1980 AND subject = ‘Physics’)
OR (yr = 1984 AND subject = ‘Chemistry’)
SELECT *
FROM nobel
WHERE yr = 1980
AND subject NOT IN (‘Chemistry’, ‘Medicine’)
SELECT *
FROM nobel
WHERE (yr < 1910 AND subject = ‘Medicine’)
OR (yr >= 2004 AND subject = ‘Literature’)
SELECT *
FROM nobel
WHERE winner = ‘PETER GRÜNBERG’
/*
Ü这个符号怎么打?按住ALT键,通过数字小键盘依次按‘0220’即可。
*/
SELECT *
FROM nobel
WHERE winner = ‘EUGENE O’NEILL’
SELECT winner, yr, subject
FROM nobel
WHERE winner LIKE ‘Sir%’
ORDER BY yr DESC, winner
SELECT winner, subject
FROM nobel
WHERE yr=1984
ORDER BY subject IN (‘Chemistry’,‘Physics’), subject, winner
–要先保证’Chemistry’,'Physics’在后面,所以优先排序。
Using SELECT in SELECT
See SELECT FROM SELECT for how to use a derived table.
The result of a SELECT statement may be used as a value in another statement. For example the statement SELECT continent FROM world WHERE name = ‘Brazil’ evaluates to ‘South America’ so we can use this value to obtain a list of all countries in the same continent as ‘Brazil’
1.
List each country in the same continent as ‘Brazil’.
Some versions of SQL insist that you give the subquery an alias. Simply put AS somename after the closing bracket:
SELECT name FROM world WHERE continent =
(SELECT continent FROM world WHERE name=‘Brazil’) AS brazil_continent
The subquery may return more than one result - if this happens the query above will fail as you are testing one value against more than one value. It is safer to use IN to cope with this possibility.
The phrase (SELECT continent FROM world WHERE name = ‘Brazil’ OR name=‘Mexico’) will return two values (‘North America’ and ‘South America’). You should use:
SELECT name, continent FROM world
WHERE continent IN
(SELECT continent FROM world WHERE name=‘Brazil’
OR name=‘Mexico’)
2.
List each country and its continent in the same continent as ‘Brazil’ or ‘Mexico’.
If you are certain that only one value will be returned you can use that query on the SELECT line.
3.
Show the population of China as a multiple of the population of the United Kingdom
These operators are binary - they normally take two parameters:
= equals
greater than
< less than
= greater or equal
<= less or equal
You can use the words ALL or ANY where the right side of the operator might have multiple values.
Show each country that has a population greater than the population of ALL countries in Europe.
Note that we mean greater than every single country in Europe; not the combined population of Europe.
This tutorial looks at how we can use SELECT statements within SELECT statements to perform more complex queries.
name continent area population gdp
Afghanistan Asia 652230 25500100 20343000000
Albania Europe 28748 2831741 12960000000
Algeria Africa 2381741 37100000 188681000000
Andorra Europe 468 78115 3712000000
Angola Africa 1246700 20609294 100990000000
…
SELECT name FROM world
WHERE population >
(SELECT population FROM world
WHERE name = ‘Russia’)
SELECT name FROM world
WHERE continent = ‘Europe’ AND
gdp/population >
(SELECT gdp/population FROM world
WHERE name = ‘United Kingdom’)
SELECT name, continent FROM world
WHERE continent IN
(SELECT continent FROM world
WHERE name IN (‘Argentina’, ‘Australia’))
ORDER BY name
SELECT name, population FROM world
WHERE population >
(SELECT population FROM world
WHERE name = ‘Canada’) AND
population < (SELECT population FROM world
WHERE name = ‘Poland’)
SELECT name,CONCAT(ROUND(population/
(SELECT population FROM world
WHERE name = ‘Germany’)*100, 0), ‘%’) FROM world
WHERE continent = ‘Europe’
SELECT name FROM world
WHERE gdp > ALL(SELECT gdp FROM world
WHERE continent = ‘Europe’ AND gdp > 0 )
SELECT continent, name, area FROM world x
WHERE x.area >=
ALL(SELECT area FROM world y
WHERE x.continent = y.continent
AND y.area > 0)
SELECT continent,name FROM world x
WHERE x.name=(SELECT name FROM world y
WHERE y.continent=x.continent ORDER BY name
LIMIT 1)
SELECT name, continent, population FROM world x
WHERE 25000000 >= ALL(SELECT population FROM world y
WHERE x.continent = y.continent
AND y.population > 0)
SELECT name, continent FROM world x
WHERE population/3 >= ALL(SELECT population FROM world y
WHERE x.continent = y.continent AND
x.name != y.name
AND y.population > 0)
The functions SUM, COUNT, MAX and AVG are “aggregates”, each may be applied to a numeric attribute resulting in a single row being returned by the query. (These functions are even more useful when used with the GROUP BY clause.)
By default the result of a SELECT may contain duplicate rows. We can remove these duplicates using the DISTINCT key word.
ORDER BY permits us to see the result of a SELECT in any particular order. We may indicate ASC or DESC for ascending (smallest first, largest last) or descending order.
1.
The total population and GDP of Europe.
What are the regions?
Show the name and population for each country with a population of more than 100000000. Show countries in descending order of population.
This tutorial is about aggregate functions such as COUNT, SUM and AVG. An aggregate function takes many values and delivers just one value. For example the function SUM would aggregate the values 2, 4 and 5 to deliver the single value 11.
name continent area population gdp
Afghanistan Asia 652230 25500100 20343000000
Albania Europe 28748 2831741 12960000000
Algeria Africa 2381741 37100000 188681000000
Andorra Europe 468 78115 3712000000
Angola Africa 1246700 20609294 100990000000
Show the total population of the world.
SELECT SUM(population) FROM world
List all the continents - just once each.
SELECT DISTINCT continent FROM world
Give the total GDP of Africa
SELECT SUM(gdp) FROM world
WHERE continent = ‘Africa’
How many countries have an area of at least 1000000
SELECT COUNT(name) FROM world
WHERE area >= 1000000
What is the total population of (‘Estonia’, ‘Latvia’, ‘Lithuania’)
SELECT SUM(population) FROM world
WHERE name IN
(‘Estonia’, ‘Latvia’, ‘Lithuania’)
For each continent show the continent and number of countries.
SELECT continent, COUNT(name) FROM world
GROUP BY continent
For each continent show the continent and number of countries with populations of at least 10 million.
SELECT continent, COUNT(name) FROM world
WHERE population > 10000000
GROUP BY continent
List the continents that have a total population of at least 100 million.
SELECT continent FROM world
GROUP BY continent
HAVING SUM(population) >= 100000000
总结:
注意理解WHERE,GROUP BY和HAVING的不同。
game
id mdate stadium team1 team2
1001 8 June 2012 National Stadium, Warsaw POL GRE
1002 8 June 2012 Stadion Miejski (Wroclaw) RUS CZE
1003 12 June 2012 Stadion Miejski (Wroclaw) GRE CZE
1004 12 June 2012 National Stadium, Warsaw POL RUS
…
goal
matchid teamid player gtime
1001 POL Robert Lewandowski 17
1001 GRE Dimitris Salpingidis 51
1002 RUS Alan Dzagoev 15
1002 RUS Roman Pavlyuchenko 82
…
eteam
id teamname coach
POL Poland Franciszek Smuda
RUS Russia Dick Advocaat
CZE Czech Republic Michal Bilek
GRE Greece Fernando Santos
…
This tutorial introduces JOIN which allows you to use data from two or more tables. The tables contain all matches and goals from UEFA EURO 2012 Football Championship in Poland and Ukraine.
The data is available (mysql format) at http://sqlzoo.net/euro2012.sql
This tutorial introduces the notion of a join. The database consists of three tables movie , actor and casting .
movie
id title yr director budget gross
actor
id name
This database features two entities (movies and actors) in a many-to-many relation. Each entity has its own table. A third table, casting , is used to link them. The relationship is many-to-many because each film features many actors and each actor has appeared in many films.
Field name Type Notes
id INTEGER An arbitrary unique identifier
title CHAR(70) The name of the film - usually in the language of the first release.
yr DECIMAL(4) Year of first release.
director INT A reference to the actor table.
budget INTEGER How much the movie cost to make (in a variety of currencies unfortunately).
gross INTEGER How much the movie made at the box office.
Example
id title yr director budget gross
10003 “Crocodile” Dundee II 1988 38 15800000 239606210
10004 'Til There Was You 1997 49 10000000
Field name Type Notes
id INTEGER An arbitrary unique identifier
name CHAR(36) The name of the actor (the term actor is used to refer to both male and female thesps.)
Example
id name
20 Paul Hogan
50 Jeanne Tripplehorn
Field name Type Notes
movieid INTEGER A reference to the movie table.
actorid INTEGER A reference to the actor table.
ord INTEGER The ordinal position of the actor in the cast list. The
star of the movie will have ord value 1 the co-star will have
value 2, …
Example
movieid actorid ord
10003 20 4
10004 50 1