在http://sqlzoo.net這個網(wǎng)站練習(xí)了sql語句,發(fā)現(xiàn)很多不會的語句,現(xiàn)記錄在下面,便于復(fù)習(xí):
1.修改此例子,查詢面積為 5,000,000 以上平方公里的國家,對每個國家顯示她的名字和人均國內(nèi)生產(chǎn)總值(gdp/population)扮叨。
SELECT name, population/area FROM world ?WHERE area > 5000000
2.找出以 Y 為開首的國家缤弦。
SELECT name FROM world WHERE name LIKE 'Y%'
3.找出以 Y 為結(jié)尾的國家。
SELECT name FROM world WHERE name LIKE '%Y'
4.找出所有國家,其名字包括三個或以上的a彻磁。
SELECT name FROM world WHERE name LIKE '%a%a%a%'
5.找出所有國家,其名字以t作第二個字母碍沐。
SELECT name FROM world WHERE name LIKE '_t%' ORDER BY name
6.找出所有國家,其名字都是 4 個字母的。
SELECT name FROM world WHERE name LIKE '____'
7.顯示所有國家名字,其首都和國家名字是相同的衷蜓。
SELECT name FROM world WHERE name = capital
8.顯示所有國家名字,其首都是國家名字加上”City”累提。
SELECT capital FROM world WHERE capital LIKE concat(name , ' City')
9.找出所有首都和其國家名字,而首都要有國家名字中出現(xiàn)。
SELECT capital,name FROM world WHERE capital LIKE concat('%',name,"%")
10.找出所有首都和其國家名字,而首都是國家名字的延伸磁浇。你應(yīng)顯示 Mexico City,因它比其國家名字 Mexico 長斋陪。你不應(yīng)顯示 Luxembourg,因它的首都和國家名相是相同的。
SELECT name,capital FROM world WHERE capital LIKE concat(name,'_%') AND capital != name
11.顯示國家名字,及其延伸詞无虚,如首都是國家名字的延伸缔赠。
SELECT name, REPLACE(capital,name,'') AS ext FROM world WHERE capital LIKE concat(name,'_%');
總結(jié):?
? ? ? ? ? CONCAT('FIRST ', 'SECOND') : 將兩個字符串合并
? ? ? ? ? REPLACE?( 'string_expression1' , 'string_expression2' , 'string_expression3' ) : 當(dāng)字符串1中出現(xiàn)字符串2,將其替換為字符串3
12.對於南美顯示以百萬計(jì)人口,以十億計(jì)2位小數(shù)GDP友题。
SELECT name,ROUND(population /1000000,2),ROUND(gdp/1000000000,2) FROM world WHERE continent = 'South America'
13.Show the name - but substitute?Australasia?for?Oceania?- for countries beginning with N.
SELECT name,
CASE WHEN continent='Oceania' THEN 'Australasia' ELSE continent END
FROM world WHERE name LIKE 'N%';
14.Show the name and the continent - but substitute?Eurasia?for Europe and Asia; substitute?America?- for each country in?North America?or?South America?or?Caribbean. Show countries beginning with A or B
SELECT name,CASE WHEN continent in('Europe','Asia') THEN 'Eurasia'WHEN continent in( 'North America','South America','Caribbean') THEN 'America' ELSE continent ENDFROM world WHERE name LIKE 'A%' OR name LIKE 'B%'
15.Put the continents right...Oceania becomes AustralasiaCountries in Eurasia and Turkey go to?Europe/AsiaCaribbean islands starting with 'B' go to?North America, other Caribbean islands go to?South AmericaShow the name, the original continent and the new continent of all countries.
SELECT name,continent,
CASE
WHEN continent = 'Oceania' THEN 'Australasia'
WHEN continent IN ('Eurasia','Turkey') THEN 'Europe/Asia'
WHEN continent = 'Caribbean' THEN
CASE
WHEN name LIKE 'B%' THEN 'North America'ELSE 'South America' END
ELSE continent END
FROM world
ORDER BY name ASC
總結(jié):
1.四舍五入函數(shù)ROUND:?ROUND 函數(shù)用于把數(shù)值字段舍入為指定的小數(shù)位數(shù)嗤堰。
2.CASE WHEN 條件函數(shù)
顯示1980年物理學(xué)(physics)獲獎?wù)撸?984年化學(xué)獎(chemistry)獲得者
SELECT * FROM NOBEL WHERE (yr=1980 AND subject='physics') OR (yr=1984 AND subject='chemistry')
查找尤金?奧尼爾EUGENE O'NEILL得獎的所有細(xì)節(jié)
SELECT * FROM nobel WHERE winner = 'EUGENE O\'NEILL'
列出爵士的獲獎?wù)叨然隆⒛攴萏呦弧ⅹ勴?爵士的名字以Sir開始)。先顯示最新獲獎?wù)吒瓿会嵬暝侔疵Q順序排列离唬。
SELECT winner, yr,subject FROM nobel WHERE winner LIKE 'SIR%' ORDER BY yr DESC,winner ASC
Show the 1984 winners and subject ordered by subject and winner name; but list Chemistry and Physics last.
SELECT winner, subject FROM nobel WHERE yr=1984 ORDER BY subject IN ('Physics','Chemistry'),subject ,winner
總結(jié) : DISTINCT函數(shù) 去重函數(shù)
顯示歐洲的國家名稱name和每個國家的人口population。以德國的人口的百分比作人口顯示呛凶。
SELECT name,
CONCAT(ROUND(population*100/(SELECT population FROM world WHERE name = 'Germany') ,0),'%')
FROM world WHERE continent = 'Europe'
哪些國家的GDP比Europe歐洲的全部國家都要高呢? [只需列出?name?男娄。] (有些國家的記錄中,GDP是NULL漾稀,沒有填入資料的模闲。)
SELECT name FROM world
WHERE gdp> ALL(SELECT gdp FROM world WHERE continent = 'Europe' AND gdp>0)
總結(jié): ALL用于子查詢?父查詢中的結(jié)果集大于子查詢中每一個結(jié)果集中的值?
在每一個州中找出最大面積的國家,列出洲份?continent, 國家名字?name及面積?area崭捍。 (有些國家的記錄中尸折,AREA是NULL,沒有填入資料的殷蛇。)
SELECT continent, name, area FROM world x
WHERE area >= ALL (SELECT area FROM world y WHERE y.continent=x.continent AND area >0)
列出洲份名稱实夹,和每個洲份中國家名字按子母順序是排首位的國家名。(即每洲只有列一國)
SELECT continent,name FROM world x WHERE x.name = (SELECT y.name FROM world y WHERE x.continent = y.continent ORDER BY name LIMIT 1)
總結(jié):? LIMIT n(補(bǔ)全為 LIMIT 0粒梦,n),則表示顯示前n條記錄
有些國家的人口是同洲份的所有其他國的3倍或以上亮航。列出 國家名字name 和 洲份 continent。
SELECT name,continent FROM world x WHERE x.population / 3 >= ALL(SELECT population FROM world y WHERE y.continent = x.continent AND population >0 AND y.name != x.name );
列出有至少100百萬(1億)(100,000,000)人口的洲份匀们。
SELECT continent FROM world
GROUP BY continent
HAVING SUM(population)>=100000000
總結(jié) : HAVING?HAVING是對組進(jìn)行過濾缴淋,where是每條記錄進(jìn)行過濾