SQLZOO练习题答案参考(全)

自学SQL将近一年,比较有名的SQL题库都尝试刷过,目前SQLZOO、XUESQL、牛客、Hackerrank初阶中阶、炼码基本刷完,Hackerrank高阶、Leetcode、SQL_intern仍然在刷,题目越来越难,不过水平也在逐渐提高啦!

一边刷高阶题,一边还会回头复盘基础,尝试多种解法,因此会不定时分享刷的题库的参考解法。注:SQL没有严格的格式要求,是我个人比较习惯缩进和大小写。

首先从SQLZOO开始。

SQLZOO有增删改查的讲解,也有练习题,涉及内容很基础,是个很考虑新手的题库了。

适用人群:刚入门小白、巩固基础准备进阶

优点:1.既可以学习,也可以刷题,相对全面

           2.练习题中也包含讲解,情境化设置,循序渐进,难度递增,适合新手培养逻辑

           3.某些部分会出现难题,对一个内容可以锻炼地比较深入

缺点:1.练习题的情境化设置导致有些题目要求查询的字段表述不清,会因为缺少答案里的字段而报错

           2.部分题目有问题,莫名其妙报错

           3.练习题涉及内容不全面,增删改没有对应的练习题

解法参考如下:

SELECT basics部分

-- Modify it to show the population of Germany
SELECT population
FROM world
WHERE name = 'Germany';

-- Show the name and the population for 'Sweden', 'Norway' and 'Denmark'
SELECT 
	name,
 	population
FROM world
WHERE name in ('Sweden','Norway','Denmark');

-- Show the country and the area for countries with an area between 200,000 and 250,000
SELECT 
	name,
	area
FROM world
WHERE area between 200000 and 250000;

SELECT from world部分

-- Show the name, continent and population of all countries
SELECT
	name,
	continent,
	population
FROM world;

-- Show the name for the countries that have a population of at least 200 million
SELECT name
FROM world
WHERE population >= 200000000;

-- Give the name and the per capita GDP for those countries with a population of at least 200 million
SELECT
	name,
	gdp/population
FROM world
WHERE population >= 200000000;

-- Show the name and population in millions for the countries of the continent 'South America' 
-- Divide the population by 1000000 to get population in millions
SELECT
	name,
	population/1000000
FROM world
WHERE continent = 'South America';

-- Show the name and population for France, Germany, Italy
SELECT 
	name,
    population
FROM world
WHERE name in ('France','Germany','Italy');

-- Show the countries which have a name that includes the word 'United'
SELECT name
FROM world
WHERE name like '%United%';

-- Show the countries that are big by area or big by population. Show name, population and area
-- A country is big if it has an area of more than 3 million sq km or it has a population of more than 250 million
SELECT
	name,
	population,
	area
FROM world
WHERE area >= 3000000 or population >= 250000000;

-- Show the countries that are big by area (more than 3 million) or big by population (more than 250 million) but not both
-- Show name, population and area
SELECT
	name,
	population,
	area
FROM world
WHERE (area >= 3000000 and population < 250000000) or
      (area < 3000000 and population >= 250000000);

-- For South America show population in millions and GDP in billions both to 2 decimal places
SELECT
	name,
	round(population/1000000,2),
	round(gdp/1000000000,2)
FROM world
WHERE continent = 'South America';

-- Show per-capita GDP for the trillion dollar countries to the nearest $1000
SELECT
	name,
	round(gdp/population,-3)
FROM world
WHERE gdp >= 1000000000000;

-- Show the name and capital where the name and the capital have the same number of characters
SELECT 
	name,
	capital
FROM world
WHERE len(name)=len(capital);

-- Show the name and the capital where the first letters of each match
-- Don't include countries where the name and the capital are the same word
SELECT
	name,
	capital
FROM world
WHERE left(name,1) = left(capital,1) AND name != capital;

-- 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 '% %';

SELECT from nobel部分

-- Change the query shown so that it displays Nobel prizes for 1950
SELECT *
FROM nobel
WHERE yr = '1950';

-- Show who won the 1962 prize for literature
SELECT winner
FROM nobel
WHERE yr = '1962' 
  and subject = 'Literature';

-- Show the
  • 4
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值