MySQL_SQLZOO练习答案(不含Quiz,持续更新)

前言

SQLZOO是一个在线SQL练习网站,在学习了SQL基础语法后可以到这个网站上进行练习实操。以下为本人在练习过程中记录下的答案,保证可以通过检验但是不保证是最好的解法,若各位有更好的解法,欢迎底下留言。

为了能够看清SQL语句,我会将过长的语句进行分段,过短的语句我将以一行的方式展示以节约篇幅。
网站的汉化是繁体中文而且相当辣鸡,有很多题目和答案的要求是完全不匹配的,所以英文基础好的同学还是尽量看英文来做。

SELECT basics/zh

链接

  1. 顯示德國 Germany 的人口
    SELECT population FROM world WHERE name = 'Germany';
    
  2. 詢面積為 5,000,000 以上平方公里的國家,對每個國家顯示她的名字和人均國內生產總值
    SELECT name, gdp/population FROM world WHERE area > 5000000;
    
  3. 顯示“Ireland 愛爾蘭”,“Iceland 冰島”,“Denmark 丹麥”的國家名稱和人口。
    SELECT name, population FROM world WHERE name IN ('Ireland ', 'Iceland ', 'Denmark ');
    
  4. 顯示面積為 200,000 及 250,000 之間的國家名稱和該國面積。
    SELECT name, area FROM world WHERE area BETWEEN 200000 AND 250000;
    

SELECT names/zh

链接

  1. 找出以 Y 為開首的國家。
    SELECT name FROM world WHERE name LIKE 'Y%';
    
  2. 找出以 Y 為結尾的國家。
    SELECT name FROM world WHERE name LIKE '%Y';
    
  3. 找出所有國家,其名字包括字母x。
    SELECT name FROM world WHERE name LIKE '%x%';
    
  4. 找出所有國家,其名字以 land 作結尾。
    SELECT name FROM world WHERE name LIKE '%land';
    
  5. 找出所有國家,其名字以 C 作開始,ia 作結尾。
    SELECT name FROM world WHERE name LIKE 'C%ia';
    
  6. 找出所有國家,其名字包括字母oo。
    SELECT name FROM world WHERE name LIKE '%oo%';
    
  7. 找出所有國家,其名字包括三個或以上的a。
    SELECT name FROM world WHERE name LIKE '%a%a%a%';
    
  8. 找出所有國家,其名字以t作第二個字母。
    SELECT name FROM world WHERE name LIKE '_t%' order by name;
    
  9. 找出所有國家,其名字都有兩個字母 o,被另外兩個字母相隔着。
    SELECT name FROM world WHERE name LIKE '%o__o%' order by name;
    
  10. 找出所有國家,其名字都是 4 個字母的。
    SELECT name FROM world WHERE name LIKE '____';
    
  11. 顯示所有國家名字,其首都和國家名字是相同的。
    select name from world where name = capital;
    
  12. 顯示所有國家名字,其首都是國家名字加上” City”。
    -- 注意City前面有个空格
    select name from world where capital = concat(name,' City');
    
  13. 找出所有首都和其國家名字,而首都要有國家名字中出現。
    select capital,name from world where capital like concat('%',name,'%');
    
  14. 找出所有首都和其國家名字,而首都是國家名字的延伸。
    -- 答案里是name在前,capital在后,题目出反了
    select name,capital from world where capital like concat('%',name,'%') and capital != name;
    
  15. 顯示國家名字,及其延伸詞,如首都是國家名字的延伸。
    -- 首先判断capital中是否包含name,然后判断替换后的字段是否为空
    SELECT name,REPLACE(capital, name, '') 
    FROM world 
    WHERE capital LIKE CONCAT('%',name,'%') AND REPLACE(capital, name, '') != '';
    

SELECT from WORLD Tutorial/zh

链接

  1. 顯示具有至少2億人口的國家名稱。
    SELECT name FROM world WHERE population>200000000;
    
  2. 找出有至少200百萬(2億)人口的國家名稱,及人均國內生產總值。
    select name,gdp/population from world where population>200000000;
    
  3. 顯示’South America’南美洲大陸的國家名字和以百萬為單位人口數。
    select name,population/1000000 from world where continent = 'South America';
    
  4. 顯示法國,德國,意大利(France, Germany, Italy)的國家名稱和人口。
    select name,population from world where name in ('France','Germany','Italy');
    
  5. 顯示包含單詞“United”為名稱的國家
    select name from world where name like '%United%';
    
  6. 展示大國的名稱,人口和面積。
    -- 成為大國的兩種方式:如果它有3百萬平方公里以上的面積,或擁有250百萬(2.5億)以上人口。
    select name,population,area from world where area>3000000 or population>250000000
    
  7. 顯示以人口或面積為大國的國家,但不能同時兩者。顯示國家名稱,人口和面積。
    -- 美國、印度和中國(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')
    
  8. 對於南美顯示以百萬計人口,以十億計2位小數GDP。
    select name,round(population/1000000,2),round(gdp/1000000000,2) 
    from world 
    where continent ='South America';
    
  9. 顯示萬億元國家的人均國內生產總值,四捨五入到最近的$ 1000。
    select name,round(gdp/population,-3) 
    from world where gdp>1000000000000;
    
  10. 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%';
    
  11. 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%';
    
  12. 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 
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值