文章目录
前言
SQLZOO是一个在线SQL练习网站,在学习了SQL基础语法后可以到这个网站上进行练习实操。以下为本人在练习过程中记录下的答案,保证可以通过检验但是不保证是最好的解法,若各位有更好的解法,欢迎底下留言。
为了能够看清SQL语句,我会将过长的语句进行分段,过短的语句我将以一行的方式展示以节约篇幅。
网站的汉化是繁体中文而且相当辣鸡,有很多题目和答案的要求是完全不匹配的,所以英文基础好的同学还是尽量看英文来做。
SELECT basics/zh
- 顯示德國 Germany 的人口
SELECT population FROM world WHERE name = 'Germany';
- 詢面積為 5,000,000 以上平方公里的國家,對每個國家顯示她的名字和人均國內生產總值
SELECT name, gdp/population FROM world WHERE area > 5000000;
- 顯示“Ireland 愛爾蘭”,“Iceland 冰島”,“Denmark 丹麥”的國家名稱和人口。
SELECT name, population FROM world WHERE name IN ('Ireland ', 'Iceland ', 'Denmark ');
- 顯示面積為 200,000 及 250,000 之間的國家名稱和該國面積。
SELECT name, area FROM world WHERE area BETWEEN 200000 AND 250000;
SELECT names/zh
- 找出以 Y 為開首的國家。
SELECT name FROM world WHERE name LIKE 'Y%';
- 找出以 Y 為結尾的國家。
SELECT name FROM world WHERE name LIKE '%Y';
- 找出所有國家,其名字包括字母x。
SELECT name FROM world WHERE name LIKE '%x%';
- 找出所有國家,其名字以 land 作結尾。
SELECT name FROM world WHERE name LIKE '%land';
- 找出所有國家,其名字以 C 作開始,ia 作結尾。
SELECT name FROM world WHERE name LIKE 'C%ia';
- 找出所有國家,其名字包括字母oo。
SELECT name FROM world WHERE name LIKE '%oo%';
- 找出所有國家,其名字包括三個或以上的a。
SELECT name FROM world WHERE name LIKE '%a%a%a%';
- 找出所有國家,其名字以t作第二個字母。
SELECT name FROM world WHERE name LIKE '_t%' order by name;
- 找出所有國家,其名字都有兩個字母 o,被另外兩個字母相隔着。
SELECT name FROM world WHERE name LIKE '%o__o%' order by name;
- 找出所有國家,其名字都是 4 個字母的。
SELECT name FROM world WHERE name LIKE '____';
- 顯示所有國家名字,其首都和國家名字是相同的。
select name from world where name = capital;
- 顯示所有國家名字,其首都是國家名字加上” City”。
-- 注意City前面有个空格 select name from world where capital = concat(name,' City');
- 找出所有首都和其國家名字,而首都要有國家名字中出現。
select capital,name from world where capital like concat('%',name,'%');
- 找出所有首都和其國家名字,而首都是國家名字的延伸。
-- 答案里是name在前,capital在后,题目出反了 select name,capital from world where capital like concat('%',name,'%') and capital != name;
- 顯示國家名字,及其延伸詞,如首都是國家名字的延伸。
-- 首先判断capital中是否包含name,然后判断替换后的字段是否为空 SELECT name,REPLACE(capital, name, '') FROM world WHERE capital LIKE CONCAT('%',name,'%') AND REPLACE(capital, name, '') != '';
SELECT from WORLD Tutorial/zh
- 顯示具有至少2億人口的國家名稱。
SELECT name FROM world WHERE population>200000000;
- 找出有至少200百萬(2億)人口的國家名稱,及人均國內生產總值。
select name,gdp/population from world where population>200000000;
- 顯示’South America’南美洲大陸的國家名字和以百萬為單位人口數。
select name,population/1000000 from world where continent = 'South America';
- 顯示法國,德國,意大利(France, Germany, Italy)的國家名稱和人口。
select name,population from world where name in ('France','Germany','Italy');
- 顯示包含單詞“United”為名稱的國家
select name from world where name like '%United%';
- 展示大國的名稱,人口和面積。
-- 成為大國的兩種方式:如果它有3百萬平方公里以上的面積,或擁有250百萬(2.5億)以上人口。 select name,population,area from world where area>3000000 or population>250000000
- 顯示以人口或面積為大國的國家,但不能同時兩者。顯示國家名稱,人口和面積。
-- 美國、印度和中國(USA, India, China)是人口又大,同時面積又大的國家。排除這些國家。 select name,population,area from world where (area>3000000 and population<250000000) or (area<3000000 and population>250000000) and name not in ('USA', 'India', 'China')
- 對於南美顯示以百萬計人口,以十億計2位小數GDP。
select name,round(population/1000000,2),round(gdp/1000000000,2) from world where continent ='South America';
- 顯示萬億元國家的人均國內生產總值,四捨五入到最近的$ 1000。
select name,round(gdp/population,-3) from world where gdp>1000000000000;
- 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%';
- 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 end from world where name like 'A%' or name like 'B%';
- Show the name, the original continent and the new continent of all countries.
-- 这一题不加order by name就报错,我也不知道为啥 select name,continent, case when continent = 'Oceania' then 'Australasia' when continent = 'Eurasia' then