您當前的位置:首頁 > 繪畫

SQL Zoo習題翻譯和答案(1-2)

作者:由 日求三餐 發表于 繪畫時間:2019-01-07

學習SQL時發現,還是需要多練習,看《SQL基礎教程》(作者:MICK)基本都看的明白,到實際使用時就差一點了,在練習SQL Zoo習題的時候發現,最大的問題是翻譯很多不準確不好理解,對著答案都弄不清楚,所以花了很多冤枉的時間,為了方便學習,我把SQL Zoo習題翻譯和答案寫出來,同時加了一點解析,大家相互學習吧~

一.SELECT basics

①Show the population of Germany(從world表中查詢出德國人口)

SELECT population FROM world WHERE name = ‘Germany’;

基礎的SELECT語句,包含了SELECT子句,FROM子句,WHERE子句。SELECT語句透過WHERE子句來指定查詢資料的條件。參考《SQL基礎教程》P45-P53。

②Show the name and the population for 'Sweden', 'Norway' and 'Denmark'.(從world表中查詢出“瑞典”,“挪威”和“丹麥” 的名稱和人口)

SELECT name, population FROM world WHERE name IN (‘Sweden’, ‘Norway’, ‘Denmark’);

TIPS:IN 謂詞。IN 允許我們在 WHERE 子句中規定多個值。參考《SQL基礎教程》P204-P210。

③Show the country and the area for countries with an area between 200,000 and 250,000.(從world表中查詢出面積在200,000到250,000之間的國家名稱和麵積)

SELECT name, area FROM world WHERE area BETWEEN 200000 AND 250000;

TIPS:BETWEEN 謂詞。使用BETWEEN可以進行範圍查詢。參考《SQL基礎教程》P202-P203。

測試題答案:

1。C 2。表E 3。 E 4。 C 5。 C 6。C 7。 C

二.SELECT from WORLD Tutorial

show the name, continent and population of all countries.(從world表中查詢出所有國家的名稱,大陸和人口。)

SELECT name, continent, population FROM world;

Show the name for the countries that have a population of at least 200 million. 200 million is 200000000, there are eight zeros.(從world表中查詢出人口至少為2億的國家的名稱。2億是2億,有8個零。)

SELECT name FROM world WHERE population >= 200000000

TIPS::>=,比較運算子。人口至少為2億=人口大於等於2億。參考《SQL基礎教程》P60-P62,比較運算子。

SQL Zoo習題翻譯和答案(1-2)

Give the name and the per capita GDP for those countries with a population of at least 200 million.(從world表中查詢出國家的人口至少200萬人口的所有國家的名稱和人均國內生產總值(人均GDP))

SELECT name,gdp/population FROM world WHERE population >= 200000000;

TIPS:算術運算子,人均GDP=GDP/人口。

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.查詢出以百萬為單位的國家名稱和人口,並且大陸是“南美”。將人口除以100萬,以獲得數百萬人口。

SELECT name,population/1000000 AS population FROM world WHERE continent = ‘South America’;

TIPS:算術運算子,人口除以100萬,以獲得數百萬人口。AS 為結果集列取別名。可延伸AS的基本用法。

Show the name and population for France, Germany, Italy

從world表中查詢出“法國,德國,義大利”的名稱和人口)

SELECT name,population FROM world WHERE name in(‘France’,‘Germany’,‘Italy’);

Show the countries which have a name that includes the word 'United'(查詢出國家名稱包含“United”字樣的國家)

SELECT name

FROM world

WHERE name like ‘%United%’;

TIPS:

LIKE 運算子

用於在 WHERE 子句中搜索列中的指定模式。

百分號(%)萬用字元。

參考《SQL基礎教程》P198-P202。

⑦Show the countries that are big by area or big by population. Show name, population and area.(查詢出按地區或按人口大的國家。顯示名稱,人口和麵積。)前情提要:兩種大的方法,一個國家的面積超過300萬平方公里或者人口超過2.5億。

SELECT name,population,area FROM world WHERE population>250000000 or area>3000000

TIPS:OR運算子在其兩側的查詢條件有一個成立時整個查詢條件都成立,其意思相當於“或者”,參考《SQL基礎教程》P70-P72。

Show the countries that are big by area or big by population but not both. Show name, population and area.(查詢出按面積或按人口大的國家,但不能同時滿足兩個條件。顯示名稱、人口和麵積。)

SELECT

name

population

area

FROM

world

WHERE

area

>

3000000

AND

population

<

250000000

OR

area

<

3000000

AND

population

>

250000000

);

TIPS:

不能同時滿足兩個條件。前情提要:兩種大的方法,一個國家的面積超過300萬平方公里或者人口超過2.5億

⑨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.(查詢出以百萬為單位顯示“南美洲”大陸國家的名稱和人口以及以十億為單位的國內生產總值)

使用ROUND函式將值顯示為兩位小數。

SELECT name,ROUND(population/1000000,2) AS population,ROUND(gdp/1000000000,2) AS gdp

FROM world

WHERE continent=‘South America’;

TIPS:ROUND函式用來進行四捨五入操作。 參考《SQL基礎教程》P181-182。

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

查詢出國內生產總值至少為1萬億的國家的名稱和人均GDP。(10000000000;即12個零)將此值四捨五入到最接近的1000。顯示萬億美元國家的人均GDP,精確到1000美元。

SELECT name, ROUND(gdp/population, -3) AS gdp FROM world WHERE gdp > 1000000000000;

注意:ROUND的用法:(借鑑優秀社群作業:love琳)

ROUND(7253。86, 0) -> 7254

ROUND(7253。86, 1) -> 7253。9

ROUND(7253。86,-3) -> 7000

Show the name and capital where the name and the capital have the same number of characters.(查詢出名稱和首都,並且國家名稱和首都字元數相同)

SELECT name,capital FROM world WHERE LENGTH(name)=LENGTH(capital);

TIPS:LENGTH函式:統計字串長度的函式。

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 FROM world WHERE LEFT(name,1)=LEFT(capital,1) AND name <> capital;

TIPS:LEFT函式,意思是擷取字串left() 從左開始 left(‘張某某12’,2)就是說將“張某某123”這個字串擷取從第2個開始將後面的截掉,結果就是“張某”。“<>”不等於。

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

查詢出國家名稱中包含所有的母音並且名稱中沒有空格的國家名稱

)【母音是這些 ( a e i o u )】

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 ‘% %’;

TIPS:AND運算子在其兩側的查詢條件都成立時整個查詢條件條件才成立,其意思相當於“並且”。

這題的我的主要注意的是名稱中沒有空格,(name NOT LIKE ‘% %’)需要兩個百分號(%)萬用字元中間空格一下。

測試題答案:

1。E 2。D 3。 B 4。 D 5。 B 6。D 7。 C

SQL練習地址:

SQLZOO

其他學習參考地址:

SQL AND & OR 運算子開始學習SQL_w3cschool

標簽: name  population  select  World  show