【问题标题】:SQL combined SELECT statementSQL组合SELECT语句
【发布时间】:2015-03-08 17:06:16
【问题描述】:

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

问题是

找出所有国家人口

我为产生解决方案所做的工作

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 查询比我想象的要多)。

感谢任何建议/解释。

【问题讨论】:

    标签: sql subquery


    【解决方案1】:

    以下代码对我有用:

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

    【讨论】:

      【解决方案2】:
      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 
         );
      

      【讨论】:

        【解决方案3】:
        SELECT name, continent, population 
        FROM world x 
        WHERE 25000000 > ALL(SELECT population 
                             FROM world y 
                             WHERE y.continent = x.continent 
                             )
        

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

        【讨论】:

          【解决方案4】:

          我已经编写 SQL 很长时间了,几乎从不使用 ALLSOMEANY

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

          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;
          

          【讨论】:

            【解决方案5】:

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

            来了:

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

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

            【讨论】:

              【解决方案6】:

              这是一种方式:

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

              列表由子查询确定。

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

              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 Server 中工作:

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

              【讨论】:

              • 再想你实际上不需要distinct,因为每个国家都有一行。有多种方法可以解决这个问题,您可以像其他答案中所建议的那样轻松 - 或者实际上更容易 - 使用 max
              • 我认为 minus 在 oracle 中工作,而不是在 SQL Server 中。
              【解决方案7】:

              我认为这将是一个简单的...

              从大陆不在的世界中选择名称,大陆,人口( 从人口 >= 25000000 的世界中选择大陆)

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

              【讨论】:

                【解决方案8】:

                这确实是一种简单且有效的方法

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

                【讨论】:

                  【解决方案9】:

                  选择名称、大陆、人口 来自世界 WHERE 大陆不在(从世界人口 > 25000000 中选择大陆)

                  【讨论】:

                  • 感谢您提供第一个答案,但是,如果有,请考虑解释它与现有答案的不同之处,并尝试解释代码的作用以及一般规则,所以它是每次都清楚它是如何解决问题的。
                  【解决方案10】:

                  以下查询对我有用

                  从大陆不在的世界中选择名称、大陆、人口 ( 从世界中选择大陆 其中人口 >= 25000000)

                  【讨论】:

                    【解决方案11】:

                    我认为这应该会有所帮助:

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

                    【讨论】:

                      【解决方案12】:

                      这也适用于“ALL”语法:

                      SELECT name, continent, population
                      FROM world w1
                      WHERE population <= ALL(SELECT continent
                                              FROM world w2
                                              WHERE population > 25000000 
                                              AND w1.continent = w2.continent) 
                      

                      【讨论】:

                        【解决方案13】:
                        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))
                        

                        【讨论】:

                        • 不鼓励仅使用代码回答。请添加一些解释,说明这如何解决问题,或者这与现有答案有何不同。 From Review
                        【解决方案14】:

                        我想出了这个

                        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
                        )
                        

                        【讨论】:

                          【解决方案15】:
                          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
                          

                          【讨论】:

                            【解决方案16】:

                            这是我使用并正确的代码

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

                            【讨论】:

                              【解决方案17】:

                              我是这样解决的:

                              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)
                              

                              块引用

                              【讨论】:

                                【解决方案18】:

                                下面的代码也适用于我:

                                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)
                                )
                                

                                【讨论】:

                                  猜你喜欢
                                  • 2018-02-16
                                  • 1970-01-01
                                  • 2013-05-31
                                  • 2011-10-11
                                  • 2017-07-14
                                  • 2010-11-24
                                  • 1970-01-01
                                  • 1970-01-01
                                  • 1970-01-01
                                  相关资源
                                  最近更新 更多