当前位置: 首页 > article >正文

sqlzoo答案4:SELECT within SELECT Tutorial

sql练习:SELECT within SELECT Tutorial - SQLZoo

world表:

namecontinentareapopulationgdp
AfghanistanAsia6522302550010020343000000
AlbaniaEurope28748283174112960000000
AlgeriaAfrica238174137100000188681000000
AndorraEurope468781153712000000
AngolaAfrica124670020609294100990000000
...

world(name, continent, area, population, gdp)

1. select...where...(...select...)

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.

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

Europe是在continent里面筛选,不是area

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

3. in 、order by

List the name and continent of countries in the continents containing either Argentina or Australia. Order by name of the country.

列出包含阿根廷或澳大利亚的大陆中的国家名称和所属大陆。按国家名称排序。

错误代码:理解错误,Argentina or Australia是国家名

select name, continent
from world
where continent in ('Argentina' , 'Australia')
order by name

正确代码:

select name, continent
from world
where continent in (
select continent 
from world 
where name in ('Argentina' , 'Australia'))
order by name

4.

Which country has a population that is more than United Kingdom but less than Germany? Show the name and the population.

select name, population
from world 
where population > 
(
select population from world
where name = 'United Kingdom')
and 
population <
(
select population from world
where name = 'Germany')

5. concat...as、round

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.

显示每个欧洲国家的名称和人口。以德国人口的百分比显示人口。

The format should be Name, Percentage for example:

namepercentage
Albania3%
Andorra0%
Austria11%
......

Decimal places?

You can use the function ROUND to remove the decimal places.

Percent symbol %

You can use the function CONCAT to add the percentage symbol.

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


To get a well rounded view of the important features of SQL you should move on to the next tutorial concerning aggregates.

To gain an absurdly detailed view of one insignificant feature of the language, read on.

We can use the word ALL to allow >= or > or < or <=to act over a list. For example, you can find the largest country in the world, by population with this query:

我们可以使用单词 ALL 来允许 >= 或 > 或 < 或 <= 在列表上操作。例如,你可以通过这个查询找到世界上人口最多的国家:

SELECT name
  FROM world
 WHERE population >= ALL(SELECT population
                           FROM world
                          WHERE population>0)

You need the condition population>0 in the sub-query as some countries have null for population.


6. all

Which countries have a GDP greater than every country in Europe? [Give the name only.] (Some countries may have NULL gdp values)

哪些国家的GDP高于欧洲所有国家?【仅提供名称】(某些国家可能没有GDP数值)

select name 
from world
where gdp >
all(
select gdp from world
where continent = 'Europe' and gdp > 0 )
name
China
Japan
United States

7.对比同一个洲内 找最大area

Find the largest country (by area) in each continent, show the continent, the name and the area:

The above example is known as a correlated or synchronized sub-query.

找出每个洲面积最大的国家,显示洲名、国家名称和面积: 上述示例被称为相关或同步子查询。

Using correlated subqueries?

A correlated subquery works like a nested loop: the subquery only has access to rows related to a single record at a time in the outer query. The technique relies on table aliases to identify two different uses of the same table, one in the outer query and the other in the subquery.

One way to interpret the line in the WHERE clause that references the two table is “… where the correlated values are the same”.

In the example provided, you would say “select the country details from world where the population is greater than or equal to the population of all countries where the continent is the same”.

使用相关子查询?

相关子查询的工作方式类似于嵌套循环:子查询仅能访问外部查询中当前记录相关的行。这种技术依赖表别名来标识同一张表的两种不同用途,一个在外部查询中,另一个在子查询中。 可以将 WHERE 子句中引用两个表的那一行解释为“…其中相关值相同”。 在提供的示例中,你会说“从 world 表中选择国家详情,其中人口大于或等于所有同一大陆的国家的人口”。

SELECT continent, name, area FROM world x
  WHERE area>= ALL
    (SELECT area FROM world y
        WHERE y.continent=x.continent
          AND population>0)

8.min(name) 按字母顺序排列第一个

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.

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.

查找所有国家人口均不超过25000000的洲。然后找出与这些洲相关的国家名称。显示名称、洲和人口。

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)

http://www.kler.cn/a/519705.html

相关文章:

  • 【阅读笔记】基于整数+分数微分的清晰度评价算子
  • Node.js下载安装及环境配置
  • 医疗集群系统中基于超融合数据库架构的应用与前景探析
  • Java 大视界 -- Java 大数据在元宇宙中的关键技术与应用场景(65)
  • 大模型学习计划
  • C#,入门教程(07)——软件项目的源文件与目录结构
  • 【Leetcode刷题记录】15.三数之和
  • 【2024年华为OD机试】(A卷,200分)- 农场施肥 (JavaScriptJava PythonC/C++)
  • 2025年美赛(MCM/ICM)赛题浅析——助攻快速选题
  • 小智 AI 聊天机器人
  • Python读写各类数据文件
  • 学习数据结构(1)算法复杂度
  • JavaScript系列(44)--微服务架构实现详解
  • 【25考研】中科院软件考研复试难度分析!
  • leetcode 1358. 包含所有三种字符的子字符串数目
  • 【PostgreSQL内核学习 —— (WindowAgg(一))】
  • 基于vue框架的的信用社业务管理系统设计与实现4gnx5(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
  • 包安装利用 LNMP 实现 phpMyAdmin 的负载均衡并利用Redis实现会话保持nginx
  • 如何优化企业的CRM流程管理?
  • 【知识图谱(2)】电影知识图谱构建
  • 【测试人生】变更风险观测的流程逻辑设计
  • Docker基础命令和配置镜像代理(最新)
  • 【竞技宝】DOTA2-裂变天地S1:XG遭遇二连败命悬一线
  • 二叉树的层序遍历||力扣--107
  • chrome插件:网页图片高清下载
  • 使用LPT wiggler jtag自制三星单片机(sam88 core)编程器-S3F9454