SQLZOO select 基础练习笔记

一、SELECT basics

world

1.Modify it to show the population of Germany

select population from world where name='Germany 

2.Show the name and the population for 'Sweden', 'Norway' and 'Denmark'.

select name,population 
from world 
where name in( 'Sweden', 'Norway','Denmark')

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

二、SELECT from WORLD Tutorial


world

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.how 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. 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.

select name,population,area 
from world 
where area > 3000000 or population >250000000

8.Exclusive OR (XOR). Show the countries that are big by area or big by population but not both. Show name, population and area.

  • Australia has a big area but a small population, it should be included.
  • Indonesia has a big population but a small area, it should be included.
  • China has a big population and big area, it should be excluded.
  • United Kingdom has a small population and a small area, it should be excluded.
select name,population,area 
from world 
where area > 3000000 xor population >250000000

9. 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.

select name,round(population/1000000,2),round(gdp/1000000000,2) 
from world where continent='South America'

10.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

11.Greece has capital Athens.

Each of the strings 'Greece', and 'Athens' has 6 characters.

Show the name and capital where the name and the capital have the same number of characters.

You can use the LENGTH function to find the number of characters in a string

select name,capital from world where length(name)=length(capital)

12. 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.

You can use the function LEFT to isolate the first character.

You can use <> as the NOT EQUALS operator.

select name,capital 
from world  
where left(name,1)=left(capital,1) and name<>capital

13.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.

14.Find the country that has all the vowels and no spaces in its name.

You can use the phrase name NOT LIKE '%a%' to exclude characters from your results.

The query shown misses countries like Bahamas and Belarus because they contain at least one 'a'

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'% %'

三、SELECT from Nobel Tutorial

Nobel

1.Change the query shown so that it displays Nobel prizes for 1950.

select yr, subject, winner   from nobel   where yr=1950

2.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 * 
from nobel 
where winner in('Theodore Roosevelt','Woodrow Wilson','Jimmy Carter','Barack Obama')
  1. Show the winners with first name John
select winner from nobel where winner like'John%'
  1. 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)
  1. Show the year, subject, and name of winners for 1980 excluding Chemistry and Medicine
select yr,subject,winner
 from nobel 
where subject not in('Chemistry', 'Medicine') and yr=1980
  1. 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 yr,subject,winner 
from nobel 
where (subject='Medicine' and yr < 1910) 
or (subject='Literature' and yr >= 2004)
  1. Find all details of the prize won by PETER GRÜNBERG
select * from nobel where winner='PETER GRÜNBERG'
  1. Find all details of the prize won by EUGENE O'NEILL
select * from nobel where winner like'EUGENE O''NEILL'   #这里引号使用注意'
  1. 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

14.The expression subject IN ('Chemistry','Physics') can be used as a value - it will be 0 or 1.

Show the 1984 winners and subject ordered by subject and winner name; but list Chemistry and Physics last.

select winner, subject
from nobel
where yr=1984
order by subject in ('Physics','Chemistry'),subject,winner

四、SELECT within SELECT Tutorial

This tutorial looks at how we can use SELECT statements within SELECT statements to perform more complex queries.


1.List each country name where the population is larger than that of 'Russia'.

select name 
from world 
where population>(select population from world where name='Russia')
  1. Show the countries in Europe with a per capita GDP greater than 'United Kingdom'.
select name 
from world
where gdp/population > (select gdp/population 
from world where name='United Kingdom' ) and continent='Europe'

3.List the name and continent of countries in the continents containing either Argentina or Australia. Order by name of the country.

select name,continent 
from world
where continent in (select continent 
                   from world where name='Argentina' or name='Australia')
order by name

4.Which country has a population that is more than Canada but less than Poland? Show the name and the population.

select name,population 
from world
where population> (select population from world where name='Canada ')
and population <(select population from world where name='Poland')
  1. Germany (population 80 million) has the largest population of the countries in Europe. Austria (population 8.5 million) has 11% of the population of Germany.

Show the name and the population of each country in Europe. Show the population as a percentage of the population of Germany.

select name,
concat(round(100*population/(select population 
                   from world where name='Germany')),'%')
from world
where continent='Europe'

6.Which countries have a GDP greater than every country in Europe? [Give the name only.] (Some countries may have NULL gdp values)

select name
from world
where gdp > all(select gdp from world
              where continent='Europe' and gdp>0)

7.Find the largest country (by area) in each continent, show the continent, the name and the area:

select continent,name,area from world x
where x.area >= all(select y.area from world y 
where y.continent=x.continent and area>0)
  1. List each continent and the name of the country that comes first alphabetically.
select continent,name from world x
where x.name=(select y.name from world y 
where x.continent=y.continent order by name limit 1) 
  1. Find the continents where all countries have a population <= 25000000. Then find the names of the countries associated with these continents. Show name, continent and population.
select name,continent,population 
from world x 
where 25000000 >=all(select y.population 
from world y where x.continent=y.continent and population>0)
  1. Some countries have populations more than three times that of any of their neighbours (in the same continent). Give the countries and continents.
select name,continent from world x
where x.population >=all(select y.population*3 from world y 
where y.continent=x.continent and population >0 and x.name!=y.name )

五、SUM and COUNT

World Country Profile: Aggregate functions

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.


  1. Show the total population of the world.
select sum(population) from world
  1. List all the continents - just once each.
select distinct continent from world  
  1. Give the total GDP of Africa
select sum(gdp) from world where continent='africa'
  1. How many countries have an area of at least 1000000
select  count(name) from world  where area >= 1000000
  1. What is the total population of ('Estonia', 'Latvia', 'Lithuania')
select sum(population) from world where name in('Estonia', 'Latvia', 'Lithuania')
  1. For each continent show the continent and number of countries.
select continent,count(name) from world  group by continent
  1. 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
  1. List the continents that have a total population of at least 100 million.
select continent 
from world  
group by continent
having sum(population)>100000000

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,444评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,421评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,036评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,363评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,460评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,502评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,511评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,280评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,736评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,014评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,190评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,848评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,531评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,159评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,411评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,067评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,078评论 2 352

推荐阅读更多精彩内容