有什么问题,欢迎评论或私聊。转载请私聊博主,谢谢。
原题链接:https://sqlzoo.net/wiki/SELECT_within_SELECT_Tutorial
其他题解连接:https://blog.csdn.net/aiqiyizz/article/details/109057732
题解对应的是英文版题目。
文章目录
- 4 SELECT within SELECT
-
- 4.1 Bigger than Russia
- 4.2 Richer than UK
- 4.3 Neighbours of Argentina and Australia
- 4.4 Between Canada and Poland
- 4.5 Percentages of Germany
- 4.6 Bigger than every country in Europe
- 4.7 Largest in each continent
- 4.8 First country of each continent (alphabetically)
- 4.9 Difficult Questions That Utilize Techniques Not Covered In Prior Sections
- 4.10
4 SELECT within SELECT
4.1 Bigger than Russia
SELECT name FROM world
WHERE population >
(SELECT population FROM world
WHERE name='Russia')
4.2 Richer than UK
SELECT name
FROM world
WHERE continent = 'Europe'
AND gdp/population > (
SELECT gdp/population
FROM world
WHERE name = 'United Kingdom'
)
4.3 Neighbours of Argentina and Australia
找到洲 ⇒ \Rightarrow ⇒找到对应洲的国家
SELECT name, continent
FROM world
WHERE continent IN (
SELECT continent
FROM world
WHERE name IN ('Argentina', 'Australia')
)
ORDER BY name
4.4 Between Canada and Poland
SELECT name, population
FROM world
WHERE population > (
SELECT population
FROM world
WHERE name = 'Canada'
)
AND
population < (
SELECT population
FROM world
WHERE name = 'Poland'
)
以下也为正解(滑稽)
SELECT name, population
FROM world
WHERE 0=1
4.5 Percentages of Germany
找到德国的人口 ⇒ \Rightarrow ⇒用各种函数拼出答案
SELECT
name,
CONCAT(
ROUND(
population /(
SELECT
population
FROM
world
WHERE
name = 'Germany'
) * 100,
0
),
'%'
) AS population
FROM
world
WHERE
continent = 'Europe'
4.6 Bigger than every country in Europe
SELECT name
FROM world
WHERE gdp > (
SELECT max(gdp)
FROM world
WHERE continent = 'Europe'
GROUP BY continent
)
4.7 Largest in each continent
SELECT continent, name, area FROM world x
WHERE area>= ALL
(SELECT area FROM world y
WHERE y.continent=x.continent
AND area>0)
4.8 First country of each continent (alphabetically)
SELECT continent, min(name)
FROM world
GROUP BY continent
4.9 Difficult Questions That Utilize Techniques Not Covered In Prior Sections
找到每个洲最大值 ⇒ \Rightarrow ⇒找到最大值符合的洲 ⇒ \Rightarrow ⇒找到这些洲对应的国家
注意需要添加AS temp1
,否则会出现Every derived table must have its own alias
。
SELECT name, continent, population
FROM world
WHERE continent IN (
SELECT continent
FROM (
SELECT continent, max(population) AS Mpop
FROM world
GROUP BY continent
) AS temp1
WHERE Mpop <= 25000000
)
4.10
被迫使用ALL,不过也不难
SELECT name, continent
FROM world x
WHERE population >= ALL (
SELECT 3 * population
FROM world y
WHERE x.continent = y.continent
AND y.name <> x.name
)