๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

SQL

[SQL] MySQL - ALL ์‚ฌ์šฉ๋ฒ•

๋ฐ˜์‘ํ˜•

๐Ÿ”Š ๋ณธ ํฌ์ŠคํŒ…์—์„œ ์‚ฌ์šฉ๋˜๋Š” ํ…Œ์ด๋ธ”์˜ ์ž๋ฃŒ์™€ ์ถœ์ฒ˜๋Š” SQL Zoo์ž„์„ ๋ฐํž™๋‹ˆ๋‹ค. ๋” ๋‹ค์–‘ํ•œ SQL ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์‹œ๋ ค๋ฉด SQL Zoo ์‚ฌ์ดํŠธ๋ฅผ ๋ฐฉ๋ฌธํ•ด๋ณด์„ธ์š”!

 

SQL ๋ฌธ์ œ ํ’€์ด ์‚ฌ์ดํŠธ์ธ SQL Zoo

 

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” MySQL์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ฌธ๋ฒ• ์ค‘ ํ•˜๋‚˜์ธ 'ALL'์„ ์‚ฌ์šฉํ•˜์—ฌ SQL ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋ ค๊ณ  ํ•œ๋‹ค. 3๊ฐ€์ง€ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณผ ํ…๋ฐ, ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ œ์—์„œ ALL์— ๋Œ€ํ•œ ๋ฐฉ๋ฒ•์„ ์ตํžˆ๋ฉด 2, 3๋ฒˆ์งธ ๋ฌธ์ œ๋Š” ๊ฐ์„ ์žก๊ณ  ์†์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋‹ค.

 

์šฐ์„  ํ•ด๋‹น ๋ฌธ์ œ์™€ ๋ฌธ์ œ ํ’€ ๋•Œ ์ด์šฉํ•  ๋ฐ์ดํ„ฐ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด์ž.

 

SQL ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ์ด์šฉํ•  ํ…Œ์ด๋ธ”

 

์ฒซ ๋ฒˆ์งธ ๋ฌธ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ ๋ฌธ์ œ๊ฐ€ ์š”๊ตฌํ•˜๋Š” ์ •๋‹ต์˜ ํ…Œ์ด๋ธ” ํ˜•ํƒœ๋„ ํ•จ๊ป˜ ์‚ดํŽด๋ณด์ž.

 

List each continent and the name of the country that comes first alphabetically.
(๊ฐ ๋Œ€๋ฅ™์— ์†ํ•˜๋Š” ๋‚˜๋ผ๋“ค ์ค‘ ์•ŒํŒŒ๋ฒณ ์ˆœ์„œ๋กœ ์ •๋ ฌ ์‹œ ์ฒซ ๋ฒˆ์งธ๋กœ ์˜ค๋Š” ๋‚˜๋ผ์˜ ์ด๋ฆ„์„ ์กฐํšŒํ•˜์—ฌ ๋Œ€๋ฅ™, ๋‚˜๋ผ์ด๋ฆ„ ์ˆœ์„œ๋กœ ์ถœ๋ ฅํ•ด๋ผ.)

๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ํ…Œ์ด๋ธ” ํ˜•ํƒœ

 

์šฐ์„  ๊ณ ๋ คํ•ด์•ผ ํ•  ์ ์€ ์•ŒํŒŒ๋ฒณ ์ˆœ์„œ๋กœ ๊ฐ€์žฅ ์ฒ˜์Œ ๋“ฑ์žฅํ•˜๋Š” ์กฐ๊ฑด์„ SQL ์ˆ˜์‹์œผ๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค๋ฉด 

a๊ฐ€ ์ฒซ๊ธ€์ž์ธ ๋‚˜๋ผ < b๊ฐ€ ์ฒซ๊ธ€์ž์ธ ๋‚˜๋ผ < c๊ฐ€ ์ฒซ๊ธ€์ž์ธ ๋‚˜๋ผ < .... < z๊ฐ€ ์ฒซ๊ธ€์ž์ธ ๋‚˜๋ผ

์ด๋Ÿฐ ์‹์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ๊ฒฐ๊ตญ ์•ŒํŒŒ๋ฒณ ์ˆœ์„œ๋กœ ๊ฐ€์žฅ ์ฒ˜์Œ ๋“ฑ์žฅํ•œ๋‹ค๋Š” ๊ฒƒ์€ ALL๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•ด ๋‚˜ํƒ€๋‚ธ๋‹ค๋ฉด ์ด๋Ÿฐ ์‹์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

๋‚˜๋ผ์ด๋ฆ„ <= ALL(๋‚˜๋ผ์ด๋ฆ„)

 

๋˜ ํ•œ ๊ฐ€์ง€ ๊ณ ๋ คํ•ด์•ผ ํ•  ์ ์€ ์ด๋ ‡๊ฒŒ ์•ŒํŒŒ๋ฒณ ์ˆœ์„œ๋กœ ๊ฐ€์žฅ ์ฒ˜์Œ์˜ค๋Š” ๋‚˜๋ผ๋ฅผ '๊ฐ ๋Œ€๋ฅ™๋ณ„'๋กœ ๊ตฌํ•ด์•ผ ํ•œ๋‹ค. ์ด ๋•Œ๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ์™€ WHERE์ ˆ์„ ์‚ฌ์šฉํ•œ๋‹ค. ์šฐ์„  ์ •๋‹ต์ธ MySQL ๊ตฌ๋ฌธ์„ ์‚ดํŽด๋ณด์ž.

 

SELECT x.continent, x.name
FROM world AS x
WHERE x.name <= ALL(SELECT y.name
                    FROM world AS y
                    WHERE x.continent = y.continent)

 

ํ•„์ž๊ฐ€ ์ฒ˜์Œ ๊ฐ€์žฅ ํ—ท๊ฐˆ๋ ธ๋˜ ๋ถ€๋ถ„์€ ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ WHERE ์ ˆ ๊ตฌ๋ฌธ์ด๋‹ค. ์„œ๋ธŒ์ฟผ๋ฆฌ ์ ˆ์„ ์ง๊ด€์ ์œผ๋กœ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ์„œ๋ธŒ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ์™€ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋„์ถœํ•˜๋Š” ํ•˜๋‹จ์˜ INNER JOIN ๋ฌธ์œผ๋กœ ๋ฐ”๊พธ์–ด ๋ณด์•˜๋‹ค.

 

SELECT DISTINCT x.continent, x.name
FROM world AS x
 INNER JOIN world AS y ON x.continent = y.continent

 

์ค‘๋ณต์„ ์ œ๊ฑฐ(Distinct) ํ•˜์ง€ ์•Š์œผ๋ฉด ๋งค์šฐ ๊ธด ๊ฒฐ๊ณผ๋ฌผ์ด ์ถœ๋ ฅ๋˜๊ธฐ ๋•Œ๋ฌธ์— DISTINCT๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒƒ์ด๊ณ  ๊ฒฐ๊ณผ๋ฌผ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

INNER JOIN์œผ๋กœ ์ถœ๋ ฅํ•œ ๊ฒฐ๊ณผ๊ฐ’(๋Œ€๋ฅ™์ด ์•„ํ”„๋ฆฌ์นด์ธ ๊ฐ’๋งŒ ๋ณด์ธ๋‹ค. ํ•˜๋‹จ์€ ์ƒ๋žต๋˜์–ด ์žˆ๋‹ค)

 

์ด๋ ‡๊ฒŒ ์ •๋‹ต SQL ๊ตฌ๋ฌธ์˜ ์„œ๋ธŒ์ฟผ๋ฆฌ ์ ˆ์ด ์œ„์™€ ๊ฐ™์€ INNER JOIN ๊ตฌ๋ฌธ์„ ๋Œ€์‹  ์ˆ˜ํ–‰ํ•œ๋‹ค๊ณ  ๋ณด๋ฉด ๋œ๋‹ค. 

 

๊ทธ๋Ÿฌ๋ฉด ๋‹ค์Œ ๋‘ ๋ฒˆ์งธ ๋ฌธ์ œ๋„ ํ•ด๊ฒฐํ•ด๋ณด์ž.

 

Find the continents where all countries have a population <= 25000000. Then find the names of the countries associated with these continents. Show namecontinent and population.
((๋ชจ๋“  ๋‚˜๋ผ๋“ค์ด ์ธ๊ตฌ 2์ฒœ 5๋ฐฑ๋งŒ๋ช… ์ดํ•˜์ธ ๋Œ€๋ฅ™๋“ค์„ ์ฐพ์•„๋ผ. ๊ทธ๋ฆฌ๊ณ  ์ด ๋Œ€๋ฅ™๋“ค์— ์†ํ•˜๋Š” ๋‚˜๋ผ๋“ค์˜ ์ด๋ฆ„, ๋Œ€๋ฅ™, ์ธ๊ตฌ๋ฅผ ์ถœ๋ ฅํ•ด๋ผ)

์ •๋‹ต SQL ๊ตฌ๋ฌธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 

 

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

 

๋งˆ์ง€๋ง‰ ์„ธ ๋ฒˆ์งธ ๋ฌธ์ œ์ด๋‹ค.

 

Some countries have populations more than three times that of any of their neighbours (in the same continent). Give the countries and continents.
(์–ด๋–ค ๋‚˜๋ผ๋“ค์€ ๊ฐ™์€ ๋Œ€๋ฅ™์— ์†ํ•˜๋Š” ๋‚˜๋ผ๋“ค์˜ ์ธ๊ตฌ๋ณด๋‹ค 3๋ฐฐ ๋งŽ์€ ์ธ๊ตฌ๊ฐ€ ๊ฑฐ์ฃผํ•œ๋‹ค. ์ด ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋‚˜๋ผ๋“ค๊ณผ ๋Œ€๋ฅ™์„ ์ถœ๋ ฅํ•ด๋ผ.)

์ •๋‹ต SQL ๊ตฌ๋ฌธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

SELECT x.name, x.continent
FROM world AS x
WHERE x.population > ALL(SELECT 3 * y.population
                         FROM world AS y
                         WHERE x.continent = y.continent
                         AND x.name != y.name)

 

์œ„ ๋ฌธ์ œ์—์„œ ์ฃผ์˜ํ•  ๋ถ€๋ถ„์€ '๋น„๊ตํ•˜๋Š” ๋‚˜๋ผ ์ž๊ธฐ ์ž์‹ ' ์„ ์ œ์™ธํ•ด์ฃผ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ๋ฌธ์ œ์™€ ๋‹ฌ๋ฆฌ 

x.name != y.name

์กฐ๊ฑด์ด ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.

๋ฐ˜์‘ํ˜•