文章来源:
SQL练习题
网址如下:https://sqlzoo.net
1.SELECT Basics
2.SELECT Quiz
3.SELECT from WORLD Tutorial
4.BBC quiz
5. SELECT from nobel
6. Nobel quiz(3,5题较难)
3. Pick the code that shows the amount of years where no Medicine awards were given
5.Select the code which would show the year when neither a Physics or Chemistry award was given
1.SELECT Basics
Introducing the world table of countries
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
select population from world where name = 'Germany';
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'.
2.Show the name and the population for 'Sweden', 'Norway' and 'Denmark'.
select name,population from world where name in ('Sweden','Norway','Denmark');
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.
3.Modify it to show the country and the area for countries with an area between 200,000 and 250,000.
select name,area from world where area between 200000 and 250000;
2.SELECT Quiz
Select the code which produces this table
name population
Bahrain 1234571
Swaziland 1220000
Timor-Leste 1066409
答案为:
select name,population where population between 1000000 and 1250000;
Pick the result you would obtain from this code:
SELECT name, population
FROM world
WHERE name LIKE 'Al%'
输出结果为:
Table-E
Albania 3200000
Algeria 32900000
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';
4.Pick the result from the query
SELECT name,length(name)
FROM world
WHERE length(name)=5 and continent='Europe'
输出结果为:
name length(name)
Italy 5
Malta 5
Spain 5
5.Here are the first few rows of the world table:
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
...
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 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','Australia','Nigeria','France');
3.SELECT from WORLD Tutorial
1.Observe the result of running this SQL command to show the name, continent and population of all countries.
select name,continent,population from world;
2.Show the name for the countries that have a population of at least 200 million. 200 million is 200000000, there are eight zeros.
select name from world where population > 200000000;
3.Give the name and the per capita GDP for those countries with a population of at least 200 million.
select name,gdp/population from world where population > 200000000;
4.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.
select name,population 1000000 from world where continent = 'South America';
5.Show the name and population for France, Germany, Italy
select name,population from world where name in ('France','Germany','Italy');
6.Show the countries which have a name that includes the word 'United'
select name from world where name like 'United';
7.Show the countries that are big by area or big by population. Show name, population and area.
select name,population,area from world where area > 3000000 or population > 250000000;
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.
select name,population, area from world where area > 3000000 or population > 250000000;
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.
select name,round(population/1000000,2),round(gdp/1000000000,2) from world where continent = 'South America';
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.
select name,round(gdp/population,-3) from world where gdp > 1000000000000;
The number of decimal places may be negative, this will round to the nearest 10 (when p is -1) or 100 (when p is -2) or 1000 (when p is -3) etc..
注意:需要注意round
函数的用法
ROUND(7253.86, 0) -> 7254
ROUND(7253.86, 1) -> 7253.9
ROUND(7253.86,-3) -> 7000
floor function:FLOOR(f) returns the integer value of f FLOOR(f) give the integer that is equal to, or just less than f. FLOOR always rounds down.
FLOOR(2.7) -> 2
FLOOR(-2.7) -> -3
CEIL function:CEIL(f) is ceiling, it returns the integer that is equal to or just more than fCEIL(f) give the integer that is equal to, or just higher than f. CEIL always rounds up.
CEIL(2.7) -> 3
CEIL(-2.7) -> -2
Mod function:MOD(a,b) returns the remainder when a is divied by b. If you use MOD(a, 2) you get 0 for even numbers and 1 for odd numbers. If you use MOD(a, 10) you get the last digit of the number a.
MOD(27,2) -> 1
MOD(27,10) -> 7
Show the name and capital where the name and the capital have the same number of characters.
select name,capital from world where len(name) = len(capital);
注意: 字符长度的函数需要使用
len
来处理
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.
select name,capital where left(capital,1) = left(name,1) and name != capital;
注意:
left
: LEFT(s,n) allows you to extract n characters from the start of the string s.
LEFT('Hello world', 4) -> 'Hell'
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.
select name from world where name like '%a%' and name like '%e%' and name like '%i%' and name like '%o%' and name like '%u%' and name not like '% %';
4.BBC quiz
Select the code which gives the name of countries beginning with U
select name from world where name like 'U%';
2.Select the code which shows just the population of United Kingdom?
select population from world where name = 'United Kingdom';
3.Select the answer which shows the problem with this SQL code - the intended result should be the continent of France:
select continent from world where name = 'France';
4.Select the result that would be obtained from the following code:
select name,population/10 from world where population
结果为:
Nauru 990
5.Select the code which would reveal the name and population of countries in Europe and Asia 答案为:
select name,population from world where continent in ('Europe','Asia');
6.Select the code which would give two rows
select name from world where name in ('Cuba','Togo');
7.Select the result that would be obtained from this code: 题目:
select name from world where continent = 'South America' and population > 40000000;
输出结果为:
Brazil
Colombia
5. SELECT from nobel
Change the query shown so that it displays Nobel prizes for 1950.
select * from nobel where yr = 1950;
Show who won the 1962 prize for Literature.
select winner from nobel where yr = 1962 and subject = 'Literature';
3.Show the year and subject that won 'Albert Einstein' his prize.
select yr,subject from nobel where winner = 'Albert Einstein';
4.Give the name of the 'Peace' winners since the year 2000, including 2000.
select winner from nobel where subject = 'Peace' and yr >= 2000;
5.Show all details (yr, subject, winner) of the Literature prize winners for 1980 to 1989 inclusive.
select yr,subject,winner from nobel where subject = 'Literature' and yr between 1980 and 1989;
6.Show all details of the presidential winners:
Theodore Roosevelt Woodrow Wilson Jimmy Carter Barack Obama
select yr,subject,winner from nobel where winner in ('Theodore Roosevelt','Woodrow Wilson','Jimmy Carter','Barack Obama');
7.Show the winners with first name John
select winner from nobel where winner like 'John %';
8.Show the year, subject, and name of Physics winners for 1980 together with the Chemistry winners for 1984.
select yr,subject,winner from nobel where subject = 'Physics' and yr = 1980 or subject = 'Chemistry' and yr = 1984;
9.Show the year, subject, and name of winners for 1980 excluding Chemistry and Medicine
select yr,subject,winner from nobel where yr = 1980 and subject not in ('Chemistry','Medicine')
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)
select * from nobel where (yr < 1910 and subject = 'Medicine') or (yr >= 2004 and subject = 'Literature');
11.Find all details of the prize won by PETER GRÜNBERG
select * from nobel where winner = 'PETER GRÜNBERG';
12.Find all details of the prize won by EUGENE O'NEILL
select * from nobel where winner = 'EUGENE O''NEILL'
13.List the winners, year and subject where the winner starts with Sir. Show the the most recent first, then by name order.
select winner,yr,subject from nobel where winner like 'Sir%' order by yr desc,winner;
The expression subject IN ('Chemistry','Physics') can be used as a value - it will be 0 or 1.
14.Show the 1984 winners and subject ordered by subject and winner name; but list Chemistry and Physics last.
select winner,yr,subject from nobel where yr = 1984 order by subject in ('Chemistry','Physics'),subject,winner;
注释: 第八题第九题、不是很懂需要重视, 第十题注意写一个子查询,主要条件的填写 13题需要注意desc的用法 14比较难知识点不好把握:将subject in ('Chemistry','Physics')作为一个0和1的值。放在order by后面进行0和1排序,所以会先排非化学物理,后排是化学物理,从而达到将物理化学排在最后的目标。
6. Nobel quiz
1.Pick the code which shows the name of winner's names beginning with C and ending in n
select name from nobel where winner like 'C%' and winner like '%n';
2.Select the code that shows how many Chemistry awards were given between 1950 and 1960
select count(subject) from nobel where subject = 'Chemistry' and yr between 1950 and 1960;
3. Pick the code that shows the amount of years where no Medicine awards were given
select count(distinct yr) from nobel where yr not in (select distinct yr from nobel where subject = 'Medicine');
4.Select the result that would be obtained from the following code:
select subject,winner from nobel where winner like 'Sir%' and yr like '196%';
输出结果为:
Medicine Sir John Eccles
Medicine Sir Frank Macfarlane Burnet
5.Select the code which would show the year when neither a Physics or Chemistry award was given
select yr from nobel
where yr not in
(select yr from nobel where subject in ('Chemistry','Physics'))
6.Select the code which shows the years when a Medicine award was given but no Peace or Literature award was
select distinct yr from nobel
where subject = 'Medicine'
and yr not in (select yr from nobel where subject = 'Literature')
and yr not in (select yr from nobel where subject = 'Peace')
7.Pick the result that would be obtained from the following code:
select subject,count(subject) from nobel where yr = '1960' group by subject
输出结果为:
Chemistry 1
Literature 1
Medicine 2
Peace 1
Physics 1
(完)




