1.觀察運行一個簡單的SQL命令的結果。 SELECTname, continent, population FROM world
1 2 3
2.顯示具有至少2億人口的國家名稱。 2億是200000000,有八個零。 SELECT name FROM world WHERE population>200000000
1 2 3 4
3.找出有至少200百萬(2億)人口的國家名稱,及人均國內生產總值。 select name,gdp/population from world where population>200000000
1 2 3 4
4.顯示'South America'南美洲大陸的國家名字和以百萬為單位人口數。 將人口population 除以一百萬(1000000)得可得到以百萬為單位人口數。 selectname,population/1000000 from world where continent = 'South America'
1 2 3 4
5.顯示法國,德國,意大利(France, Germany, Italy)的國家名稱和人口。 selectname,population from world wherenamein('France','Germany','Italy')
1 2 3
6.顯示包含單詞“United”為名稱的國家。 selectnamefrom world wherenameLIKE'%United%'
1 2 3 4 5
7.成為大國的兩種方式:如果它有3百萬平方公里以上的面積,或擁有250百萬(2.5億)以上人口。 展示大國的名稱,人口和面積。 select name,population,area from world where area > 3000000 or population > 250000000
1 2 3 4 5
8.美國、印度和中國(USA, India, China)是人口又大,同時面積又大的國家。排除這些國家。 顯示以人口或面積為大國的國家,但不能同時兩者。顯示國家名稱,人口和面積。 select name,population,area from world where (area > 3000000and population <= 250000000) or (area <= 3000000and population > 250000000)
1 2 3 4 5
9.除以為1000000(6個零)是以百萬計。除以1000000000(9個零)是以十億計。使用 ROUND 函數來顯示的數值到小數點後兩位。 對於南美顯示以百萬計人口,以十億計2位小數GDP。 select name,round(population/1000000,2),round(gdp/1000000000,2) from world where continent = 'South America'
1 2 3 4 5
10.顯示國家有至少一個萬億元國內生產總值(萬億,也就是12個零)的人均國內生產總值。四捨五入這個值到最接近1000。 顯示萬億元國家的人均國內生產總值,四捨五入到最近的$1000。 select name,round(gdp/population,-3) from world where gdp >= 1000000000000
More difficult
1 2 3 4 5 6 7 8
11.The CASE statement shown is used to substitute North America for Caribbean in the third column. Show the name - but substitute Australasia for Oceania - for countries beginningwith N.(我明明选了zh捏...)
SELECTname, CASEWHEN continent='Oceania'THEN'Australasia' ELSE continent END FROM world WHEREnameLIKE'N%'
1 2 3 4 5 6 7 8 9 10 11
12.Show the nameand the continent - but substitute Eurasia for Europe and Asia; substitute America - foreach country in North America or South America or Caribbean. Show countries beginning with A or B
selectname, casewhen continent in ('Europe','Asia') then'Eurasia' when continent in ('North America','South America','Caribbean') then'America' else continent end from world wherenameLIKE'A%'ornameLIKE'B%'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
13.Put the continents right... Oceania becomes Australasia Countries in Eurasia and Turkey go to Europe/Asia Caribbean islands starting with'B'goto North America, other Caribbean islands goto South America Show the name, the original continent and the new continent ofall countries.(这个题的运行口貌似有点问题一直显示错误,在12题处运行就没有问题,但是不能有空格)
selectname,continent, casewhen continent = 'Oceania' then'Australasia' when continent in ('Eurasia','Turkey') then'Europe/Asia' when continent = 'Caribbean'andnamelike'B%' then'North America' when continent = 'Caribbean' then'South America' else continent end from world
SELECT from WORLD Tutorial
1 2 3 4 5 6 7
11.Greece has capital Athens. Eachof the strings 'Greece', and'Athens' has 6 characters. Show the nameand capital where the nameand the capital have the same number of characters.
SELECTname,capital FROM world WHERE length(name) = length(capital)
1 2 3 4 5 6
12.The capital of Sweden is Stockholm. Both wordsstart withthe letter 'S'. Show thenameandthe capital wherethefirst letters of each match. Don't include countries wherethenameandthe capital are the same word.
select name,capital from world where left(name,1)=left(capital,1) andname !=capital
1 2 3 4 5 6
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. Find the country that has all the vowels and no spaces in its name.
selectname from world wherenamenotlike'% %'andnamelike'%a%'andnamelike'%e%'andnamelike'%i%'andnamelike'%o%'andnamelike'%u%'
Quiz
1.選擇代碼找出以U開頭的國家名稱。 Answer:
1 2 3
SELECTname FROM world WHEREnameLIKE'U%'
2.選擇代碼以顯示英國United Kingdom的人口。 Answer:
1 2 3
SELECT population FROM world WHEREname = 'United Kingdom'
3.這個SQL代碼有什麼錯處。預計結果應該是含’France’的單列。
1 2 3
SELECT continent FROM world WHERE'name' = 'France'
Answer:
1
'name' 應該是 name
4.選擇你會從這個代碼獲得的結果。
1 2 3
SELECTname, population / 10 FROM world WHERE population < 10000
Answer:
5.選擇代碼以顯示在歐洲和亞洲的國家的名稱和人口。 Answer:
1 2 3
SELECTname, population FROM world WHERE continent IN ('Europe', 'Asia')
6.選擇代碼,結果只有兩行。 Answer:
1 2
SELECTnameFROM world WHEREnameIN ('Cuba', 'Togo')
7.選擇你會從這個代碼獲得的結果。
1 2 3
SELECTnameFROM world WHERE continent = 'South America' AND population > 40000000