This tutorial looks at how we can use SELECT statements within SELECT statements to perform more complex queries.

name continent area population gdp
Afghanistan Asia 652230 25500100 20343000000
Albania Europe 28748 2831741 12960000000
Algeria Africa 2381741 37100000 188681000000
Andorra Europe 468 78115 3712000000
Angola Africa 1246700 20609294 100990000000
...

1、Bigger than Russia

获取人口比俄罗斯多的国家名字

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

2、Richer than UK

获取欧洲国家中,人均gdp高于United Kingdom的国家名称。

Show the countries in Europe with a per capita GDP greater than 'United Kingdom'.

select name from world
where continent='Europe' and gdp/population>(select gdp/population from world where name='United Kingdom');

3、Neighbours of Argentina and Australia

获取国家名称及所在大陆名称,条件是包含阿根廷或者澳大利亚的大陆,按照国家名称升序排序。

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 in('Argentina','Australia'))
order by name;

4、Between Canada and Poland

获取人口大于加拿大,同时人口小于波兰的国家名称、人口。

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、Percentages of Germany

获取欧洲各国家名称,以及各国家人口/德国人口的百分比。

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,0),'%') as percentage
from world
where continent='Europe';

解题思路:️获取德国人口的select语句;️用concat函数,转换成字符串后拼接百分号。

6、Bigger than every country in Europe

获取超过欧洲任何一个国家gdp的国家名字。

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>(select max(gdp) from world where continent='Europe');

解题思路:超过欧洲任何一个国家gdp,这里用到聚合函数max(gdp),意思是取欧洲国家gdp的最大值。

7、Largest in each continent

获取每个大洲面积最大的国家,展示所在大陆、国家名称、面积。

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);

8、First country of each continent (alphabetically)

列出每个大洲以及按字母顺序排在第一位的国家/地区名称。
List each continent and the name of the country that comes first alphabetically. 
select continent,name
from world x
where name=(select min(name) from world y where x.continent=y.continent);

9、Difficult Questions That Utilize Techniques Not Covered In Prior Sections

Find the continents where all countries have a population <= 25000000. Then find the names of the countries associated with these continents. Show namecontinent and population.

select name,continent,population
from world
where continent=(select continent from world
group by continent
having max(population)<=25000000);

解题思路:每个大洲全部国家人口都要小于2500万,用聚合函数max()函数。注意,这时候,不能用where,必须用having,因为where不能和聚合函数一起使用。

10、获取同一大洲,该国家面积大于等于其他所有国家3倍以上的国家名称和大洲名称。

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

select name,continent
from world x
where population>=ALL(select population*3 from world y
where x.continent=y.continent and x.name!=y.name and population>0);

解题思路:

SQLZOO练习三--SELECT within SELECT Tutorial的更多相关文章

  1. sqlzoo - SELECT from WORLD Tutorial 答案

    01.SELECT from WORLD Tutorial 01.显示所有国家的名称,大洲和人口. SELECT name, continent, population FROM world; 02. ...

  2. SELECT within SELECT Tutorial -- SQLZOO

    SELECT within SELECT Tutorial 注意:where语句中对表示条件的需要用单引号, 下面的译文使用的是有道翻译如有不正确,请直接投诉有道 01.List each count ...

  3. sqlzoo刷题 SELECT from Nobel Tutorial

    SELECT from Nobel Tutorial 1.Change the query shown so that it displays Nobel prizes for 1950. SELEC ...

  4. SELECT from Nobel Tutorial

    02.SELECT from Nobel Tutorial 注意:where语句中对表示条件的需要用单引号, 下面的译文使用的是有道翻译如有不正确,请直接投诉有道 01.Change the quer ...

  5. 关于Select * 与Select 字段名 的问题!

    [转]http://blog.csdn.net/tongyu2009/article/details/8252418 1.SELECT * 语句取出表中的所有字段,不论该字段的数据对调用的应用程序是否 ...

  6. Insert Into select 与 Select Into 哪个更快?

    在平常数据库操作的时候,我们有时候会遇到表之间数据复制的情况,可能会用到INSERT INTO SELECT 或者 SELECT INTO : 那么二者语法上有什么区别?性能上又如何呢? 围绕着这两个 ...

  7. oracle 中 insert select 和 select insert 配合使用

    Insert Into select 与 Select Into 哪个更快? 在平常数据库操作的时候,我们有时候会遇到表之间数据复制的情况,可能会用到INSERT INTO SELECT 或者 SEL ...

  8. SQL笔记1:SELECT及SELECT高级应用

      T-SQL笔记1:SELECT及SELECT高级应用 本章摘要 1:安装AdventureWorks 2:基本运算符和表达式 3:between 4:like 5:escape 6:TOP 7:G ...

  9. PHP MySQL Select 之Select

    从数据库表中选取数据 SELECT 语句用于从数据库中选取数据. 语法 SELECT column_name(s) FROM table_name 注释:SQL 语句对大小写不敏感.SELECT 与 ...

  10. select * from (select P.*,ROWNUM RN FROM(select * from Mp_Relatedart where pubbaseid=785 order by ID ASC )P)M WHERE M.RN>2 and M.RN <= 7

    select * from (select P.*,ROWNUM RN FROM(select * from Mp_Relatedart where pubbaseid=785 order by ID ...

随机推荐

  1. Bugku练习题---MISC---1和0的故事

    Bugku练习题---MISC---1和0的故事 flag:flag{QR_c0de_1s_1nterest1n9} 解题步骤: 1.观察题目,下载附件 2.不得不说这道题挺贵的,打开是这个样子的,由 ...

  2. mybatis添加代码出现是第几条数据

  3. SpringBoot整合MybatisPlus基本的增删改查,保姆级教程

    概述 MybatisPlus是国产的第三方插件, 它封装了许多常用的CURDapi,免去了我们写mapper.xml的重复劳动,这里介绍了基本的整合SpringBoot和基础用法. 引入依赖 在项目中 ...

  4. 常见排序算法的golang 实现

    五种基础排序算法对比 五种基础排序算法对比 1:冒泡排序 算法描述 比较相邻的元素.如果第一个比第二个大,就交换它们两个: 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对,这样在最后的元素 ...

  5. Git命令行提交代码步骤

    先进入对应的项目目录 1.拉取服务器代码,避免覆盖他人代码 git pull 2.查看当前项目中有哪些文件被修改过 git status 具体状态如下: 1:Untracked: 未跟踪,一般为新增文 ...

  6. python之模块(os、sys、json、subprocess)

    目录 os模块 sys模块 json模块 subprocess模块 os模块 os模块主要是与操作系统打交道. 导入os模块 import os 创建单层文件夹,路径必须要存在 os.mkdir(路径 ...

  7. MySql笔记Ⅰ

    MySql part 1: 数据库概念 数据库:(DataBase, 简称DB):数据库中的数据按一定的数据模型组织.描述和储存,具有较小的冗余度.较高的数据独立性和易扩展性,并可为各种 用户共享 数 ...

  8. 解读ICDE'22论文:基于鲁棒和可解释自编码器的无监督时间序列离群点检测算法

    摘要:本文提出了两个用于无监督的具备可解释性和鲁棒性时间序列离群点检测的自动编码器框架. 本文分享自华为云社区<解读ICDE'22论文:基于鲁棒和可解释自编码器的无监督时间序列离群点检测算法&g ...

  9. Microsoft Office 代码执行漏洞临时防范方法

    一.删除ms-msdt URI 注册表 1.按下键盘上的快捷组合键:win键 和 R键,打开运行(也可以在开始菜单打开运行). 2.在运行窗口中输入命令:regedit,点击确定或敲回车键就可以快速打 ...

  10. SPFA 最短路算法

    SPFA算法 1.什么是spfa算法? SPFA 算法是 Bellman-Ford算法 的队列优化算法的别称,通常用于求含负权边的单源最短路径,以及判负权环.SPFA一般情况复杂度是O(m)O(m) ...