SQL ZOO 练习 —— SELECT within SELECT Tutorial

2023-11-21 03:10

本文主要是介绍SQL ZOO 练习 —— SELECT within SELECT Tutorial,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

This tutorial looks at how we can use SELECT statements within SELECT statements to perform more complex queries.
在这里插入图片描述

1.List each country name where the population is larger than that of ‘Russia’.

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

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’.

SELECT name FROM world
WHERE continent = 'Europe' AND name IN(SELECT name FROM world WHERE gdp/population > (SELECT gdp/population FROM world WHERE name = 'United Kingdom' ) )

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

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 Kingom 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.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:
在这里插入图片描述

SELECT name,CONCAT(ROUND(100*population/(select population from world where name='Germany'),0),'%')FROM world WHERE continent='Europe'

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

SELECT name
FROM world
WHERE gdp > ALL(SELECT gdp FROM world WHERE gdp>0 AND continent = 'Europe')

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

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

8.List each continent and the name of the country that comes first alphabetically.

第一种方法(符合本节,相关子查询)

SELECT continent,name
FROM world a
WHERE name = (SELECT min(name) FROM world b WHERE a.continent = b.continent )

第二种方法(组合查询),不懂的先不看,后面学到了组合查询就懂了

SELECT continent,MIN(name)
FROM world 
GROUP BY continent

第三中方法

select continent, name from world a where name <= all
(select name from world b where a.continent=b.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.

第一种

SELECT name,continent,population
FROM world 
WHERE continent NOT IN(SELECT continentFROM world WHERE 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 x
WHERE population >= ALL(SELECT 3*population FROM world yWHERE y.continent = x.continentAND y.name <> x.nameAND population > 0)

这篇关于SQL ZOO 练习 —— SELECT within SELECT Tutorial的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/399397

相关文章

使用Dify访问mysql数据库详细代码示例

《使用Dify访问mysql数据库详细代码示例》:本文主要介绍使用Dify访问mysql数据库的相关资料,并详细讲解了如何在本地搭建数据库访问服务,使用ngrok暴露到公网,并创建知识库、数据库访... 1、在本地搭建数据库访问的服务,并使用ngrok暴露到公网。#sql_tools.pyfrom

MySQL中实现多表查询的操作方法(配sql+实操图+案例巩固 通俗易懂版)

《MySQL中实现多表查询的操作方法(配sql+实操图+案例巩固通俗易懂版)》本文主要讲解了MySQL中的多表查询,包括子查询、笛卡尔积、自连接、多表查询的实现方法以及多列子查询等,通过实际例子和操... 目录复合查询1. 回顾查询基本操作group by 分组having1. 显示部门号为10的部门名,员

MyBatis-Plus中静态工具Db的多种用法及实例分析

《MyBatis-Plus中静态工具Db的多种用法及实例分析》本文将详细讲解MyBatis-Plus中静态工具Db的各种用法,并结合具体案例进行演示和说明,具有很好的参考价值,希望对大家有所帮助,如有... 目录MyBATis-Plus中静态工具Db的多种用法及实例案例背景使用静态工具Db进行数据库操作插入

MySQL中COALESCE函数示例详解

《MySQL中COALESCE函数示例详解》COALESCE是一个功能强大且常用的SQL函数,主要用来处理NULL值和实现灵活的值选择策略,能够使查询逻辑更清晰、简洁,:本文主要介绍MySQL中C... 目录语法示例1. 替换 NULL 值2. 用于字段默认值3. 多列优先级4. 结合聚合函数注意事项总结C

通过ibd文件恢复MySql数据的操作方法

《通过ibd文件恢复MySql数据的操作方法》文章介绍通过.ibd文件恢复MySQL数据的过程,包括知道表结构和不知道表结构两种情况,对于知道表结构的情况,可以直接将.ibd文件复制到新的数据库目录并... 目录第一种情况:知道表结构第二种情况:不知道表结构总结今天干了一件大事,安装1Panel导致原来服务

mysql关联查询速度慢的问题及解决

《mysql关联查询速度慢的问题及解决》:本文主要介绍mysql关联查询速度慢的问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql关联查询速度慢1. 记录原因1.1 在一次线上的服务中1.2 最终发现2. 解决方案3. 具体操作总结mysql

Linux搭建Mysql主从同步的教程

《Linux搭建Mysql主从同步的教程》:本文主要介绍Linux搭建Mysql主从同步的教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux搭建mysql主从同步1.启动mysql服务2.修改Mysql主库配置文件/etc/my.cnf3.重启主库my

MySql中的数据库连接池详解

《MySql中的数据库连接池详解》:本文主要介绍MySql中的数据库连接池方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql数据库连接池1、概念2、为什么会出现数据库连接池3、原理4、数据库连接池的提供商5、DataSource数据源6、DBCP7、C

MySQL的隐式锁(Implicit Lock)原理实现

《MySQL的隐式锁(ImplicitLock)原理实现》MySQL的InnoDB存储引擎中隐式锁是一种自动管理的锁,用于保证事务在行级别操作时的数据一致性和安全性,本文主要介绍了MySQL的隐式锁... 目录1. 背景:什么是隐式锁?2. 隐式锁的工作原理3. 隐式锁的类型4. 隐式锁的实现与源代码分析4

MySQL中Next-Key Lock底层原理实现

《MySQL中Next-KeyLock底层原理实现》Next-KeyLock是MySQLInnoDB存储引擎中的一种锁机制,结合记录锁和间隙锁,用于高效并发控制并避免幻读,本文主要介绍了MySQL中... 目录一、Next-Key Lock 的定义与作用二、底层原理三、源代码解析四、总结Next-Key L