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

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)

相关文章:

sqlzoo答案4:SELECT within SELECT Tutorial

sql练习&#xff1a;SELECT within SELECT Tutorial - SQLZoo world表&#xff1a; namecontinentareapopulationgdpAfghanistanAsia6522302550010020343000000AlbaniaEurope28748283174112960000000AlgeriaAfrica238174137100000188681000000AndorraEurope46878115371200000…...

【fly-iot飞凡物联】(20):2025年总体规划,把物联网整套技术方案和实现并落地,完成项目开发和课程录制。

前言 fly-iot飞凡物联专栏&#xff1a; https://blog.csdn.net/freewebsys/category_12219758.html 1&#xff0c;开源项目地址进行项目开发 https://gitee.com/fly-iot/fly-iot-platform 完成项目开发&#xff0c;接口开发。 把相关内容总结成文档&#xff0c;并录制课程。…...

Lucene常用的字段类型lucene检索打分原理

在 Apache Lucene 中&#xff0c;Field 类是文档中存储数据的基础。不同类型的 Field 用于存储不同类型的数据&#xff08;如文本、数字、二进制数据等&#xff09;。以下是一些常用的 Field 类型及其底层存储结构&#xff1a; TextField&#xff1a; 用途&#xff1a;用于存储…...

适用于IntelliJ IDEA 2024.1.2部署Tomcat的完整方法,以及笔者踩的坑,避免高血压,保姆级教程

Tips:创建部署Tomcat直接跳转到四 一、软件准备 笔者用的是IntelliJ IDEA 2024.1.2和Tomcat 8.5。之前我使用的是Tomcat 10&#xff0c;但遇到了许多问题。其中一个主要问题是需要使用高于1.8版本的JDK&#xff0c;为此我下载了新的JDK版本&#xff0c;但这又引发了更多的兼容…...

XSS靶场通关详解

前言 这里作者采用phpstudy部署的xss-lab靶场&#xff0c;配置如下&#xff1a; 第一关 进入靶场后寻找页面的传参处&#xff0c;发现url中的name参数传了test给页面&#xff0c;可以在此处进行尝试xss 成功弹窗&#xff01; payload&#xff1a; <script>alert(1)<…...

Excel 技巧15 - 在Excel中抠图头像,换背景色(★★)

本文讲了如何在Excel中抠图头像&#xff0c;换背景色。 1&#xff0c;如何在Excel中抠图头像&#xff0c;换背景色 大家都知道在PS中可以很容易抠图头像&#xff0c;换背景色&#xff0c;其实Excel中也可以抠简单的图&#xff0c;换背景色。 ※所用头像图片为百度搜索&#x…...

备忘-humanplus相关的代码解析

-1: numpy必须为1.20.0&#xff0c;否则会报错&#xff0c;版本冲突0.rlvalue-based: 如q-learning&#xff08;走迷宫&#xff09;&#xff0c;对当前状态下作出的动作进行价值计算&#xff0c;通过贪婪策略穷尽所有可能选择最佳state-action,但是对于连续的动作空间&#x…...

青少年编程与数学 02-008 Pyhon语言编程基础 01课题、语言概要

青少年编程与数学 02-008 Pyhon语言编程基础 01课题、语言概要 一、榜一大哥起源与早期发展版本演进与社区壮大应用领域的拓展编程语言排行榜的常客结语 二、当前排行三、出色表现四、易学易用五、特色显著六、资源丰富初学者资源中高级学习资源在线编程学习平台 课题摘要:本文…...

XSS (XSS)分类

XSS &#xff08;XSS&#xff09; 概要 XSS全称为Cross Site Scripting&#xff0c;为了和CSS分开简写为XSS&#xff0c;中文名为跨站脚本。该漏洞发生在用户端&#xff0c;是指在渲染过程中发生了不在预期过程中的JavaScript代码执行。XSS通常被用于获取Cookie、以受攻击者的…...

[Linux]el8安全配置faillock:登录失败达阈值自动锁定账户配置

前言 本篇文章的配置仅使用于el8版本的Linux&#xff0c;目前已在centos8、BCLinux8上验证成功&#xff0c;其他版本系统是否可行还得考查。 el8中管理用户登录失败锁定账户所用的模块是faillock.so&#xff0c;如果想要将配置应用与其他版本的Linux&#xff0c;建议确认Linux…...

最新-CentOS 7安装1 Panel Linux 服务器运维管理面板

CentOS 7安装1 Panel Linux 服务器运维管理面板 一、前言二、环境要求三、在线安装四、离线安装1.点击下面1 Panel官网链接访问下载&#xff0c;如未登录或注册&#xff0c;请登录/注册后下载2.使用将离线安装包上传至目标终端/tem目录下3.进入到/tem目录下解压离线安装包4.执行…...

selenium定位网页元素

1、概述 在使用 Selenium 进行自动化测试时&#xff0c;定位网页元素是核心功能之一。Selenium 提供了多种定位方法&#xff0c;每种方法都有其适用场景和特点。以下是通过 id、linkText、partialLinkText、name、tagName、xpath、className 和 cssSelector 定位元素的…...

積分方程與簡單的泛函分析8.具連續對稱核的非齊次第II類弗雷德霍姆積分算子方程

1)def求解具連續對稱核的非齊次第II類弗雷德霍姆積分算子方程 设 是定义在上的连续对称核函数&#xff0c; 非齐次第二类弗雷德霍姆积分算子方程的形式为&#xff1a; &#xff0c; 其中是未知函数&#xff0c;是给定的连续函数&#xff0c;是参数。 2)def其特徵值是否一致…...

长理算法复习

选择排序 #include<iostream>using namespace std;const int N 1010; int a[N]; int n;void selectSort(){for (int i 0; i < n;i){int pos i;for (int j i 1; j < n;j){if(a[j]<a[pos])pos j;}swap(a[i], a[pos]);} }int main() {cin >> n;for (i…...

机器学习-K近邻算法

文章目录 一. 数据集介绍Iris plants dataset 二. 代码三. k值的选择 一. 数据集介绍 鸢尾花数据集 鸢尾花Iris Dataset数据集是机器学习领域经典数据集&#xff0c;鸢尾花数据集包含了150条鸢尾花信息&#xff0c;每50条取自三个鸢尾花中之一&#xff1a;Versicolour、Setosa…...

使用rsync+inotify简单实现文件实时双机双向同步

使用rsyncinotify简单实现文件实时双机双向同步 实现思路 使用inotify-tools的inotifywait工具监控文件变化&#xff0c;触发后使用rsync做同步。加入系统服务项&#xff0c;实现实时监听&#xff0c;方便管理。 以下配置操作&#xff0c;单向同步&#xff0c;只需在单边部…...

Ubuntu 24.04 LTS开机自启动脚本设置方法

目录 Ubuntu中设置开机自启动脚本步骤1&#xff1a;修改 rc-local.service文件步骤2&#xff1a;创建/etc/rc.local文件步骤3&#xff1a;修改/etc/rc.local的权限步骤4&#xff1a;启动rc-local.service步骤5&#xff1a;查看rc-local.service的服务状态 Ubuntu中设置开机自启…...

谈谈对JavaScript 中的事件冒泡(Event Bubbling)和事件捕获(Event Capturing)的理解

JavaScript 中的事件冒泡&#xff08;Event Bubbling&#xff09;和事件捕获&#xff08;Event Capturing&#xff09;&#xff0c;是浏览器在处理事件时采用的两种机制&#xff0c;它们在事件的传播顺序上有显著区别。这两种机制帮助开发者在事件触发时&#xff0c;能够以不同…...

解读2025年生物医药创新技术:展览会与论坛的重要性

2025生物医药创新技术与应用发展展览会暨论坛&#xff0c;由天津市生物医药行业协会、BIO CHINA生物发酵展组委会携手主办&#xff0c;山东信世会展服务有限公司承办&#xff0c;定于2025年3月3日至5日在济南黄河国际会展中心盛大开幕。展会规模60000平方米、800参展商、35场会…...

【第七天】零基础入门刷题Python-算法篇-数据结构与算法的介绍-一种常见的分治算法(持续更新)

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、Python数据结构与算法的详细介绍1.Python中的常用的分治算法2. 分治算法3.详细的分治代码1&#xff09;一种常见的分治算法 总结 前言 提示&#xff1a;这…...

Pixel Mind Decoder 数据结构优化:提升批量文本情绪处理效率

Pixel Mind Decoder 数据结构优化&#xff1a;提升批量文本情绪处理效率 1. 为什么需要优化批量处理 当你需要分析成千上万条用户评论或社交媒体内容时&#xff0c;逐条调用情绪分析模型会变得非常低效。就像在快餐店点餐一样&#xff0c;一个一个处理订单远不如批量处理来得…...

别再只盯着Loss曲线了!TensorBoard的SCALARS面板还有这些隐藏玩法(附GAN训练实战)

解锁TensorBoard SCALARS面板的隐藏战力&#xff1a;从GAN训练曲线中洞察模型灵魂 当你盯着GAN训练中那对纠缠不清的生成器和判别器Loss曲线时&#xff0c;是否感觉像在解读一部悬疑小说&#xff1f;TensorBoard的SCALARS面板远比大多数开发者想象的强大——它不仅是数据的展示…...

TinyNAS子网硬件感知编译:针对T4 GPU的CUDA kernel自动调优

TinyNAS子网硬件感知编译&#xff1a;针对T4 GPU的CUDA kernel自动调优 1. 项目概述 1.1 这是什么技术&#xff1f; TinyNAS子网硬件感知编译是一项专门针对NVIDIA T4 GPU优化的深度学习编译技术。它通过智能分析神经网络结构和硬件特性&#xff0c;自动生成最优的CUDA kern…...

FPGA做信号处理,你的浮点加减法拖后腿了吗?聊聊Vivado Floating-point IP核的性能调优

FPGA信号处理中浮点加减法的性能瓶颈与Vivado Floating-point IP核深度调优 在雷达脉冲压缩、波束成形等实时信号处理系统中&#xff0c;浮点运算单元往往是制约整体性能的关键瓶颈。许多工程师在完成基础功能验证后&#xff0c;常发现系统吞吐量不达标或时序无法收敛&#xff…...

Open WebUI:重构人机交互的开源解决方案

Open WebUI&#xff1a;重构人机交互的开源解决方案 【免费下载链接】open-webui Open WebUI 是一个可扩展、功能丰富且用户友好的自托管 WebUI&#xff0c;设计用于完全离线操作&#xff0c;支持各种大型语言模型&#xff08;LLM&#xff09;运行器&#xff0c;包括Ollama和兼…...

Docker容器中运行Windows系统的突破性企业级解决方案:架构解析与部署实践

Docker容器中运行Windows系统的突破性企业级解决方案&#xff1a;架构解析与部署实践 【免费下载链接】windows Windows inside a Docker container. 项目地址: https://gitcode.com/GitHub_Trending/wi/windows 在当今云原生和容器化技术快速发展的背景下&#xff0c;如…...

UE5 Python远程执行:利用UDP组播实现高效命令分发

1. 为什么需要UE5 Python远程执行&#xff1f; 想象一下这个场景&#xff1a;你正在开发一个大型UE5项目&#xff0c;团队里有10个设计师需要同时修改场景参数。传统做法是每个人手动操作编辑器&#xff0c;或者通过RPC一个个连接。这种方式的效率有多低&#xff0c;相信每个开…...

基于SpringBoot + Vue的垃圾分类回收网站(角色:用户、回收人员、管理员)

文章目录前言一、详细操作演示视频二、具体实现截图三、技术栈1.前端-Vue.js2.后端-SpringBoot3.数据库-MySQL4.系统架构-B/S四、系统测试1.系统测试概述2.系统功能测试3.系统测试结论五、项目代码参考六、数据库代码参考七、项目论文示例结语前言 &#x1f49b;博主介绍&#…...

智能配置黑苹果终极指南:OpCore Simplify一键生成OpenCore EFI完整教程

智能配置黑苹果终极指南&#xff1a;OpCore Simplify一键生成OpenCore EFI完整教程 【免费下载链接】OpCore-Simplify A tool designed to simplify the creation of OpenCore EFI 项目地址: https://gitcode.com/GitHub_Trending/op/OpCore-Simplify 还在为繁琐的黑苹果…...

Hunyuan-MT Pro详细步骤:本地启动http://localhost:6666翻译终端

Hunyuan-MT Pro详细步骤&#xff1a;本地启动http://localhost:6666翻译终端 1. 快速了解Hunyuan-MT Pro Hunyuan-MT Pro是一个基于腾讯混元开源模型构建的现代化翻译工具&#xff0c;它把强大的AI翻译能力包装成了一个简单易用的网页应用。你不需要懂复杂的技术&#xff0c;…...