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 worldWHERE population >(SELECT population FROM worldWHERE 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 nameFROM worldWHERE population >= ALL(SELECT populationFROM worldWHERE 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 xWHERE area>= ALL(SELECT area FROM world yWHERE y.continent=x.continentAND 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 ywhere 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 bwhere 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 bwhere a.continent = b.continentand a.name<> b.name)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/68335.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

OpenAI的真正对手?DeepSeek-R1如何用强化学习重构LLM能力边界——DeepSeek-R1论文精读

2025年1月20日&#xff0c;DeepSeek-R1 发布&#xff0c;并同步开源模型权重。截至目前&#xff0c;DeepSeek 发布的 iOS 应用甚至超越了 ChatGPT 的官方应用&#xff0c;直接登顶 AppStore。 DeepSeek-R1 一经发布&#xff0c;各种资讯已经铺天盖地&#xff0c;那就让我们一起…

Baklib如何重塑内容中台的智能化推荐系统实现个性化服务

内容概要 在数字内容日益丰富的今天&#xff0c;内容中台的智能化推荐系统显得尤为重要。它通过分析和处理海量的数据&#xff0c;为用户提供个性化的内容推荐&#xff0c;从而提升用户体验。在智能化推荐系统中&#xff0c;主要由以下几个部分构成&#xff1a; 部分主要功能…

从零推导线性回归:最小二乘法与梯度下降的数学原理

​ 欢迎来到我的主页&#xff1a;【Echo-Nie】 本篇文章收录于专栏【机器学习】 本文所有内容相关代码都可在以下仓库中找到&#xff1a; Github-MachineLearning 1 线性回归 1.1 什么是线性回归 线性回归是一种用来预测和分析数据之间关系的工具。它的核心思想是找到一条直…

【MySQL】 数据类型

欢迎拜访&#xff1a;雾里看山-CSDN博客 本篇主题&#xff1a;【MySQL】 数据类型 发布时间&#xff1a;2025.1.27 隶属专栏&#xff1a;MySQL 目录 数据类型分类数值类型tinyint类型数值越界测试结果说明 bit类型基本语法使用注意事项 小数类型float语法使用注意事项 decimal语…

Tensor 基本操作5 device 管理,使用 GPU 设备 | PyTorch 深度学习实战

前一篇文章&#xff0c;Tensor 基本操作4 理解 indexing&#xff0c;加减乘除和 broadcasting 运算 | PyTorch 深度学习实战 本系列文章 GitHub Repo: https://github.com/hailiang-wang/pytorch-get-started Tensor 基本使用 检查设备创建 tensor 时声明设备更改默认设备创建…

砥砺奋进,展望新程0114

2024年&#xff0c;非凸科技在金融数智化浪潮中奋楫扬帆&#xff0c;实现了跨越式发展。面对市场的起伏变化&#xff0c;我们始终坚守蓄势待发的沉稳姿态&#xff0c;以笃定之心深耕&#xff0c;以坚毅之态磨砺&#xff0c;以无畏之勇突破&#xff0c;于时代洪流中稳健前行。 …

数字人+展厅应用方案:开启全新沉浸式游览体验

随着人们生活质量的不断提升&#xff0c;对于美好体验的追求日益增长。在展厅展馆领域&#xff0c;传统的展示方式已难以满足大众日益多样化的需求。而通过将数字人与展厅进行深度结合&#xff0c;可以打造数字化、智能化新型展厅&#xff0c;不仅能提升展示效果&#xff0c;还…

基于区块链的数字身份认证:安全与隐私的未来

友友们好! 我的新专栏《Python进阶》正式启动啦!这是一个专为那些渴望提升Python技能的朋友们量身打造的专栏,无论你是已经有一定基础的开发者,还是希望深入挖掘Python潜力的爱好者,这里都将是你不可错过的宝藏。 在这个专栏中,你将会找到: ● 深入解析:每一篇文章都将…

RK3588平台开发系列讲解(ARM篇)ARM64底层中断处理

文章目录 一、异常级别二、异常分类2.1、同步异常2.2、异步异常三、中断向量表沉淀、分享、成长,让自己和他人都能有所收获!😄 一、异常级别 ARM64处理器确实定义了4个异常级别(Exception Levels, EL),分别是EL0到EL3。这些级别用于管理处理器的特权级别和权限,级别越高…

AIP-130 方法

编号130原文链接AIP-130: Methods状态批准创建日期2023-03-13更新日期2023-03-13 API由若干个方法组成&#xff0c;方法是服务代表消费者执行的特定操作。 指南 方法类别 以下列举了现存的多种方法类别&#xff0c;通常根据方法操作的对象&#xff08;例如集合或资源&#…

下载Visual Studio Community 2019

官方链接如下&#xff1a;Visual Studio Community 2019下载链接 https://learn.microsoft.com/zh-cn/visualstudio/releases/2019/system-requirements#download 目前官方仅建议2022版&#xff0c;已经关闭vs2019等旧版本&#xff0c;哪天开放了&#xff0c;记得踢我一下。 …

K8s运维管理平台 - xkube体验:功能较多

目录 简介Lic安装1、需要手动安装MySQL&#xff0c;**建库**2、启动命令3、[ERROR] GetNodeMetric Fail:the server is currently unable to handle the request (get nodes.metrics.k8s.io qfusion-1) 使用总结优点优化 补充1&#xff1a;layui、layuimini和beego的详细介绍1.…

JavaScript系列(45)--响应式编程实现详解

JavaScript响应式编程实现详解 &#x1f504; 今天&#xff0c;让我们深入探讨JavaScript的响应式编程实现。响应式编程是一种基于数据流和变化传播的编程范式&#xff0c;它使我们能够以声明式的方式处理异步数据流。 响应式编程基础概念 &#x1f31f; &#x1f4a1; 小知识…

无人机红外热成像:应急消防的“透视眼”

无人机红外热成像&#xff1a;应急消防的“透视眼” 亲爱的小伙伴们&#xff0c;每年一到夏天&#xff0c;应急消防的战士们就像上紧了发条的闹钟&#xff0c;时刻准备应对各种灾害。炎热天气让火灾隐患“蹭蹭”往上涨&#xff0c;南北各地还有防洪救灾、台风、泥石流等灾害轮…

14-6-3C++STL的list

&#xff08;一&#xff09;list的插入 1.list.insert(pos,elem);//在pos位置插入一个elem元素的拷贝&#xff0c;返回新数据的位置 #include <iostream> #include <list> using namespace std; int main() { list<int> lst; lst.push_back(10); l…

【Linux】 冯诺依曼体系与计算机系统架构全解

Linux相关知识点可以通过点击以下链接进行学习一起加油&#xff01;初识指令指令进阶权限管理yum包管理与vim编辑器GCC/G编译器make与Makefile自动化构建GDB调试器与Git版本控制工具Linux下进度条 冯诺依曼体系是现代计算机设计的基石&#xff0c;其统一存储和顺序执行理念推动…

定制Centos镜像

环境准备&#xff1a; 一台最小化安装的干净的系统&#xff0c;这里使用Centos7.9,一个Centos镜像&#xff0c;镜像也使用Centos7.9的。 [rootlocalhost ~]# cat /etc/system-release CentOS Linux release 7.9.2009 (Core) [rootlocalhost ~]# rpm -qa | wc -l 306 [rootloca…

【C++ 动态规划】1024. 视频拼接|1746

本文涉及知识点 C动态规划 LeetCode1024. 视频拼接 你将会获得一系列视频片段&#xff0c;这些片段来自于一项持续时长为 time 秒的体育赛事。这些片段可能有所重叠&#xff0c;也可能长度不一。 使用数组 clips 描述所有的视频片段&#xff0c;其中 clips[i] [starti, end…

EasyExcel写入和读取多个sheet

最近在工作中&#xff0c;作者频频接触到Excel处理&#xff0c;因此也对EasyExcel进行了一定的研究和学习&#xff0c;也曾困扰过如何处理多个sheet&#xff0c;因此此处分享给大家&#xff0c;希望能有所帮助 目录 1.依赖 2. Excel类 3.处理Excel读取和写入多个sheet 4. 执…

字节iOS面试经验分享:HTTP与网络编程

字节iOS面试经验分享&#xff1a;HTTP与网络编程 &#x1f31f; 嗨&#xff0c;我是LucianaiB&#xff01; &#x1f30d; 总有人间一两风&#xff0c;填我十万八千梦。 &#x1f680; 路漫漫其修远兮&#xff0c;吾将上下而求索。 目录 字节iOS面试经验分享&#xff1a;HTT…