当前位置: 首页 > 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)
http://www.lryc.cn/news/526515.html

相关文章:

  • 【fly-iot飞凡物联】(20):2025年总体规划,把物联网整套技术方案和实现并落地,完成项目开发和课程录制。
  • Lucene常用的字段类型lucene检索打分原理
  • 适用于IntelliJ IDEA 2024.1.2部署Tomcat的完整方法,以及笔者踩的坑,避免高血压,保姆级教程
  • XSS靶场通关详解
  • Excel 技巧15 - 在Excel中抠图头像,换背景色(★★)
  • 备忘-humanplus相关的代码解析
  • 青少年编程与数学 02-008 Pyhon语言编程基础 01课题、语言概要
  • XSS (XSS)分类
  • [Linux]el8安全配置faillock:登录失败达阈值自动锁定账户配置
  • 最新-CentOS 7安装1 Panel Linux 服务器运维管理面板
  • selenium定位网页元素
  • 積分方程與簡單的泛函分析8.具連續對稱核的非齊次第II類弗雷德霍姆積分算子方程
  • 长理算法复习
  • 机器学习-K近邻算法
  • 使用rsync+inotify简单实现文件实时双机双向同步
  • Ubuntu 24.04 LTS开机自启动脚本设置方法
  • 谈谈对JavaScript 中的事件冒泡(Event Bubbling)和事件捕获(Event Capturing)的理解
  • 解读2025年生物医药创新技术:展览会与论坛的重要性
  • 【第七天】零基础入门刷题Python-算法篇-数据结构与算法的介绍-一种常见的分治算法(持续更新)
  • Spring Data JPA 实战:构建高性能数据访问层
  • Python JSON:深入解析与高效应用
  • 【C语言进阶(四)】指针进阶详解(上)
  • DDD架构实战第五讲总结:将领域模型转化为代码
  • FPGA实现任意角度视频旋转(完结)视频任意角度旋转实现
  • CPU 缓存基础知识
  • 微信小程序date picker的一些说明
  • Vue3 + TS 实现批量拖拽 文件夹和文件 组件封装
  • 【Kubernetes】Pod生命周期、初始化容器、主容器
  • 2025牛客寒假训练营1-M题
  • css3 svg制作404页面动画效果HTML源码