# ¿Cómo puedo reescribir esta consulta para que sea más legible?

Tengo la siguiente tabla:

Find each country that belongs to a continent where all populations are less than 25000000.

``````SELECT name,
continent,
population
FROM world
WHERE continent IN
(SELECT continent
FROM world t
WHERE 25000000 > ALL
(SELECT population
FROM world
WHERE continent = t.continent))
``````

Can I write this in a better way using a keyword or function that wouldn't require so many levels of nesting?

preguntado el 12 de junio de 14 a las 11:06

code formatting, the key to everything. -

What you want Query formatting or Query reducing? -

## 3 Respuestas

You are looking for continents where all countries have a population less then 25,000,000. So group by continents and keep those where even the maximum population doesn't exceed this amount.

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

Respondido el 12 de junio de 14 a las 11:06

``````SELECT w1.name,w1.continent,w1.population
FROM world w1
INNER JOIN
(
SELECT continent, SUM(population) as continent_population
FROM world
GROUP BY continent
) w2 ON w2.continent = w1.continent
WHERE w2.continent_population < 25000000
``````

.. o ..

``````SELECT name,continent,population
FROM world w1
WHERE
EXISTS
(
SELECT 1
FROM world w2
WHERE w2.continent = w1.continent
GROUP BY continent
HAVING SUM(population) < 25000000
)
``````

Respondido el 12 de junio de 14 a las 11:06

Requisito:

Find each country that belongs to a continent where all populations are less than 25000000.

La forma más sencilla que se me ocurre:

``````SELECT name, continent, population FROM world
WHERE continent in (
SELECT continent FROM world
GROUP BY continent
HAVING SUM(population) < 25000000)
``````

Respondido el 12 de junio de 14 a las 11:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.