首先查看world表的字段:
name | continent | area | population | gdp | capital | tld | flag |
---|
SELECT * FROM world;
2、顯示人口至少為2億的國家/地區的名稱。2億=200million
SELECT name FROM world WHERE population >= 200000000;
3、給出人口至少2億的國家的名稱和人均國內生產總值。
select name,(gdp/population) as per_capita_gdp from world where population>=200000000;
4、顯示continent
='South America'的國家的名稱和人口。 將人口除以100萬,以獲得數百萬人口,也就是population的單位為百萬。
select name,(population/1000000) as population from world where continent='South America' ;
5、顯示法國,德國,意大利的名稱和人口(France、Germany、Italy)
select name,population from world where name in ('France','Germany','Italy'); #注意 #France、Germany等是以字符串形式出現的,加引號,否則會出錯
6、顯示名稱中包含“United”字樣的國家/地區
select name from world where name like '%united%';
7、如果一個國家面積超過300萬平方公里,或者人口超過2.5億,那么這個國家就很大。
按人口顯示面積大或面積大的國家。 顯示名稱,人口和面積。
select name,population,area from world where population>250000000 or area>3000000;
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 (population<=250000000 and area>3000000) or (population>250000000 and area<3000000);
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.
select name,round(population/1000000,2) as population,round(gdp/1000000000,2) as gdp from world where continent='South America';
10、顯示GDP至少為1萬億(100億,即12個零)的國家的名稱和人均GDP。 將此值舍入到最接近的1000。
將萬億美元國家的人均GDP顯示為最接近的1000美元。
select name,round(gdp/population,-3) as per_capita_gdp 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.
select name,capital from world where length(name)=length(capital);
12、The capital of Sweden is Stockholm. Both words start with the letter 'S'.
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.
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 '% %';
雖然運行正確,但是感覺並不對,后面會找更好的答案解決此問題
總結:
1、round函數的使用
鏈接:http://www.w3school.com.cn/sql/sql_func_round.asp
2、like操作符
3、XOR操作符(第8題)
4、length函數
用法:
length(str):一個漢字是算三個字符,一個數字或字母算一個字符
char_length: 不管漢字還是數字或者是字母都算是一個字符
5、left
用法:left(str, length),即:left(被截取字符串, 截取長度)
#用法: left(str, length) #即:left(被截取字符串, 截取長度)
#從右側截取: right(str, length) #即:right(被截取字符串, 截取長度)
#截取特定長度字符串 substring(str, pos, length) #substring(被截取字符串,從第幾位開始截取,截取長度)