2 SELECT from WORLD Tutorial

練習(xí)來(lái)自網(wǎng)頁(yè):http://sqlzoo.net/wiki/SELECT_from_WORLD_Tutorial

image.png

1.

Read the notes about this table. 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.

How to use WHERE to filter records. 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.

HELP:How to calculate per capita GDP
per capita GDP is the GDP divided by the population GDP/population

SELECT name, gdp/population as perGDP 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 as population1  FROM world
WHERE continent='South America';

5.

Show the name and population for France, Germany, Italy

SELECT name, population FROM world
WHERE name ='France' or name ='Germany' or name ='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.

Millions and billions
Divide by 1000000 (6 zeros) for millions. Divide by 1000000000 (9 zeros) for billions.

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/1000,0)*1000  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.

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 '% %'
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子及皂,更是在濱河造成了極大的恐慌宗收,老刑警劉巖舞萄,帶你破解...
    沈念sama閱讀 221,273評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件屋匕,死亡現(xiàn)場(chǎng)離奇詭異院刁,居然都是意外死亡渐白,警方通過(guò)查閱死者的電腦和手機(jī)尊浓,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)纯衍,“玉大人栋齿,你說(shuō)我怎么就攤上這事。” “怎么了褒颈?”我有些...
    開封第一講書人閱讀 167,709評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵柒巫,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我谷丸,道長(zhǎng)堡掏,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,520評(píng)論 1 296
  • 正文 為了忘掉前任刨疼,我火速辦了婚禮泉唁,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘揩慕。我一直安慰自己亭畜,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,515評(píng)論 6 397
  • 文/花漫 我一把揭開白布迎卤。 她就那樣靜靜地躺著拴鸵,像睡著了一般。 火紅的嫁衣襯著肌膚如雪蜗搔。 梳的紋絲不亂的頭發(fā)上劲藐,一...
    開封第一講書人閱讀 52,158評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音樟凄,去河邊找鬼聘芜。 笑死,一個(gè)胖子當(dāng)著我的面吹牛缝龄,可吹牛的內(nèi)容都是我干的汰现。 我是一名探鬼主播,決...
    沈念sama閱讀 40,755評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼叔壤,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼瞎饲!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起百新,我...
    開封第一講書人閱讀 39,660評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤企软,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后饭望,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體仗哨,經(jīng)...
    沈念sama閱讀 46,203評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,287評(píng)論 3 340
  • 正文 我和宋清朗相戀三年铅辞,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了厌漂。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,427評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡斟珊,死狀恐怖苇倡,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤旨椒,帶...
    沈念sama閱讀 36,122評(píng)論 5 349
  • 正文 年R本政府宣布晓褪,位于F島的核電站,受9級(jí)特大地震影響综慎,放射性物質(zhì)發(fā)生泄漏涣仿。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,801評(píng)論 3 333
  • 文/蒙蒙 一示惊、第九天 我趴在偏房一處隱蔽的房頂上張望好港。 院中可真熱鬧,春花似錦米罚、人聲如沸钧汹。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)拔莱。三九已至,卻和暖如春糊肠,著一層夾襖步出監(jiān)牢的瞬間辨宠,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工货裹, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人精偿。 一個(gè)月前我還...
    沈念sama閱讀 48,808評(píng)論 3 376
  • 正文 我出身青樓弧圆,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親笔咽。 傳聞我的和親對(duì)象是個(gè)殘疾皇子搔预,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,440評(píng)論 2 359

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

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,345評(píng)論 0 10
  • 世間男女拯田,若男子傾心于一個(gè)女子,表現(xiàn)形式可有多種甩十。送女子回家船庇,請(qǐng)客吃飯自不必說(shuō),拎包提攜重物侣监,贈(zèng)送禮物鸭轮,凡此種種,...
    蘇曼卿閱讀 4,153評(píng)論 9 1
  • 春風(fēng)依舊暖橄霉, 人面桃花紅窃爷。 怎奈思緒濃, 歸臥醉柳堤。
    晰翼閱讀 186評(píng)論 0 2