SQL練習(xí)_1 | 0,1,2 | SQLZOO_20191002

本系列刷題筆記主要用以記錄刷SQLZOO的過程中的思路嫌变、個人答案以及陌生的或者新的知識點东涡。

題目來源 - SQLZOO
SQLZOO中題目中文版本與英文版本略有差異凸舵,題目以英文版為準(zhǔn)

目錄
0 SELECT basics
1 SELECT name
2 SELECT from World

0 SELECT basics(SELECT 基礎(chǔ))

查詢表格

查詢表格_world

0_1 Show the population of Germany

SELECT population
FROM world
WHERE name = 'Germany'

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

SELECT name,
       population
FROM world
WHERE name IN ('Sweden','Norway','Denmark')

0_3 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

1 SELECT name

查詢表格(同Select Basics - World)

1_1 Find the country that start with Y

SELECT name
FROM world
WHERE name LIKE 'Y%'

1_2 Find the countries that end with y

SELECT name
FROM world
WHERE name LIKE '%y'

1_3 Find the countries that contain the letter x

SELECT name
FROM world
WHERE name LIKE '%x%'

1_4 Find the countries that end with land

SELECT name
FROM world
WHERE name LIKE '%land'

1_5 Find the countries that start with C and end with ia

SELECT name
FROM world
WHERE name LIKE 'C%ia'

1_6 Find the country that has oo in the name

SELECT name
FROM world
WHERE name LIKE '%oo%'

1_7 Find the countries that have three or more a in the name

SELECT name
FROM world
WHERE name LIKE '%a%a%a%''

1_8 Find the countries that have "t" as the second character.

SELECT name
FROM world
WHERE name LIKE '_t%''
ORDER BY name

1_9 Find the countries that have two "o" characters separated by two others.

SELECT name
FROM world
WHERE name LIKE '%o__o%''

1_10 Find the countries that have exactly four characters.

SELECT name
FROM world
WHERE name LIKE '%____%''

1_11 Find the country where the name is the capital city.

SELECT name
FROM world
WHERE name = capital

1_12 Find the country where the capital is the country plus "City".

SELECT name
FROM world
WHERE capital = concat(name,' City')

1_13 Find the capital and the name where the capital includes the name of the country.

SELECT capital,
       name
FROM world
WHERE capital LIKE concat(name,'%')
OR    capital LIKE concat('%',name)

1_14 Find the capital and the name where the capital is an extension of name of the country.

SELECT capital,
       name
FROM world
WHERE capital LIKE concat(name,'_%')

1_15 Show the name and the extension where the capital is an extension of name of the country.

SELECT name,
       replace(capital,name,'')
FROM world
WHERE capital LIKE concat(name,'_%')

2 SELECT from World

查詢表格(同Select Basics - World)

2_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_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

2_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

2_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 population
FROM world
WHERE continent = 'South America'

2_5 Show the name and population for France, Germany, Italy

SELECT name,
       population
FROM world
WHERE name IN ('France','Germany','Italy')

2_6 Show the countries which have a name that includes the word 'United'

SELECT name
FROM world
WHERE name LIKE '%United%'

2_7 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

2_8 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
XOR    population > 250000000

2_9 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'

2_10 Show per-capita GDP for the trillion dollar countries to the nearest $1000.

SELECT name,
       ROUND((gdp /(population*1000)))*1000
FROM world
WHERE gdp >= 1000000000000

2_11 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)

2_12 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

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

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)容合作請聯(lián)系作者
  • 序言:七十年代末哗戈,一起剝皮案震驚了整個濱河市郊艘,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌谱醇,老刑警劉巖暇仲,帶你破解...
    沈念sama閱讀 206,968評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異副渴,居然都是意外死亡,警方通過查閱死者的電腦和手機全度,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,601評論 2 382
  • 文/潘曉璐 我一進店門煮剧,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人将鸵,你說我怎么就攤上這事勉盅。” “怎么了顶掉?”我有些...
    開封第一講書人閱讀 153,220評論 0 344
  • 文/不壞的土叔 我叫張陵草娜,是天一觀的道長。 經(jīng)常有香客問我痒筒,道長宰闰,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,416評論 1 279
  • 正文 為了忘掉前任簿透,我火速辦了婚禮移袍,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘老充。我一直安慰自己葡盗,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 64,425評論 5 374
  • 文/花漫 我一把揭開白布啡浊。 她就那樣靜靜地躺著觅够,像睡著了一般。 火紅的嫁衣襯著肌膚如雪巷嚣。 梳的紋絲不亂的頭發(fā)上喘先,一...
    開封第一講書人閱讀 49,144評論 1 285
  • 那天,我揣著相機與錄音涂籽,去河邊找鬼苹祟。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的树枫。 我是一名探鬼主播直焙,決...
    沈念sama閱讀 38,432評論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼砂轻!你這毒婦竟也來了奔誓?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,088評論 0 261
  • 序言:老撾萬榮一對情侶失蹤搔涝,失蹤者是張志新(化名)和其女友劉穎厨喂,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體庄呈,經(jīng)...
    沈念sama閱讀 43,586評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡蜕煌,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,028評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了诬留。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片斜纪。...
    茶點故事閱讀 38,137評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖文兑,靈堂內(nèi)的尸體忽然破棺而出盒刚,到底是詐尸還是另有隱情,我是刑警寧澤绿贞,帶...
    沈念sama閱讀 33,783評論 4 324
  • 正文 年R本政府宣布因块,位于F島的核電站,受9級特大地震影響籍铁,放射性物質(zhì)發(fā)生泄漏涡上。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,343評論 3 307
  • 文/蒙蒙 一寨辩、第九天 我趴在偏房一處隱蔽的房頂上張望吓懈。 院中可真熱鬧,春花似錦靡狞、人聲如沸耻警。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,333評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽甘穿。三九已至,卻和暖如春梢杭,著一層夾襖步出監(jiān)牢的瞬間温兼,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,559評論 1 262
  • 我被黑心中介騙來泰國打工武契, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留募判,地道東北人荡含。 一個月前我還...
    沈念sama閱讀 45,595評論 2 355
  • 正文 我出身青樓,卻偏偏與公主長得像届垫,于是被迫代替她去往敵國和親释液。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 42,901評論 2 345

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