SQL 查漏补缺
创始人
2024-05-29 02:47:15
0

1.显示 GDP 至少为 1 万亿(1000000000000;即 12 个零)的国家/地区的名称和人均 GDP。将人均GDP舍入到最接近的 1000,即保留至千位

【解析】round,第2个参数为正数时,表示保留几位小数;如果为负数时,表示取几位数字;

select name, round(gdp/population,-3) from world
where gdp >= 1000000000000;

2.查找1984年获奖者和主题,按主题和获胜者名称排序,并把化学奖和物理奖排到最后面显示。

SELECT winner, subject
FROM nobel
WHERE yr=1984
ORDER BY subject IN ('Physics','Chemistry'),subject,winner

【解析】:

SELECT winner, subject
FROM nobel
WHERE yr=1984
ORDER BY
CASE
WHEN subject IN ('Physics','Chemistry') THEN 1
ELSE 0
END ASC,
subject,
winner

subject in()条件是一个Boolean表达式,
如果满足这个条件,这个表达式的结果=1;否则不满足条件,这个表达式的结果=0.
默认情况下,按条件升序排列;因此排序的结果将是0在前,而1在后;所以满足条件的将会在最后显示。

3.查找名称中有三个或更多A 的国家/地区。
下面展示一些 内联代码片

SELECT name 
FROM worldWHERE name LIKE '%A%A%A%'

4.查询有两个 o 字符,由另外两个字符分隔的国家/地区。

SELECT name 
FROM worldWHERE name LIKE '%o__o%'

5.查找正好有四个字符的国家/地区。

SELECT name 
FROM world
WHERE name LIKE '____'

6.查询:首都名称包含国家名称的首都

select capital, name from world where capital like concat('%',name,'%')

7.显示首都是国家名称扩展的名称和扩展。
【解析】此处用到replace替代函数,replace(原内容,需替换部分,替换后内容)

select name, replace (capital, name, '') from world where capital like concat(name,'%') and capital <> name;

8.列出包括阿根廷或澳大利亚在内的各大洲的国家名称和大洲

select name,continent from world where continent in (select continent from world where name in('Argentina', 'Australia')) order by name

9.哪些国家的GDP比欧洲的每个国家都高?只告诉我名字。(有些国家可能有零gdp值)

SELECT name FROM worldWHERE gdp>All(SELECT gdp FROM world WHERE continent='Europe'AND  gdp IS NOT NULL)

10.在每个洲找出最大的国家(按面积),显示洲,名称和地区:

select continent, name, area
from world x
where area >= all(select areafrom world ywhere x.continent=y.continentand area>0)

or

select continent,name,area from world where area in(select max(area) from world group by continent)

11.列出每一个大陆和按字母顺序排列第一的国家的名称

SELECT continent,name FROM world a WHERE name <= ALL(SELECT name from world b WHERE a.continent = b.continent )ORDER by name

12.找出所有国家的人口都小于2500亿的大陆。然后找出与这些大陆相关的国家的名字。显示姓名,大陆和人口。

SELECT name,continent,population FROM world x WHERE 25000000 >= ALL(SELECT population FROM world y WHERE y.continent = x.continent)

13.有些国家的人口是其邻国(在同一大陆)的三倍还多。给出国家和大陆。

SELECT name,continent FROM world x WHERE x.population/3 >= ALL(SELECT population FROM world y WHERE y.continent = x.continent AND y.name != x.name)

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...