SQL 组合 SELECT 语句

SQL combined SELECT statement

这实际上来自一个互动网站 SQLzoo.net,我用它来复习我的 SQL 知识。

问题是

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 x 
WHERE population <= ALL(SELECT population 
                        FROM world y 
                        WHERE y.continent = x.continent 
                          AND population > 25000000)

我不想复制和编辑输出,因为它非常乏味 - 这可以通过进入本页的第 7 项来检查 - http://www.sqlzoo.net/wiki/SELECT_within_SELECT_Tutorial

我想弄明白我写错了什么。其他问题我都做对了,但这一个似乎有点棘手(或者可能比我想象的更多嵌套 SELECT 查询)。

任何 suggestion/explanation 不胜感激。

这是一种方式:

基本上,该大陆必须位于国家数与人口少于或等于该数量的国家数相同的大陆列表中。

列表由子查询决定。

人口少于该数量的国家/地区数量由条件聚合确定。

select name, continent, population
  from world
 where continent in
       (select continent
          from world
         group by continent
        having count(*)
             = sum(case when population <= 25000000 then 1 else 0 end))

附带说明,minus 将在 Oracle 或 SQL 服务器中工作:

select name, continent, population
  from world
 where continent in (select continent
                       from world
                     minus
                     select continent
                       from world
                      where population > 25000000)

我已经写了 SQL 很久很久了,几乎从来没有使用过 ALLSOMEANY

对我来说,编写此查询的明显方法是使用 window 函数:

SELECT name, continent, population 
FROM (SELECT w.*, MAX(population) OVER (PARTITION BY continent) as maxpop
      FROM world w
     ) w
WHERE maxpop < 250000000;

如果您不喜欢该解决方案,请使用明确的 joinaggregation:

SELECT name, continent, population 
FROM world w JOIN
     (SELECT continent
      FROM world
      GROUP BY continent
      HAVING max(pop) < 250000000
     ) c
     ON w.continent = c.continent;
SELECT name, continent, population 
FROM world w
WHERE NOT EXISTS (                  -- there are no countries
   SELECT *
   FROM world nx
   WHERE nx.continent = w.continent -- on the same continent
   AND nx.population > 25000000     -- with more than 25M population 
   );

我写了这个并且有效。当我按原样使用给定条件时,它变得越来越复杂,所以我使用了否定。如果一个大陆至少有一个人口超过 2500 万的国家,您可能希望从 select 中跳过它。你的第二个 select 不符合这个逻辑。因此错误。

开始了:

select name,continent,population from world
where not continent in
(select distinct continent from world where population >25000000)

上面的代码获取至少有一个人口超过 2500 万的国家的大陆,首先 select 获取所有不属于该大陆的国家。如果您注意到我们不必再次检查人口,因为所有国家/地区的人口显然都少于或等于 2500 万。

以下代码对我有用:

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

'ALL'部分比较大陆所有国家的人口与25000000,如果小于25000000,则打印其中所有国家的名称,人口。

这是一个简单的方法,确实有效

select name,continent ,population from world 
    where continent in( select continent from world group by
         continent having MAX(population)<=25000000 )

以下查询对我有效

Select name, continent ,population 
from world 
where continent not in 
(
    Select continent  
    from world 
    where population >= 25000000
)

我认为这会很简单...

select name,continent, population from world where continent not in ( select continent from world where population >= 25000000)

只需过滤掉人口大于 2500 万的大陆,剩下的就给你了..

我认为这应该有所帮助:

SELECT name, continent, population FROM world
WHERE continent IN (
SELECT distinct  continent FROM world
GROUP BY continent HAVING MAX(population)<=25000000
)

SELECT 姓名、大陆、人口 来自世界 WHERE 大陆不在 (SELECT continent FROM where where population > 25000000)

这也适用于使用 'ALL' 语法:

SELECT name, continent, population
FROM world w1
WHERE population <= ALL(SELECT continent
                        FROM world w2
                        WHERE population > 25000000 
                        AND w1.continent = w2.continent) 
SELECT name, continent, population FROM world WHERE continent = (SELECT continent
FROM world x
WHERE population <= 25000000
GROUP BY continent
HAVING COUNT(name) = (SELECT COUNT(name) FROM world y WHERE x.continent = y.continent GROUP BY continent))

我想到了这个

select name,continent,population from world where continent in
(
   select continent from world where population in
   (select max(population) from world group by continent) 
   and population<= 25000000
)
select name, continent, population
FROM 
 (
 select continent, name, population, 
 SUM(indicator) OVER (PARTITION BY continent) total
 FROM
  (select 
  continent, name, population,
  case when population > 25000000 THEN 1 ELSE 0 END indicator
  from world
  )sub1 
 )sub2
WHERE total = 0
ORDER BY name

这是我使用并得到正确的代码

SELECT name, continent, population FROM world x 
     WHERE (SELECT MAX(y.population) FROM world y WHERE y.continent = x.continent ) <= 25000000

我是这样解决的:

WITH  MY_TABLE AS
(SELECT CONTINENT, NAME, POPULATION,
  ROW_NUMBER() OVER(PARTITION BY CONTINENT ORDER BY POPULATION DESC) AS RANK
FROM WORLD)

SELECT NAME, CONTINENT, POPULATION
FROM WORLD 
WHERE CONTINENT IN (SELECT CONTINENT
FROM MY_TABLE
WHERE RANK = 1 AND POPULATION <= 25000000)

Blockquote

下面的代码也适用于我:

select name, continent, population
from world
where continent in
(
select continent
from world x
where 25000000 > ALL
(select population from world y
where x.continent = y.continent
and population >0)
)
SELECT name
      ,continent
      ,population
FROM world
WHERE continent IN
    (
    SELECT continent AS f_country
    FROM world
    WHERE population <= 25000000
    GROUP BY continent
    HAVING COUNT(name) IN
        (
        SELECT COUNT(name) AS t_country 
        FROM world 
        GROUP BY continent
        )
    );