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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市怀吻,隨后出現(xiàn)的幾起案子瞬浓,更是在濱河造成了極大的恐慌,老刑警劉巖蓬坡,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件猿棉,死亡現(xiàn)場離奇詭異,居然都是意外死亡屑咳,警方通過查閱死者的電腦和手機萨赁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來兆龙,“玉大人杖爽,你說我怎么就攤上這事∽匣剩” “怎么了慰安?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長聪铺。 經(jīng)常有香客問我化焕,道長,這世上最難降的妖魔是什么铃剔? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任撒桨,我火速辦了婚禮搬俊,結(jié)果婚禮上渔扎,老公的妹妹穿的比我還像新娘末购。我一直安慰自己稀蟋,他們只是感情好藏鹊,可當我...
    茶點故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布昼激。 她就那樣靜靜地躺著汁汗,像睡著了一般咪辱。 火紅的嫁衣襯著肌膚如雪棋电。 梳的紋絲不亂的頭發(fā)上茎截,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天,我揣著相機與錄音赶盔,去河邊找鬼企锌。 笑死,一個胖子當著我的面吹牛于未,可吹牛的內(nèi)容都是我干的撕攒。 我是一名探鬼主播,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼烘浦,長吁一口氣:“原來是場噩夢啊……” “哼抖坪!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起闷叉,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤擦俐,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后握侧,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蚯瞧,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年品擎,在試婚紗的時候發(fā)現(xiàn)自己被綠了埋合。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡萄传,死狀恐怖甚颂,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情秀菱,我是刑警寧澤振诬,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站答朋,受9級特大地震影響贷揽,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜梦碗,卻給世界環(huán)境...
    茶點故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一禽绪、第九天 我趴在偏房一處隱蔽的房頂上張望蓖救。 院中可真熱鬧,春花似錦印屁、人聲如沸循捺。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽从橘。三九已至,卻和暖如春础钠,著一層夾襖步出監(jiān)牢的瞬間恰力,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工旗吁, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留踩萎,地道東北人。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓很钓,卻偏偏與公主長得像香府,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子码倦,可洞房花燭夜當晚...
    茶點故事閱讀 44,577評論 2 353

推薦閱讀更多精彩內(nèi)容