SELECT within SELECT Tutorial

image.png

1.List each country name where the population is larger than that of 'Russia'.
world(name, continent, area, population, gdp)

SELECT name FROM world
  WHERE population >
     (SELECT population FROM world
      WHERE name='Romania')

2.Show the names of countries in Europe with a per capita GDP greater than 'United Kingdom'.
Per Capita GDP

select name 
from world 
where continent ='europe'and gdp/population > (select gdp/population from world
where name = 'United Kingdom') ;

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 ('South America','Oceania')
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') ;

5.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.
Decimal places
Percent symbol %

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 gdp>0
and continent='Europe');

7.Find the largest country (by area) in each continent, show the continent, the name and the area:

 SELECT continent, name, area FROM world AS x
WHERE area >= ALL(SELECT area 
                   FROM world AS y
                   WHERE y.continent=x.continent
                   AND area>0)

8.List each continent and the name of the country that comes first alphabetically.

select continent,name
from world as x
where x.name = (select y.name from world as y  
                where x.continent=y.continent
                order by name
                limit 1)

9.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 as x
where 25000000>all(select population from world as y
where x.continent=y.continent
and population>0)

10.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 AS x
WHERE population/3 > ALL(SELECT population
                         FROM world AS y
                         WHERE x.continent=y.continent
                         AND x.name <> y.name
                         AND population > 0)

補(bǔ)充
1.嵌套時(shí)先寫字句(問題中的那個(gè)條件),再寫主句喘垂。
2.題1:比較條件時(shí)撇寞,為同一個(gè)對(duì)象進(jìn)行比較,例如人口比較人口眨八,人均國內(nèi)值比較人均國內(nèi)值腺兴。
3.CONCAT允許您將兩個(gè)或多個(gè)字符串粘在一起。此操作是串聯(lián)的廉侧。
例如題5页响,contact(20,%),顯示的結(jié)果為20%。
使用cantact和round函數(shù)段誊,可以組成百分?jǐn)?shù) 占比的形式闰蚕。其中round(參數(shù)1,參數(shù)2)函數(shù)中连舍,也可以只使用1個(gè)參數(shù)没陡。
4.limit(第一個(gè)參數(shù),第二個(gè)參數(shù))第一個(gè)參數(shù)為輸出的起始位置,第二個(gè)參數(shù)為偏移變量盼玄,偏移多少輸出多少贴彼,例如limit(2,3)表示從位置2開始輸出,輸出到位置5的結(jié)果埃儿。
例如列字段:列名num:字段為12345器仗,SELECT * FROM 表 LIMIT 2, 3 輸出結(jié)果為3,4,5。

num
1 
2
3
4
5
select  num from 表
輸出結(jié)果為
num
3
4
5
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末童番,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子妓盲,更是在濱河造成了極大的恐慌杂拨,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,402評(píng)論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件悯衬,死亡現(xiàn)場(chǎng)離奇詭異弹沽,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)筋粗,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門策橘,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人娜亿,你說我怎么就攤上這事丽已。” “怎么了买决?”我有些...
    開封第一講書人閱讀 162,483評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵沛婴,是天一觀的道長。 經(jīng)常有香客問我督赤,道長嘁灯,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,165評(píng)論 1 292
  • 正文 為了忘掉前任躲舌,我火速辦了婚禮丑婿,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘没卸。我一直安慰自己羹奉,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,176評(píng)論 6 388
  • 文/花漫 我一把揭開白布约计。 她就那樣靜靜地躺著诀拭,像睡著了一般。 火紅的嫁衣襯著肌膚如雪病蛉。 梳的紋絲不亂的頭發(fā)上炫加,一...
    開封第一講書人閱讀 51,146評(píng)論 1 297
  • 那天瑰煎,我揣著相機(jī)與錄音,去河邊找鬼俗孝。 笑死酒甸,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的赋铝。 我是一名探鬼主播插勤,決...
    沈念sama閱讀 40,032評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼革骨!你這毒婦竟也來了农尖?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,896評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤良哲,失蹤者是張志新(化名)和其女友劉穎盛卡,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體筑凫,經(jīng)...
    沈念sama閱讀 45,311評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡滑沧,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,536評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了巍实。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片滓技。...
    茶點(diǎn)故事閱讀 39,696評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖棚潦,靈堂內(nèi)的尸體忽然破棺而出令漂,到底是詐尸還是另有隱情,我是刑警寧澤丸边,帶...
    沈念sama閱讀 35,413評(píng)論 5 343
  • 正文 年R本政府宣布叠必,位于F島的核電站,受9級(jí)特大地震影響原环,放射性物質(zhì)發(fā)生泄漏挠唆。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,008評(píng)論 3 325
  • 文/蒙蒙 一嘱吗、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧滔驾,春花似錦谒麦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至摊阀,卻和暖如春耻蛇,著一層夾襖步出監(jiān)牢的瞬間踪蹬,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評(píng)論 1 269
  • 我被黑心中介騙來泰國打工臣咖, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留跃捣,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,698評(píng)論 2 368
  • 正文 我出身青樓夺蛇,卻偏偏與公主長得像疚漆,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子刁赦,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,592評(píng)論 2 353

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