¿Cómo puedo reescribir esta consulta para que sea más legible?
Frecuentes
Visto 50 equipos
0
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?
3 Respuestas
0
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
0
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
0
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 mysql sql or haz tu propia pregunta.
Sadikhasan formatted your query and bam, it's more readable. Awesome, don't you think? - fancyPants
code formatting, the key to everything. - BeNdErR
What you want Query formatting or Query reducing? - Sadikhasan
@Sadikhasan Query reducing. - Kshitiz Sharma