SQLZOO 答案—完整版(上)

简介: SQLZOO 答案—完整版(上)

sqlzoo练习题答,已更完。


SELECT basics


1、The example uses a WHERE clause to show the population of ‘France’. Note that strings (pieces of text that are data) should be in ‘single quotes’;

Modify it to show the population of Germany

SELECT population FROM world
WHERE name = 'Germany'


2、Checking a list The word IN allows us to check if an item is in a list. The example shows thename and population for the countries ‘Brazil’, ‘Russia’, ‘India’ and ‘China’.

Show the name and the population for ‘Sweden’, ‘Norway’ and ‘Denmark’.

SELECT name, population FROM world
WHERE name IN ('Sweden', 'Norway', 'Denmark');

3、Which countries are not too small and not too big? BETWEEN allows range checking (range specified is inclusive of boundary values). The example below shows countries with an area of 250,000-300,000 sq. km. Modify it to 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


1、Read the notes about this table. Observe the result of running this SQL command to show the name, continent and population of all countries.

SELECT name, continent, population FROM world;

2、How to use WHERE to filter records. Show the name for the countries that have a population of at least 200 million. 200 million is 200000000, there are eight zeros.

SELECT name FROM world
WHERE population >= 200000000;

3、Give the name and the per capita GDP for those countries with a population of at least 200 million

HELP:How to calculate per capita GDP

per capita GDP is the GDP divided by the population GDP/population

select name, gdp/population from world 
where population >= 200000000;

4、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';

5、Show the name and population for France, Germany, Italy

select name, population from world 
where name IN('France','Germany','Italy');


6、Show the countries which have a name that includes the word ‘United’

select name from world 
where name like '%United%';


7、Two ways to be big: 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.

Show the countries that are big by area or big by population. Show name, population and area.

select name, population, area from world 
where area >= 3000000 or population >= 250000000;

8、Exclusive OR (XOR). 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.

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 
(area >= 3000000 and population <= 250000000)
or
(area <= 3000000 and population >= 250000000);

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.

For South America show population in millions and GDP in billions both to 2 decimal places.

Millions and billions

Divide by 1000000 (6 zeros) for millions. Divide by 1000000000 (9 zeros) for billions.

select name, round(population/1000000,2), round(gdp/1000000000,2) 
from world 
where continent = 'South America';

10、Show the name and per-capita GDP for those countries with a GDP of at least one trillion (1000000000000; that is 12 zeros). Round this value to the nearest 1000.

Show per-capita GDP for the trillion dollar countries to the nearest $1000.

select name, round(gdp/population,-3) from world 
where gdp >= 1000000000000;

或

select name,round(GDP/population/1000,0)*1000 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.

You can use the LENGTH function to find the number of characters in a string

SELECT name,capital 
FROM world
WHERE LENGTH(name) = LENGTH(capital)

12、The capital of Sweden is Stockholm. Both words start with the letter ‘S’.

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.

You can use the function LEFT to isolate the first character.

You can use <> as the NOT EQUALS operator.

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.

You can use the phrase name NOT LIKE ‘%a%’ to exclude characters from your results.

The query shown misses countries like Bahamas and Belarus because they contain at least one ‘a’

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


  1. Change the query shown so that it displays Nobel prizes for 1950.
SELECT yr, subject, winner
FROM nobel
WHERE yr = 1950


  1. Show who won the 1962 prize for Literature.
SELECT winner
FROM nobel
WHERE yr = 1962
AND subject = 'Literature'


  1. Show the year and subject that won ‘Albert Einstein’ his prize.
SELECT yr, subject 
FROM nobel
WHERE winner = 'Albert Einstein'


  1. Give the name of the ‘Peace’ winners since the year 2000, including 2000.
SELECT winner 
FROM nobel
WHERE yr >= 2000
AND subject = 'Peace'


  1. Show all details (yr, subject, winner) of the Literature prize winners for 1980 to 1989 inclusive.
SELECT * 
FROM nobel
WHERE subject = 'Literature'
AND yr BETWEEN 1980 AND 1989


6、Show all details of the presidential winners:

Theodore Roosevelt

Woodrow Wilson

Jimmy Carter

Barack Obama

SELECT * 
FROM nobel
WHERE 
winner IN ('Theodore Roosevelt', 
           'Woodrow Wilson', 
           'Jimmy Carter', 
           'Barack Obama')


  1. Show the winners with first name John
SELECT winner 
FROM nobel
WHERE winner LIKE 'John%'


  1. Show the year, subject, and name of Physics winners for 1980 together with the Chemistry winners for 1984.
SELECT * FROM nobel 
WHERE 
(subject = 'Physics' AND yr = 1980)
OR
(subject = 'Chemistry' AND yr = 1984)


  1. Show the year, subject, and name of winners for 1980 excluding(不包括) Chemistry and Medicine
SELECT * FROM nobel 
WHERE 
yr = 1980 
AND 
subject NOT IN ('Chemistry', 'Medicine')


10、Show year, subject, and name of people who won a ‘Medicine’ prize in an early year (before 1910, not including 1910) together with winners of a ‘Literature’ prize in a later year (after 2004, including 2004)

SELECT * FROM nobel 
WHERE 
(subject = 'Medicine' AND yr < 1910)
OR
(subject = 'Literature' AND yr >= 2004)

11、Find all details of the prize won by PETER GRÜNBERG

Non-ASCII characters

The u in his name has an umlaut. You may find this link useful

https://en.wikipedia.org/wiki/%C3%9C#Keyboarding

SELECT * FROM nobel 
WHERE winner = 'PETER GRÜNBERG'


12、Find all details of the prize won by EUGENE O’NEILL

Escaping single quotes

You can’t put a single quote in a quote string directly. You can use two single quotes within a quoted string.

SELECT * FROM nobel 
WHERE winner = 'EUGENE O''NEILL'


  1. Knights in order
    List the winners, year and subject where the winner starts with Sir. Show the the most recent first, then by name order.
SELECT winner, yr, subject FROM nobel 
WHERE winner LIKE 'Sir%' 
ORDER BY yr DESC , winner

14、The expression subject IN (‘Chemistry’,‘Physics’) can be used as a value - it will be 0 or 1.

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 ('Chemistry','Physics'),subject,winner

或

SELECT winner, subject 
FROM nobel
WHERE yr='1984'
ORDER BY 
CASE WHEN subject IN ('physics','chemistry') 
THEN 1
ELSE 0 
END,
subject,winner


注:若这两种方式执行不出笑脸,右上角设置,选择MySQL。

bc99d8eccdc548c88e45eed2f0ca07ab.png


SELECT within SELECT


  1. List each country name where the population is larger than that of ‘Russia’.
SELECT name FROM world
WHERE population >
     (SELECT population FROM world
      WHERE name='Russia')


  1. Show the countries in Europe with a per capita GDP greater than ‘United Kingdom’.
    Per Capita GDP
    The per capita GDP is the gdp/population
select name from world 
where continent = 'Europe'
and
gdp/population > 
    (
    select gdp/population from world 
    where name = 'United Kingdom'
    )
  1. List the name and continent of countries in the continents containing either Argentina or Australia. Order by name of the country.
select name, continent from world 
where continent in
    (select continent from world 
    where name = 'Australia' or name = 'Argentina')
order by name


  1. Which country has a population that is more than Canada but less than Poland? Show the name and the population.
select name, population from world 
where 
population > 
    (select population from world where name = 'Canada')
and
population < 
    (select population from world where name = 'Poland')


5、Germany (population 80 million) has the largest population of the countries in Europe. Austria (population 8.5 million) has 11% of the population of Germany.

Show the name and the population of each country in Europe. Show the population as a percentage of the population of Germany.

select 
name, 
concat(round(
population/(select population from world 
            where name = 'Germany')*100),'%') as percentage
from world 
where continent = 'Europe';


  1. Which countries have a GDP greater than every country in Europe? [Give the name only.] (Some countries may have NULL gdp values)
select name from world 
where gdp >
     all(select gdp from world
      where gdp > 0
      and continent = 'Europe')


  1. Find the largest country (by area) in each continent, show the continent, the name and the area:
SELECT continent, name, area FROM world x
WHERE area >= ALL
       (SELECT area FROM world y
      WHERE y.continent=x.continent
      AND area>0)


  1. List each continent and the name of the country that comes first alphabetically.
select continent, name from world a
where name = (select min(name) from world b
      where a.continent = b.continent)


9、Find the continents where all countries have a population <= 25000000. Then find the names of the countries associated with these continents. Show name, continent and population.

select name, continent, population from world a
where 25000000 >=all (select population from world b
                      where a.continent = b.continent)

10、Some countries have populations more than three times that of all of their neighbours (in the same continent). Give the countries and continents.

select name, continent from world a
where population/3 >= 
all(select population from world b 
    where a.continent = b.continent
    and a.name <> b.name)


相关文章
|
5月前
|
域名解析 缓存 网络协议
阿里云国际站代理商:OSS自定义域名配置教程 解析绑定与HTTPS证书设置
在阿里云上给 OSS 配置自定义域名,到这一步卡住的人远比想象中多。域名绑完了,浏览器里敲进去要么 403,要么直接打不开,排查半天才发现问题出在解析、证书或者 Bucket 权限这一层。
|
11月前
|
人工智能 自然语言处理 供应链
全面解析 RPA 机器人:核心逻辑、行业场景、实战案例与发展趋势
RPA机器人是企业数字化转型的“隐形助手”,通过模拟人工操作,自动完成重复性工作。它无需改造系统,高效连接各类软件,广泛应用于金融、制造、医疗、政务等领域,大幅提升效率、降低成本、减少差错。随着AI融合,RPA正从“工具”进化为“智能员工”,实现自然语言交互、自主决策。未来,人人可用的“全民自动化”时代即将到来。
1582 2
|
搜索推荐 小程序 数据可视化
网站用户访问分析入门:新手必懂的6个关键指标
如果你正在运营一个网站,无论是产品官网还是电商平台,用户访问分析一定是绕不开的一项工作。 但对很多刚入门的新手来说,打开一个数据平台,常常是一脸懵: PV?UV?跳出率?这些到底是什么?要看哪些指标才有用? 这篇文章,我们一起看看网站访问分析中最基础的6个核心指标,帮你快速理解并掌握基本的分析能力。
3000 0
|
存储 数据采集 监控
性能测试报告模板
本文档为某某某项目性能测试报告,主要内容包括概述、测试环境、测试方法、测试工具等。主要的读者有性能测试脚本开发人员、性能测试执行人员、性能评估人员、开发人员、项目经理、用户代表等。
7481 1
性能测试报告模板
|
jenkins Java 程序员
实战:向GitHub提交代码时触发Jenkins自动构建
当我们提交代码到GitHub后,可以在Jenkins上执行构建,但是每次都要动手去执行略显麻烦,今天我们就来实战Jenkins的自动构建功能,每次提交代码到GitHub后,Jenkins会进行自动构建
1638 0
实战:向GitHub提交代码时触发Jenkins自动构建
|
JSON Java 测试技术
SpringCloud2023实战之接口服务测试工具SpringBootTest
SpringBootTest同时集成了JUnit Jupiter、AssertJ、Hamcrest测试辅助库,使得更容易编写但愿测试代码。
1066 3
|
前端开发 JavaScript 数据库连接
一、Flask入门介绍
一、Flask入门介绍
738 1
|
运维 数据可视化 搜索推荐
(极态/JIT)新一代软件开发平台
极态云平台,极态云,也叫Jit、极态。是一个基于全新的软件设计理论的开发平台。他继承了经典软件开发理念理论、技术和方法,结合了最新的可视化零代码开发的价值,开创性地创造了面向元素开发方法论、元素动态管理和分层机制、应用继承、通用系统模型,可扩展的可视化工具框架,彻底解决了产业界一直以来非常棘手的许多难题和冲突,大大提升业务系统软件开发和维护的效率,也必将给产业带来更加健康的发展,可以说,是近20年来,软件开发技术最重大的一次进步。
|
SQL 存储 数据库
如何在SQLServer中创建数据库
在SQL Server中创建数据库,可通过SSMS的图形界面或T-SQL语句。在SSMS中,连接到服务器,右键“数据库”选择“新建数据库”,配置属性后点击确定。使用T-SQL,连接到服务器,编写CREATE DATABASE语句指定数据库名称、文件路径及大小信息,执行查询完成创建。确保有足够磁盘空间,并注意权限设置。