sqlzoo答案4:SELECT within SELECT Tutorial
sql练习:SELECT within SELECT Tutorial - SQLZoo
world表:
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 |
... |
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:
name | percentage |
---|---|
Albania | 3% |
Andorra | 0% |
Austria | 11% |
... | ... |
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 name, continent 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练习:SELECT within SELECT Tutorial - SQLZoo world表: namecontinentareapopulationgdpAfghanistanAsia6522302550010020343000000AlbaniaEurope28748283174112960000000AlgeriaAfrica238174137100000188681000000AndorraEurope46878115371200000…...
【fly-iot飞凡物联】(20):2025年总体规划,把物联网整套技术方案和实现并落地,完成项目开发和课程录制。
前言 fly-iot飞凡物联专栏: https://blog.csdn.net/freewebsys/category_12219758.html 1,开源项目地址进行项目开发 https://gitee.com/fly-iot/fly-iot-platform 完成项目开发,接口开发。 把相关内容总结成文档,并录制课程。…...
Lucene常用的字段类型lucene检索打分原理
在 Apache Lucene 中,Field 类是文档中存储数据的基础。不同类型的 Field 用于存储不同类型的数据(如文本、数字、二进制数据等)。以下是一些常用的 Field 类型及其底层存储结构: TextField: 用途:用于存储…...

适用于IntelliJ IDEA 2024.1.2部署Tomcat的完整方法,以及笔者踩的坑,避免高血压,保姆级教程
Tips:创建部署Tomcat直接跳转到四 一、软件准备 笔者用的是IntelliJ IDEA 2024.1.2和Tomcat 8.5。之前我使用的是Tomcat 10,但遇到了许多问题。其中一个主要问题是需要使用高于1.8版本的JDK,为此我下载了新的JDK版本,但这又引发了更多的兼容…...

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

Excel 技巧15 - 在Excel中抠图头像,换背景色(★★)
本文讲了如何在Excel中抠图头像,换背景色。 1,如何在Excel中抠图头像,换背景色 大家都知道在PS中可以很容易抠图头像,换背景色,其实Excel中也可以抠简单的图,换背景色。 ※所用头像图片为百度搜索&#x…...
备忘-humanplus相关的代码解析
-1: numpy必须为1.20.0,否则会报错,版本冲突0.rlvalue-based: 如q-learning(走迷宫),对当前状态下作出的动作进行价值计算,通过贪婪策略穷尽所有可能选择最佳state-action,但是对于连续的动作空间&#x…...

青少年编程与数学 02-008 Pyhon语言编程基础 01课题、语言概要
青少年编程与数学 02-008 Pyhon语言编程基础 01课题、语言概要 一、榜一大哥起源与早期发展版本演进与社区壮大应用领域的拓展编程语言排行榜的常客结语 二、当前排行三、出色表现四、易学易用五、特色显著六、资源丰富初学者资源中高级学习资源在线编程学习平台 课题摘要:本文…...
XSS (XSS)分类
XSS (XSS) 概要 XSS全称为Cross Site Scripting,为了和CSS分开简写为XSS,中文名为跨站脚本。该漏洞发生在用户端,是指在渲染过程中发生了不在预期过程中的JavaScript代码执行。XSS通常被用于获取Cookie、以受攻击者的…...
[Linux]el8安全配置faillock:登录失败达阈值自动锁定账户配置
前言 本篇文章的配置仅使用于el8版本的Linux,目前已在centos8、BCLinux8上验证成功,其他版本系统是否可行还得考查。 el8中管理用户登录失败锁定账户所用的模块是faillock.so,如果想要将配置应用与其他版本的Linux,建议确认Linux…...

最新-CentOS 7安装1 Panel Linux 服务器运维管理面板
CentOS 7安装1 Panel Linux 服务器运维管理面板 一、前言二、环境要求三、在线安装四、离线安装1.点击下面1 Panel官网链接访问下载,如未登录或注册,请登录/注册后下载2.使用将离线安装包上传至目标终端/tem目录下3.进入到/tem目录下解压离线安装包4.执行…...
selenium定位网页元素
1、概述 在使用 Selenium 进行自动化测试时,定位网页元素是核心功能之一。Selenium 提供了多种定位方法,每种方法都有其适用场景和特点。以下是通过 id、linkText、partialLinkText、name、tagName、xpath、className 和 cssSelector 定位元素的…...
積分方程與簡單的泛函分析8.具連續對稱核的非齊次第II類弗雷德霍姆積分算子方程
1)def求解具連續對稱核的非齊次第II類弗雷德霍姆積分算子方程 设 是定义在上的连续对称核函数, 非齐次第二类弗雷德霍姆积分算子方程的形式为: , 其中是未知函数,是给定的连续函数,是参数。 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数据集是机器学习领域经典数据集,鸢尾花数据集包含了150条鸢尾花信息,每50条取自三个鸢尾花中之一:Versicolour、Setosa…...

使用rsync+inotify简单实现文件实时双机双向同步
使用rsyncinotify简单实现文件实时双机双向同步 实现思路 使用inotify-tools的inotifywait工具监控文件变化,触发后使用rsync做同步。加入系统服务项,实现实时监听,方便管理。 以下配置操作,单向同步,只需在单边部…...
Ubuntu 24.04 LTS开机自启动脚本设置方法
目录 Ubuntu中设置开机自启动脚本步骤1:修改 rc-local.service文件步骤2:创建/etc/rc.local文件步骤3:修改/etc/rc.local的权限步骤4:启动rc-local.service步骤5:查看rc-local.service的服务状态 Ubuntu中设置开机自启…...
谈谈对JavaScript 中的事件冒泡(Event Bubbling)和事件捕获(Event Capturing)的理解
JavaScript 中的事件冒泡(Event Bubbling)和事件捕获(Event Capturing),是浏览器在处理事件时采用的两种机制,它们在事件的传播顺序上有显著区别。这两种机制帮助开发者在事件触发时,能够以不同…...

解读2025年生物医药创新技术:展览会与论坛的重要性
2025生物医药创新技术与应用发展展览会暨论坛,由天津市生物医药行业协会、BIO CHINA生物发酵展组委会携手主办,山东信世会展服务有限公司承办,定于2025年3月3日至5日在济南黄河国际会展中心盛大开幕。展会规模60000平方米、800参展商、35场会…...
【第七天】零基础入门刷题Python-算法篇-数据结构与算法的介绍-一种常见的分治算法(持续更新)
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言一、Python数据结构与算法的详细介绍1.Python中的常用的分治算法2. 分治算法3.详细的分治代码1)一种常见的分治算法 总结 前言 提示:这…...
【Linux】shell脚本忽略错误继续执行
在 shell 脚本中,可以使用 set -e 命令来设置脚本在遇到错误时退出执行。如果你希望脚本忽略错误并继续执行,可以在脚本开头添加 set e 命令来取消该设置。 举例1 #!/bin/bash# 取消 set -e 的设置 set e# 执行命令,并忽略错误 rm somefile…...

2.Vue编写一个app
1.src中重要的组成 1.1main.ts // 引入createApp用于创建应用 import { createApp } from "vue"; // 引用App根组件 import App from ./App.vue;createApp(App).mount(#app)1.2 App.vue 其中要写三种标签 <template> <!--html--> </template>…...

【OSG学习笔记】Day 16: 骨骼动画与蒙皮(osgAnimation)
骨骼动画基础 骨骼动画是 3D 计算机图形中常用的技术,它通过以下两个主要组件实现角色动画。 骨骼系统 (Skeleton):由层级结构的骨头组成,类似于人体骨骼蒙皮 (Mesh Skinning):将模型网格顶点绑定到骨骼上,使骨骼移动…...

IT供电系统绝缘监测及故障定位解决方案
随着新能源的快速发展,光伏电站、储能系统及充电设备已广泛应用于现代能源网络。在光伏领域,IT供电系统凭借其持续供电性好、安全性高等优势成为光伏首选,但在长期运行中,例如老化、潮湿、隐裂、机械损伤等问题会影响光伏板绝缘层…...

分布式增量爬虫实现方案
之前我们在讨论的是分布式爬虫如何实现增量爬取。增量爬虫的目标是只爬取新产生或发生变化的页面,避免重复抓取,以节省资源和时间。 在分布式环境下,增量爬虫的实现需要考虑多个爬虫节点之间的协调和去重。 另一种思路:将增量判…...
Android第十三次面试总结(四大 组件基础)
Activity生命周期和四大启动模式详解 一、Activity 生命周期 Activity 的生命周期由一系列回调方法组成,用于管理其创建、可见性、焦点和销毁过程。以下是核心方法及其调用时机: onCreate() 调用时机:Activity 首次创建时调用。…...

AI病理诊断七剑下天山,医疗未来触手可及
一、病理诊断困局:刀尖上的医学艺术 1.1 金标准背后的隐痛 病理诊断被誉为"诊断的诊断",医生需通过显微镜观察组织切片,在细胞迷宫中捕捉癌变信号。某省病理质控报告显示,基层医院误诊率达12%-15%,专家会诊…...

给网站添加live2d看板娘
给网站添加live2d看板娘 参考文献: stevenjoezhang/live2d-widget: 把萌萌哒的看板娘抱回家 (ノ≧∇≦)ノ | Live2D widget for web platformEikanya/Live2d-model: Live2d model collectionzenghongtu/live2d-model-assets 前言 网站环境如下,文章也主…...
【前端异常】JavaScript错误处理:分析 Uncaught (in promise) error
在前端开发中,JavaScript 异常是不可避免的。随着现代前端应用越来越多地使用异步操作(如 Promise、async/await 等),开发者常常会遇到 Uncaught (in promise) error 错误。这个错误是由于未正确处理 Promise 的拒绝(r…...

elementUI点击浏览table所选行数据查看文档
项目场景: table按照要求特定的数据变成按钮可以点击 解决方案: <el-table-columnprop"mlname"label"名称"align"center"width"180"><template slot-scope"scope"><el-buttonv-if&qu…...