简体   繁体   中英

Noob Concern: Sub-query calculations

I've got a table bbc with the following columns:

name (refers to a name of a country within a particular region of the world)

region (continent of the world)

population (population of the country in the name name field) The question I'm trying to answer:

The question is as follows:
"Some countries have populations more than three times that of any of their neighbours (in the same region). Give the countries and regions."

I was thinking the answer might be something like:

SELECT a.name, a.region FROM bbc AS a
WHERE a.region IN
     (
        SELECT b.region FROM bbc AS b 
        GROUP By b.region 
        HAVING MIN(b.population) < 3*b.population)

But honestly, I lose it at that last line... I have no idea how I would find counteries that have more than three times that of any of their neighbours in the same region! Quite tough. O_o

Any and all help would be appreciated.

select
   a.name, a.region
from bbc as a
where 
    a.population >
    (
        select 3*max(b.population)
        from bbc as b
        where b.region = a.region and b.name <> a.name
    )
Select
  a.name,
  a.region
From
  bbc as a
Where
  Not Exists (
    Select
      'x'
    From
      bbc as b
    Where
      a.region = b.region And
      a.name != b.name And
      a.population < 3 * b.population
  )
SELECT name, continent from world x
WHERE (SELECT population from world y
          where y.name = x.name 
           and y.continent = x.continent
           and population > 0) > ALL 
(SELECT 3*population from world z 
          where z.continent = x.continent 
             and z.name != x.name
          and population > 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM