SQL ์‹œ๋ฆฌ์ฆˆ (5์žฅ) ๐Ÿชด

14680 ๋‹จ์–ด TILsqlDatabaseDatabase

<SQL ์ฒซ๊ฑธ์Œ> ์„ ๋ณด๊ณ  ์ง์ ‘ ํ•™์Šตํ•˜๊ณ  ๊ธฐ๋กํ•˜๋Š” ํฌ์ŠคํŠธ! ๐Ÿ”ฅ

DATABASE ๋งˆ์Šคํ„ฐ๋ฅผ ๊ฟˆ๊พธ๋ฉฐ...๐Ÿ”ฅ

๐Ÿ’š 5์žฅ ์ง‘๊ณ„์™€ ์„œ๋ธŒ์ฟผ๋ฆฌ

๐Ÿ’š 20๊ฐ• COUNT๋กœ ํ–‰ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ

  • ์‹ค์ œ ์—ฐ์Šต์„ ์œ„ํ•ด ์ง€๋‚œ ํ”„๋กœ์ ํŠธ์˜€๋˜ hoxylush DB๋ฅผ ๊ฐ€์ง€๊ณ  ์—ฐ์Šต! ๐Ÿฆ–
  • ๋Œ€ํ‘œ์ ์ธ ์ง‘๊ณ„ํ•จ์ˆ˜๋Š” 5๊ฐœ! COUNT, SUM, AVG, MIN, MAX
  • SQL์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ผ ๋ถˆ๋ฆฌ๋Š” '๋ฐ์ดํ„ฐ ์ง‘ํ•ฉ'์„ ๋‹ค๋ฃจ๋Š” ์–ธ์–ด. ์ด ๊ฐ™์€ ์ง‘ํ•ฉ์˜ ๊ฐœ์ˆ˜๋‚˜ ํ•ฉ๊ณ„๊ฐ€ ๊ถ๊ธˆํ•˜๋‹ค๋ฉด SQL์ด ์ œ๊ณตํ•˜๋Š” ์ง‘๊ณ„ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ์”€!
  1. COUNT๋กœ ํ–‰ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
  • SQL์€ ์ง‘ํ•ฉ์„ ๋‹ค๋ฃจ๋Š” ์ง‘๊ณ„ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•จ! -> ์ธ์ˆ˜๋กœ '์ง‘ํ•ฉ'์„ ์ง€์ •ํ•จ!
๐Ÿ’š select count(*) from locations; ๐Ÿ’š


-> 16๊ฐœ์˜ ํ–‰ ๋ฐ˜ํ™˜

  • ์ง‘๊ณ„ํ•จ์ˆ˜์˜ ํŠน์ง•์€ ๋ณต์ˆ˜์˜ ๊ฐ’(์ง‘ํ•ฉ)์—์„œ ํ•˜๋‚˜์˜ ๊ฐ’์„ ๊ณ„์‚ฐํ•ด๋‚ด๋Š” ๊ฒƒ.
  • where ๊ตฌ ์ง€์ •ํ•˜๊ธฐ
๐Ÿ’š select count(*) from locations where name LIKE ='%์—ญ%'; ๐Ÿ’š
# locations ํ…Œ์ด๋ธ”์—์„œ '์—ญ'์ด ๋“ค์–ด๊ฐ€์žˆ๋Š” ํ–‰๋“ค๋งŒ ์ถ”์ถœํ•ด์„œ ๋ช‡๊ฐœ์ธ์ง€ ์„ธ์–ด๋ณด๊ธฐ! 
# ์•ž์—์„œ like ๋ฐฐ์šด๊ฑฐ ์‘์šฉํ–ˆ๋Š”๋ฐ ใ… ใ…  ๋Œ€๋ฐ•์“ฐ ์ง„์งœ ๋‚˜์˜ด..

  1. ์ง‘๊ณ„ํ•จ์ˆ˜์™€ NULL ๊ฐ’
  • COUNT์˜ ์ธ์ˆ˜๋กœ ์—ด๋ช…์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Œ. ์—ด๋ช…์„ ์ง€์ •ํ•˜๋ฉด ๊ทธ ์—ด์— ํ•œํ•ด์„œ ํ–‰์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Œ! ํŠนํžˆ, '*'๋ฅผ ์ธ์ˆ˜๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ COUNT ํ•จ์ˆ˜ ๋ฟ!
  • ์—ฌ๊ธฐ์„œ! ๐Ÿ‘ŠNULL๊ฐ’์„ ์–ด๋–ป๊ฒŒ ์ทจ๊ธ‰ํ• ๊ฒƒ์ธ๊ฐ€!!!!๐Ÿ‘Š En general, SQL์—์„œ๋Š” NULL๊ฐ’์„ ๊ณ ๋ คํ•ด์•ผํ•จ. ์ง‘๊ณ„ํ•จ์ˆ˜๋Š” ์ง‘ํ•ฉ ์•ˆ์— NULL๊ฐ’์ด ์žˆ์„ ๊ฒฝ์šฐ ์ด๋ฅผ ์ œ์™ธํ•˜๊ณ  ์ฒ˜๋ฆฌํ•จ.
  • NULL์ด ์žˆ๋Š” ์—ด๋ช…์„ ์–ธ๊ธ‰ํ•ด์„œ count๋ช…๋ น์–ด๋กœ ๋ถˆ๋Ÿฌ๋‚ด๋ฉด NULL๊ฐ’์€ ์ œ์™ธํ•˜๊ณ  ๊ณ„์‚ฐ๋จ. ํ•˜์ง€๋งŒ count(*)์˜ ๊ฒฝ์šฐ ๋ชจ๋“  ์—ด์˜ ํ–‰์ˆ˜๋ฅผ ์นด์šดํŠธํ•˜๊ธฐ ๋•Œ๋ฌธ์— NULL๊ฐ’์€ ์…ˆ์— ๋“ค์–ด๊ฐ„๋‹ค.


  • ์™œ๋ƒ๋ฉด phone_number์—๋Š” null๊ฐ’์ด 19๊ฐœ๋‚˜ ๋˜๊ธฐ๋•Œ๋ฌธ์— ์ด๊ฑธ ๋‹ค ์ œํ•˜๊ณ  8๊ฐœ๋งŒ ์นด์šดํŠธ๋ผ์„œ ๋‚˜์˜ค๋Š” ๊ฒƒ!
  1. DISTINCT๋กœ ์ค‘๋ณต์ œ๊ฑฐ
  • ์ง‘ํ•ฉ ์•ˆ์— ์ค‘๋ณต๋œ ๊ฐ’์ด ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๊ฐ€ ๋ฌธ์ œ๋  ๋•Œ๋„ ์žˆ์Œ!

    -> ์ด๋Ÿฐ์‹์œผ๋กœ ์„ ๋ฆ‰์—ญ์ด ๋‘๊ฐœ๊ฐ€ ์ค‘๋ณต๋จ! ๊ทธ๋Ÿด ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด DISTINCT!
  • DISTINCT๋Š” ์˜ˆ์•ฝ์–ด๋กœ ์—ด๋ช…์ด ์•„๋‹˜. select๊ตฌ์—์„œ distinct๋ฅผ ์ง€์ •ํ•˜๋ฉด ์ค‘๋ณต๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ์™ธํ•œ ๊ฒฐ๊ณผ๋ฅผ ํด๋ผ์ด์–ธํŠธ๋กœ ๋ฐ˜ํ™˜. ์ค‘๋ณต ์—ฌ๋ถ€๋Š” select๊ตฌ์— ์ง€์ •๋œ ๋ชจ๋“  ์—ด์„ ๋น„๊ตํ•ด ํŒ๋‹จ!
๐Ÿ’š select all name from locations; ๐Ÿ’š
๐Ÿ’š select distinct name from locations; ๐Ÿ’š

์‹ ๊ธฐํ•ด...! ๐Ÿฆ–

  1. ์ง‘๊ณ„ํ•จ์ˆ˜์—์„œ DISTINCT
  • SQL์—์„œ๋Š” NULL์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ๋ฌด์—‡๋ณด๋‹ค ์ค‘์š”!
  • ๊ทธ๋ ‡๋‹ค๋ฉด !!! ์—ด์—์„œ NULL๊ฐ’๋„ ์ œ์™ธํ•˜๊ณ , ์ค‘๋ณตํ•˜์ง€์•Š๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌํ•  ์ˆ˜๋Š” ์—†์„๊นŒ?
    -> ์šฐ๋ฆฌ๋Š” ์ง‘๊ณ„ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋กœ DISTINCT๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด!
๐Ÿ’š select count(all phone_number), count(distinct phone_number) from users; ๐Ÿ’š
# count all๋กœ phone_number์˜ null๊ฐ’ ์ œ๊ฑฐ,
# distinct์—์„œ phone_number์˜ ์ค‘๋ณต๊ฐ’ ์ œ๊ฑฐ + null ์ œ๊ฑฐ!

  • ๋‹ค์‹œ ๋งํ•ด count(all...)์€ null๊ฐ’๋งŒ ์ œ๊ฑฐํ•ด์ฃผ๊ณ , count(distinct...)๋Š” ์ค‘๋ณต๊ฐ’์ œ๊ฑฐ+null์ œ๊ฑฐ๋ผ๋Š” ๊ฒƒ !

๐Ÿ’š 21๊ฐ• COUNT ์ด์™ธ์˜ ์ง‘๊ณ„ํ•จ์ˆ˜

  • ์ง‘๊ณ„ํ•จ์ˆ˜๋Š” COUNT๋งŒ ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ์•ผ! SUM ์ง‘๊ณ„ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ์ง‘ํ•ฉ์˜ ํ•ฉ๊ณ„์น˜๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์”€!
  • ์ตœ์†Ÿ๊ฐ’, ์ตœ๋Œ“๊ฐ’์„ ์ฐพ๋Š” ๊ฒฝ์šฐ์—๋„ ์ง‘๊ณ„ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋œธ!
  1. SUM์œผ๋กœ ํ•ฉ๊ณ„ ๊ตฌํ•˜๊ธฐ
๐Ÿ’š select sum*age) from owners; ๐Ÿ’š

  • SUM ์ง‘๊ณ„ํ•จ์ˆ˜์— ์ง€์ •๋˜๋Š” ์ง‘ํ•ฉ์€ ์ˆ˜์น˜ํ˜• ๋ฟ! ๋ฌธ์ž์—ดํ˜•์ด๋‚˜ ๋‚ ์งœ์‹œ๊ฐ„ํ˜•์˜ ์ง‘ํ•ฉ์—์„œ๋Š” ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•  ์ˆ˜ ์—†์จ...
  • SUM ์ง‘๊ณ„ํ•จ์ˆ˜๋„ COUNT์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ NULL๊ฐ’์„ ๋ฌด์‹œํ•จ. NULL๊ฐ’์„ ์ œ๊ฑฐํ•œ ๋’ค ํ•ฉ๊ณ„๋ฅผ ๋ƒ„.
  1. AVG๋กœ ํ‰๊ท ๋‚ด๊ธฐ
  • ์•„๊นŒ ์œ„์—์„œ ๊ตฌํ•œ SUM์„ ๊ฐœ์ˆ˜๋กœ ๋‚˜๋ˆ„๋ฉด ํ‰๊ท ! -> SUM / COUNT
  • ํ•˜์ง€๋งŒ ๊ตณ์ด, sum, count ์“ฐ์ง€ ์•Š๋”๋ผ๋„ AVG๋ผ๋Š” ์ง‘๊ณ„ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํ‰๊ท ๊ฐ’์„ ๊ตฌํ•  ์ˆ˜ ์žˆ์Œ!
๐Ÿ’š select avg(age), sum(age)/count(age) from owners; ๐Ÿ’š

  • ๊ทผ๋ฐ ๊ฐ„๋‹จํ•˜๊ฒŒ ์š”๋ ‡๊ฒŒ๋งŒํ•ด๋„ ๋‚˜์˜ด!
  • AVG ์ง‘๊ณ„ํ•จ์ˆ˜๋„ NULL๊ฐ’์€ ๋ฌด์‹œ. ์ฆ‰, NULL๊ฐ’์„ ์ œ๊ฑฐํ•œ ๋’ค ํ‰๊ท ๊ฐ’์„ ๊ณ„์‚ฐ.
  • NULL์„ 0์œผ๋กœ ๊ฐ„์ฃผํ•ด์„œ ํ‰๊ท ์„ ๋‚ด๊ณ  ์‹ถ๋‹ค๋ฉด case๋ฅผ ์‚ฌ์šฉํ•ด์„œ NULL์„ 0์œผ๋กœ ๋ณ€ํ•œํ•œ ๋’ค AVG ์‚ฌ์šฉํ•˜๊ธฐ!
  1. MIN, MAX๋กœ ์ตœ์†Ÿ๊ฐ’, ์ตœ๋Œ“๊ฐ’ ๊ตฌํ•˜๊ธฐ
  • MIN, MAX๋„ ์ง‘๊ณ„ํ•จ์ˆ˜! ์ด๋“ค ํ•จ์ˆ˜๋Š” ๋ฌธ์ž์—ดํ˜•๊ณผ ๋‚ ์งœ์‹œ๊ฐ„ํ˜•์—๋„ ์‚ฌ์šฉ๊ฐ€๋Šฅ!
  • NULL๊ฐ’์„ ๋ฌด์‹œํ•˜๋Š” ๊ธฐ๋ณธ๊ทœ์น™์€ ๋‹ค๋ฅธ ์ง‘๊ณ„ํ•จ์ˆ˜์™€ ๊ฐ™์Œ!
๐Ÿ’š select min(running_time), max(running_time), min(release_date), max(release_date) from movies; ๐Ÿ’š

  • ์‹ ๊ธฐํ•˜๋‹ค! ์ง„์งœ ๋‚ ์งœ์‹œ๊ฐ„ํ˜•์—๋„ ์ตœ์†Ÿ๊ฐ’, ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค๋‹ˆ ๐Ÿฆ–

๐Ÿ’š 22๊ฐ• ๊ทธ๋ฃนํ™” - GROUP BY

  • GROUP BY๋ฅผ ์‚ฌ์šฉํ•ด ๊ทธ๋ฃนํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋Š” ์‹œ๊ฐ„ ๐ŸŒฑ
๐Ÿ’š select * from ํ…Œ์ด๋ธ”๋ช… group by ์—ด1, ์—ด2, ... ๐Ÿ’š
  1. group by๋กœ ๊ทธ๋ฃนํ™”
  • ๊ทธ๋ฃน์œผ๋กœ ๋‚˜๋ˆŒ ๋•Œ GROUP BY ๊ตฌ๋ฅผ ์‚ฌ์šฉ!
  • tickets table๋กœ group์„ ๋‚˜๋ˆ ๋ด์•ผ์ง€!
๐Ÿ’š select seat_remain from tickets group by seat_remain; ๐Ÿ’š

  • group by ๊ตฌ์— ์—ด์„ ์ง€์ •ํ•˜์—ฌ ๊ทธ๋ฃนํ™”ํ•˜๋ฉด ์ง€์ •๋œ ์—ด์˜ ๊ฐ’์ด ๊ฐ™์€ ํ–‰์ด ํ•˜๋‚˜์˜ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์ž„! select ๊ตฌ์—์„œ seat_remain ์—ด์„ ์ง€์ •ํ–ˆ์œผ๋ฏ€๋กœ ๊ทธ๋ฃนํ™”๋œ seat_remain ์—ด์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฒƒ!

  • group by ๋ฅผ ์ง€์ •ํ•ด ๊ทธ๋ฃนํ™”ํ•˜๋ฉด distinct์™€ ๊ฐ™์ด ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๋Š” ํšจ๊ณผ๊ฐ€ ์žˆ์”€!

  • ๐Ÿ˜‡ ๊ทธ๋ ‡๋‹ค๋ฉด! distinct๋กœ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ๊ณผ group by๋กœ ๊ทธ๋ฃนํ™”ํ•˜๋Š” ๊ฒƒ์€ ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์žˆ์„๊นŒ? ๐Ÿ˜‡
    -> group by ๊ตฌ๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์ง‘๊ณ„ํ•จ์ˆ˜์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ๋ณ„ ์˜๋ฏธ๊ฐ€ ์—†์Šด.
    -> group by ๊ตฌ๋กœ ๊ทธ๋ฃนํ™”๋œ ๊ฐ๊ฐ์˜ ๊ทธ๋ฃน์ด ํ•˜๋‚˜์˜ ์ง‘ํ•ฉ์œผ๋กœ์„œ ์ง‘๊ณ„ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋กœ ๋„˜๊ฒจ์ง€๊ธฐ ๋•Œ๋ฌธ.

  • group by๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ?
    -> ์ ํฌ์˜ ์ผ๋ณ„ ๋งค์ถœ ๋ฐ์ดํ„ฐ๊ฐ€ ์ค‘์•™ ํŒ๋งค ๊ด€๋ฆฌ์‹œ์Šคํ…œ์— ์ „์†ก๋˜์–ด ์ ํฌ๋ณ„ ๋งค์ถœ์‹ค์ ์„ ์ง‘๊ณ„ํ•ด ์–ด๋–ค ์ ํฌ๊ฐ€ ๋งค์ถœ์ด ์˜ฌ๋ผ๊ฐ€๋Š”์ง€, ์–ด๋–ค ์ƒํ’ˆ์ด ์ธ๊ธฐ๊ฐ€ ์žˆ๋Š”์ง€ ๋“ฑ์„ ๋ถ„์„ํ•  ๋•Œ.
    -> ์—ฌ๊ธฐ์„œ ์ ํฌ๋ณ„, ์ƒํ’ˆ๋ณ„, ์›”๋ณ„, ์ผ๋ณ„ ๋“ฑ ํŠน์ • ๋‹จ์œ„๋กœ ์ง‘๊ณ„ํ•  ๋•Œ group by๋ฅผ ์ž์ฃผ ์‚ฌ์šฉ.

  1. HAVING ๊ตฌ๋กœ ์กฐ๊ฑด ์ง€์ •
  • ์ง‘๊ณ„ํ•จ์ˆ˜๋Š” where ๊ตฌ์˜ ์กฐ๊ฑด์‹์—์„œ๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ.
  • seat_remain ์—ด์„ ๊ทธ๋ฃนํ™”ํ•˜์—ฌ ํ–‰ ๊ฐœ์ˆ˜๊ฐ€ ํ•˜๋‚˜๋งŒ ์กด์žฌํ•˜๋Š” ๊ทธ๋ฃน์„ ๊ฒ€์ƒ‰ํ•˜๊ณ  ์‹ถ์—ˆ์ง€๋งŒ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ! ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ ์ด์œ ๋Š”? group by์˜ where๊ตฌ์˜ ๋‚ด๋ถ€์ฒ˜๋ฆฌ ์ˆœ์„œ์™€ ๊ด€๊ณ„๊ฐ€ ์žˆ์Œ!
  • ์ฆ‰, where ๊ตฌ๋กœ ํ–‰์„ ๊ฒ€์ƒ‰ํ•˜๋Š” ์ฒ˜๋ฆฌ๊ฐ€ group by๋กœ ๊ทธ๋ฃนํ™”ํ•˜๋Š” ์ฒ˜๋ฆฌ๋ณด๋‹ค ์ˆœ์„œ์ƒ ์•ž์„œ๊ธฐ ๋•Œ๋ฌธ! (select ๊ตฌ์—์„œ ์ง€์ •ํ•œ ๋ณ„๋ช…์„ where ๊ตฌ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์—ˆ๋˜ ๊ฒƒ๊ณผ ๊ฐ™์€ ์ด์œ ๋กœ ๊ทธ๋ฃนํ™”๊ฐ€ ํ•„์š”ํ•œ ์ง‘๊ณ„ํ•จ์ˆ˜๋Š” where ๊ตฌ์—์„œ ์ง€์ •ํ•  ์ˆ˜ ์—†์Œ!)

๐Ÿค ๋‚ด๋ถ€์ฒ˜๋ฆฌ์ˆœ์„œ: where๊ตฌ -> group by ๊ตฌ -> select ๊ตฌ -> order by ๊ตฌ

  • where ๊ตฌ์—์„œ๋Š” ์ง‘๊ณ„ํ•จ์ˆ˜(count, sum, avg, min, max)๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ!
  • ๊ทธ๋Ÿผ ์ง‘๊ณ„ํ•œ ๊ฒฐ๊ณผ์—์„œ ์กฐ๊ฑด์— ๋งž๋Š” ๊ฐ’์„ ๋”ฐ๋กœ ๊ฑธ๋Ÿฌ๋‚ด๋ ค๋ฉด? ๐ŸŒฑ HAVING ๊ตฌ๋ฅผ ํ™œ์šฉ! ๐ŸŒฑ
  • Having๊ตฌ๋Š” group by ๊ตฌ ๋’ค์— ๊ธฐ์ˆ , where ๊ตฌ์™€ ๋™์ผํ•˜๊ฒŒ ์กฐ๊ฑด์‹์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Œ.
  • ์กฐ๊ฑด์‹์—๋Š” ๊ทธ๋ฃน๋ณ„๋กœ ์ง‘๊ณ„๋œ ์—ด์˜ ๊ฐ’์ด๋‚˜ ์ง‘๊ณ„ํ•จ์ˆ˜์˜ ๊ณ„์‚ฐ๊ฒฐ๊ณผ๊ฐ€ ์ „๋‹ฌ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋จ.
  • ์œ„์— where๊ตฌ์— ๋„ฃ์–ด์ฃผ์—ˆ๋˜ ์กฐ๊ฑด์‹(์—๋Ÿฌ๋‚œ ๊ฒƒ)์„ having ๊ตฌ๋ฅผ ํ†ตํ•ด ๋ฐ”๊ฟ”๋ณด๊ธฐ!
    -> having ๊ตฌ์— ์ง‘๊ณ„ํ•จ์ˆ˜๋ฅผ ๋„ฃ์–ด์คŒ์œผ๋กœ์จ ์›ํ•˜๋Š” ๊ฐ’์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Œ!

๐Ÿค having ๊ตฌ์˜ ๋‚ด๋ถ€์ฒ˜๋ฆฌ ์ˆœ์„œ: where ๊ตฌ -> group by ๊ตฌ -> having ๊ตฌ -> select ๊ตฌ -> order by ๊ตฌ

  • select ๊ตฌ๋ณด๋‹ค ๋จผ์ € ์ฒ˜๋ฆฌ๋˜๋ฏ€๋กœ having๊ตฌ์—์„œ ๋ณ„๋ช… ์‚ฌ์šฉ ๋ถˆ๊ฐ€.
  • ์˜ˆ๋ฅผ ๋“ค์–ด, count์— total์ด๋ผ๋Š” ๋ณ„๋ช…์„ ๋ถ™์ด๋ฉด, order by๊ตฌ์—์„œ๋Š” ์‚ฌ์šฉ๊ฐ€๋Šฅ, ํ•˜์ง€๋งŒ group by๊ตฌ๋‚˜ having ๊ตฌ์—์„œ๋Š” ์‚ฌ์šฉ ๋ถˆ๊ฐ€.
  1. ๋ณต์ˆ˜์—ด์˜ ๊ทธ๋ฃนํ™”
  • group by ์‚ฌ์šฉํ•  ๋•Œ ์ฃผ์˜ํ•  ์ ! -> group by์— ์ง€์ •ํ•œ ์—ด ์ด์™ธ์˜ ์—ด์€ ์ง‘๊ณ„ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ์ฑ„ select ๊ตฌ์— ๊ธฐ์ˆ ํ•ด์„œ๋Š” ์•ˆ๋จ!
  • ๋ฌด์Šจ๋ง์ด๋ƒ๋ฉด~
![](https://media.vlpt.us/images/majaeh43/post/024b6d02-563a-4cae-896d-7f299a0501ed/image.png)
  • group by์—์„œ ์ง€์ •ํ•œ ์—ด์€ seat_remain์ด๊ณ , ๊ทธ ๋ฐ–์— ๋‚˜๋จธ์ง€ ์—ด(price, departure_time์—ด)์€ ์ง‘๊ณ„ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜์œผ๋‹ˆ select ๊ตฌ์— ๊ธฐ์ˆ ํ•ด์„œ๋Š” ์•ˆ๋œ๋‹ค๋Š” ๋ง !
  • ์š”๋ ‡๊ฒŒ ์ง‘๊ณ„ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ง‘ํ•ฉ์€ ํ•˜๋‚˜์˜ ๊ฐ’์œผ๋กœ ๊ณ„์‚ฐ๋˜๋ฏ€๋กœ ๊ทธ๋ฃน๋งˆ๋‹ค ํ•˜๋‚˜์˜ ํ–‰์„ ์ถœ๋ ฅ ๊ฐ€๋Šฅ ! ์ด๋Ÿฐ์‹์˜ ์ฟผ๋ฆฌ๋กœ๋Š” ๋ฌธ์ œ ์—†์ด ์ถœ๋ ฅ ๊ฐ€๋Šฅ!
    -> group by ์—์„œ ์ง€์ •ํ•œ ์—ด ์ด์™ธ์˜ ์—ด์€ ์ง‘๊ณ„ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ์ฑ„ select ๊ตฌ์— ์ง€์ •ํ•  ์ˆ˜ ์—†์Œ!
  • ์ด๋ ‡๊ฒŒ ๋ฏธ๋ฆฌ group by์—์„œ ์ง€์ •ํ•œ ์—ด์ธ ๊ฒฝ์šฐ์—๋Š” select ๊ตฌ์—์„œ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ!
  1. ๊ฒฐ๊ด๊ฐ’ ์ •๋ ฌ
  • group by๋กœ ๊ทธ๋ฃนํ™”ํ•ด๋„ ์‹คํ–‰๊ฒฐ๊ณผ ์ˆœ์„œ๋ฅผ ์ •๋ ฌํ•  ์ˆ˜๋Š” ์—†๋‹น! -> ์ด๋Ÿด ๋•Œ๋Š” order by ๊ตฌ๋ฅผ ์‚ฌ์šฉํ•ด ๊ฒฐ๊ณผ๋ฅผ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ์Œ! group by ๊ตฌ๋กœ ๊ทธ๋ฃนํ™”ํ•œ ๊ฒฝ์šฐ์—๋„ order by ๊ตฌ๋ฅผ ์‚ฌ์šฉํ•ด ์ •๋ ฌํ•  ์ˆ˜ ์žˆ์Œ!

    -> ์š”๋ ‡๊ฒŒ sum(price)๋กœ ์ •๋ ฌํ•ด์ฃผ๋‹ˆ ๊น”๋” ๋”ฐ๋กœ desc ์•ˆํ•ด์ฃผ๋ฉด ๊ธฐ๋ณธ๊ฐ’์€ asc!

๐Ÿ’š 23๊ฐ• ์„œ๋ธŒ์ฟผ๋ฆฌ

  • ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” select ๋ช…๋ น์— ์˜ํ•œ ๋ฐ์ดํ„ฐ ์งˆ์˜๋กœ, ์ƒ๋ถ€๊ฐ€ ์•„๋‹Œ ํ•˜๋ถ€์˜ ๋ถ€์ˆ˜์ ์ธ ์งˆ์˜๋ฅผ ์˜๋ฏธํ•จ. (๋ฌด์Šจ์†Œ๋ฆฌ์ผ๊นŒใ…...? ๐Ÿ˜‡)
  • ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” SQL ๋ช…๋ น๋ฌธ ์•ˆ์— ์ง€์ •ํ•˜๋Š” ํ•˜๋ถ€ select ๋ช…๋ น์œผ๋กœ ๊ด„ํ˜ธ๋กœ ๋ฌถ์–ด ์ง€์ •ํ•จ!
  • ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” SQL ๋ช…๋ น์˜ WHERE ๊ตฌ์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ! where ๊ตฌ๋Š” select, delete, update ๊ตฌ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด๋“ค ์ค‘ ์–ด๋–ค ๋ช…๋ น์—์„œ๋“  ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ!
  1. delete์˜ where๊ตฌ์—์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ
  • owners table์œผ๋กœ ์—ฐ์Šต~! ๐Ÿฆ–
  • ์ฒซ๋ฒˆ์งธ ํ–‰์„ ์‚ญ์ œํ•ด๋ณด์žฅ. ๋ˆ„๊ฐ€๋ด๋„ ์ฒซ๋ฒˆ์งธ ํ–‰์˜ age๊ฐ€ ๊ฐ€์žฅ ์ž‘๋‹ค.
๐Ÿ’š delete from owners where name = '๋ฐ•์€ํ˜œ'; ๐Ÿ’š

  • ์‚ญ์ œ ์™„๋ฃŒ ! ๊ทผ๋ฐ ์•ž์„  ๋ฐฉ๋ฒ•์ฒ˜๋Ÿผ age๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ํ–‰์ด ์–ด๋–ค ํ–‰์ธ์ง€ ์•Œ๋•Œ๋Š” ์ด๋ ‡๊ฒŒ ๋ฐ”๋กœ ์‚ญ์ œ๊ฐ€ ๊ฐ€๋Šฅํ•œ๋ฐ, ๊ทธ๋ ‡์ง€ ์•Š์„ ๋•Œ๋Š” ์–ด๋–ป๊ฒŒ ์ฐพ์•„์„œ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์„๊นŒ?? min์„ ์จ์„œ ์‚ญ์ œ๊ฐ€ ๊ฐ€๋Šฅํ•œ๋ฐ ํ•œ ๋ฒˆ ํ•ด๋ณผ๊นŒ...? ๐Ÿฆ–
  • (๋ฐฉ๊ธˆ insert into๋ฅผ ํ†ตํ•ด id=4์ธ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์ฃผ์—ˆ๋‹น!)์ด์ œ id=2์ธ ๊ฐ’(age๊ฐ€ ์ตœ์†Œ์ธ ๊ฐ’)์„ ์‚ญ์ œํ•ด๋ณผ๊นŒ?
  • ์—ฌ๊ธฐ์„œ id=2์ธ ํ–‰์€ age ๊ฐ’์ด ๊ฐ€์žฅ ์ ๋‹ค. ์ด select ๋ช…๋ น์„ delete ๋ช…๋ น์˜ where ๊ตฌ์—์„œ ์‚ฌ์šฉํ•˜๋ฉด ํ•˜๋‚˜์˜ delete ๋ช…๋ น์œผ๋กœ ์›ํ•˜๋Š” ํ–‰์„ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์Œ!
๐Ÿ’š delete from owners where age = (select min(age) from owners); ๐Ÿ’š
# (select min(age) from owners) ์ด๋ถ€๋ถ„์ด ์„œ๋ธŒ์ฟผ๋ฆฌ ํ˜•ํƒœ.

  • ์ด๋Ÿฐ ์˜ค๋ฅ˜๊ฐ€ ๋œจ๋Š”๋ฐ! ๊ทธ ์ด์œ ๋Š”? ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ๊ฐฑ์‹ ํ•  ๊ฒฝ์šฐ ๋™์ผํ•œ ํ…Œ์ด๋ธ”์„ ์„œ๋ธŒ์ฟผ๋ฆฌ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋„๋ก ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋ผ๋„ค..?^^
  • ์—๋Ÿฌ ๋ฐœ์ƒ ์•ˆ์‹œํ‚ค๊ณ  ์‹คํ–‰ํ•˜๋ ค๋ฉด ์ž„์‹œ ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค๊ณ  ์ฒ˜๋ฆฌํ•˜๋ฉด ๋œ๋‹ค๊ณ  ํ•จ !
  1. ์Šค์นผ๋ผ ๊ฐ’
  • ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๊ทธ select ๋ช…๋ น์ด ์–ด๋–ค ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š”์ง€ ์ฃผ์˜ํ•  ํ•„์š”๊ฐ€ ์žˆ์Œ!
  • ์—ฌ๋Ÿฌ๊ฐ€์ง€ ํŒจํ„ด ์ค‘์—์„œ๋„ ๋‹ค์Œ๊ณผ ๊ฐ™์€ 4๊ฐ€์ง€๊ฐ€ ์ผ๋ฐ˜์ ์ธ ์„œ๋ธŒ์ฟผ๋ฆฌ ํŒจํ„ด !
    1) ํ•˜๋‚˜์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํŒจํ„ด
๐Ÿ’š select min(running_time) from movies; ๐Ÿ’š


2) ๋ณต์ˆ˜์˜ ํ–‰์ด ๋ฐ˜ํ™˜๋˜์ง€๋งŒ ์—ด์€ ํ•˜๋‚˜์ธ ํŒจํ„ด

๐Ÿ’š select running_time from movies; ๐Ÿ’š


3) ํ•˜๋‚˜์˜ ํ–‰์ด ๋ฐ˜ํ™˜๋˜์ง€๋งŒ ์—ด์ด ๋ณต์ˆ˜์ธ ํŒจํ„ด

๐Ÿ’š select min(release_date), max(running_time) from movies; ๐Ÿ’š


4)๋ณต์ˆ˜์˜ ํ–‰, ๋ณต์ˆ˜์˜ ์—ด์ด ๋ฐ˜ํ™˜๋˜๋Š” ํŒจํ„ด

๐Ÿ’š select release_date, running_time from movies; ๐Ÿ’š

-> select ๋ช…๋ น์ด ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์„ '์Šค์นผ๋ผ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค'๊ณ  ํ•œ๋‹ค!

  • ์Šค์นผ๋ผ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” select ๋ช…๋ น์„ ํŠน๋ณ„ ์ทจ๊ธ‰ํ•˜๋Š” ์ด์œ ๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฝ๊ธฐ๋•Œ๋ฌธ!
  1. select ๊ตฌ์—์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ
  • ์œ„์—์„œ์ฒ˜๋Ÿผ where๊ตฌ์— ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•จ. ๊ทธ ๋ฐ–์—๋„ ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” select๊ตฌ, update์˜ set๊ตฌ ๋“ฑ ๋‹ค์–‘ํ•œ ๊ตฌ ์•ˆ์—์„œ ์ง€์ •๊ฐ€๋Šฅ.
  • select ๊ตฌ์—์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์ง€์ •ํ•  ๋•Œ๋Š” ์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ ํ•„์š”ํ•จ!
๐Ÿ’š select
	(select count(*) from movies) AS sql1,
    (select count(*) from actors) AS sql2; ๐Ÿ’š


-> ๊ฐ ํ…Œ์ด๋ธ”์˜ ํ–‰ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ ๊ตฌํ•ด๋ด„.

  • ๋งจ ์œ„์ชฝ select ๋ช…๋ น์—๋Š” from ๊ตฌ๊ฐ€ ์—†๋‹ค. MySQL ๋“ฑ์—์„œ๋Š” ์‹ค์ œ๋กœ FROM ๊ตฌ๋ฅผ ์ƒ๋žต๊ฐ€๋Šฅ.
  1. SET ๊ตฌ์—์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ
  • update์˜ set ๊ตฌ์—์„œ๋„ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ! ํ•˜์ง€๋งŒ ๋ณ„๋กœ ๋งŽ์ด ์“ฐ์ด์ง„ ์•Š์Œ...^^
  1. FROM ๊ตฌ์—์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ
  • FROM ๊ตฌ์— ํ…Œ์ด๋ธ” ์ง€์ •๋ง๊ณ ๋„ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์”€! ๊ด„ํ˜ธ๋กœ select ๋ช…๋ น์„ ๋ฌถ์œผ๋ฉด ๋จ!
  • select๊ตฌ๋‚˜ set๊ตฌ์—์„œ๋Š” ์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•˜์ง€๋งŒ from๊ตฌ์— ๊ธฐ์ˆ ํ•  ๊ฒฝ์šฐ์—๋Š” ์Šค์นผ๋ผ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์•„๋„ ๋จ!
๐Ÿ’š select * from (select * from movies) sq; ๐Ÿ’š

  • select ๋ช…๋ น ์•ˆ์— select ๋ช…๋ น์ด ๋“ค์–ด์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ž„. ์ด๊ฑธ '๋„ค์Šคํ‹ฐ๋“œ(nested) ๊ตฌ์กฐ', ๋˜๋Š” '์ค‘์ฒฉ๊ตฌ์กฐ'๋‚˜ '๋‚ดํฌ๊ตฌ์กฐ'๋ผ ๋ถ€๋ฆ„ใ…! sq๋Š” ํ…Œ์ด๋ธ”์˜ ๋ณ„๋ช…์ž„.
  1. insert๋ช…๋ น๊ณผ ์„œ๋ธŒ์ฟผ๋ฆฌ
  • insert๋ช…๋ น๊ณผ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์กฐํ•ฉํ•ด ์‚ฌ์šฉ๊ฐ€๋Šฅ. insert ๋ช…๋ น์—๋Š” values๊ตฌ์˜ ์ผ๋ถ€๋กœ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์™€ values๊ตฌ ๋Œ€์‹  select ๋ช…๋ น์„ ์‚ฌ์šฉํ•˜๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Œ.

๐Ÿ’š 24๊ฐ• ์ƒ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ

  • ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด delete ๋ช…๋ น๊ณผ select ๋ช…๋ น์„ ๊ฒฐํ•ฉํ•  ์ˆ˜ ์žˆ์”€! ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ์ผ์ข…์ธ '์ƒ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ'๋ฅผ exists ์ˆ ์–ด๋กœ ์กฐํ•ฉ์‹œ์ผœ์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž...!๐Ÿฆ–
  • EXISTS ์ˆ ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒฐ๊ด๊ฐ’์ด ์žˆ๋Š”์ง€๋ฅผ ์กฐ์‚ฌํ•  ์ˆ˜ ์žˆ์Œ.. EXIST๋Š” ๋‹จ์ง€ ๋ฐ˜ํ™˜๋œ ํ–‰์ด ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•ด๋ณด๊ณ  ๊ฐ’์ด ์žˆ์œผ๋ฉด ์ฐธ, ์—†์œผ๋ฉด ๊ฑฐ์ง“์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ผญ ์Šค์นผ๋ผ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ํ•„์š”๋Š” ์—†์Œ !
  1. EXISTS
  • ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด ๊ฒ€์ƒ‰ํ•  ๋•Œ '๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ์•„๋‹Œ์ง€' ํŒ๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด ์กฐ๊ฑด์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Œ !
๐Ÿ’š update movies SET title = '์žˆ์Œ' where exists (select * from actors where first_name = '์ •'); ๐Ÿ’š


-> ^^ ์™œ ์ด๋ ‡๊ฒŒ ๋˜์—ˆ์„๊นŒ....? ^^ ๋ถ„๋ช… '์ •'์ด๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์žˆ์œผ๋ฉด title์„ '์žˆ์Œ'์ด๋ผ๊ณ  ๋ฐ”๊ฟ”๋‹ฌ๋ผ๊ณ  ๋ช…๋ น ๋‚ด๋ฆฐ๊ฑด๋ฐ ๋‹ค ๋ฐ”๋€Œ์–ด๋ฒ„๋ ธ๋„ค...?^^
-> actors ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ์—ด ์ค‘์—์„œ, first_name์ด '์ •'์ธ ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•˜๋ฉด, movies ํ…Œ์ด๋ธ”์˜ title์„ '์žˆ์Œ'์ด๋ผ๊ณ  ๋ฐ”๊ฟ”์ค˜.... ์–ด๋””์„œ ์˜ค๋ฅ˜๊ฐ€ ๋‚ฌ์„๊นŒ... ใ… ใ…  ๋ณด๋ฅ˜....!

  1. NOT EXISTS
  • '์—†์Œ'์˜ ๊ฒฝ์šฐ, ํ–‰์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ƒํƒœ๊ฐ€ ์ฐธ์ด ๋˜๋ฏ€๋กœ ์ด๋•Œ๋Š” NOT EXISTS๋ฅผ ์‚ฌ์šฉ.

    -> '์ •'์ด ์—†๊ธฐ๋•Œ๋ฌธ์— ์–ด๋–ค ๋ณ€ํ™”๋„ ์ผ์–ด๋‚˜์ง€ ์•Š์•˜๋‹ค๊ณ  ํ•œ๋‹ค...
  1. ์ƒ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ
  • ์„œ๋ธŒ์ฟผ๋ฆฌ์—๋Š” ๋ช…๋ น ์•ˆ์— ์ค‘์ฒฉ๊ตฌ์กฐ๋กœ ๋œ select ๋ช…๋ น์ด ์กด์žฌํ•จ.
๐Ÿ’š update movies SET title = '์žˆ์Œ' where exists (select * from actors where first_name = '์ •'); ๐Ÿ’š
  • ์—ฌ๊ธฐ์„œ ๋ณด๋‹ค์‹œํ”ผ first_name = '์ •'์—์„œ actor ํ…Œ์ด๋ธ”์˜ first_name ์—ด๊ฐ’์ด ๋ถ€๋ชจ์˜ '์ •' ์—ด๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋Š” ํ–‰์„ ๊ฒ€์ƒ‰ํ•˜๋Š” ๊ฒƒ์ž„. ์ด๋ ‡๊ฒŒ ๋ถ€๋ชจ ๋ช…๋ น๊ณผ ์ž์‹์ธ ์„œ๋ธŒ ์ฟผ๋ฆฌ๊ฐ€ ํŠน์ • ๊ด€๊ณ„๋ฅผ ๋งบ๋Š” ๊ฒƒ์„ '์ƒ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ'๋ผ ๋ถ€๋ฆ„

  • ํ…Œ์ด๋ธ”๋ช… ๋ถ™์ด๊ธฐ
    ๊ทธ๋ ‡๋‹ค๋ฉด, ๋งŒ์•ฝ ๋‘ ์—ด์ด ๋ชจ๋‘ ๊ฐ™์€ ์ด๋ฆ„์„ ๊ฐ€์ง„๋‹ค๋ฉด? where first_name = first_name์ด๋ผ๋ฉด?
    -> ๊ทธ๋Ÿฌ๋ฉด ์ž˜ ๋™์ž‘ํ•˜์ง€ ์•Š์„ ๊ฒƒ์ž„. ๊ทธ๋ž˜์„œ ์—ฌ๊ธฐ์„œ๋Š” ์—ด์ด ์–ด๋Š ํ…Œ์ด๋ธ”์˜ ๊ฒƒ์ธ์ง€ ๋ช…์‹œ์ ์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ํ•„์š”๊ฐ€ ์žˆ์Œ! ์—ด๋ช… ์•ž์— โฃ๏ธ'ํ…Œ์ด๋ธ”๋ช….'โฃ๏ธ์„ ๋ถ™์ด๊ธฐ๋งŒ ํ•˜๋ฉด ๋จ!

  1. IN
  • ์Šค์นผ๋ผ ๊ฐ’๋ผ๋ฆฌ ๋น„๊ตํ•  ๋•Œ๋Š” '=' ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉ. ๋‹ค๋งŒ ์ง‘ํ•ฉ์„ ๋น„๊ตํ•  ๋•Œ๋Š” ์‚ฌ์šฉ ๋ชปํ•ด!
  • IN์„ ์‚ฌ์šฉํ•˜๋ฉด ์ง‘ํ•ฉ ์•ˆ์˜ ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ์กฐ์‚ฌํ•  ์ˆ˜ ์žˆ์Œ!
๐Ÿ’š ์—ด๋ช… IN (์ง‘ํ•ฉ) ๐Ÿ’š
๐Ÿ’š where no = 3 or no = 5; ๐Ÿ’š

  • IN๊ณผ NULL
    ์ง‘๊ณ„ํ•จ์ˆ˜์—์„œ๋Š” ์ง‘ํ•ฉ ์•ˆ์˜ NULL ๊ฐ’์„ ๋ฌด์‹œํ•˜๊ณ  ์ฒ˜๋ฆฌํ–ˆ์Œ. ํ•˜์ง€๋งŒ IN์—์„œ๋Š” ์ง‘ํ•ฉ์•ˆ์— NULL๊ฐ’์„ ๋ฌด์‹œํ•˜์ง€ ์•Š์Œ~! ์ค‘์š”ํ•œ๊ฑด NULL์„ ๋น„๊ตํ•˜๋ ค๋ฉด IS NULL์„ ์‚ฌ์šฉํ•ด์•ผํ•จ! ๋˜ํ•œ, NOT IN์˜ ๊ฒฝ์šฐ, ์ง‘ํ•ฉ ์•ˆ์— NULL๊ฐ’์ด ์žˆ์œผ๋ฉด ์™ผ์ชฝ ๊ฐ’์ด ์ง‘ํ•ฉ ์•ˆ์— ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š์•„๋„ ์ฐธ์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์Œ!

๐Ÿฉ 5์žฅ ์ง‘๊ณ„ ํ•จ์ˆ˜๊นŒ์ง€๋Š” ๊ดœ์ฐฎ์•˜๋Š”๋ฐ ์•ผ๊ธˆ์•ผ๊ธˆ ์–ด๋ ต๊ณ  ํ—ท๊ฐˆ๋ฆฌ๋Š” ๊ฒƒ๋“ค์ด ์ƒ๊ธด๋‹ค. distinct ๋ผ๋˜๊ฐ€ having...^^ ์Šค์นผ๋ผ ๋“ค์–ด๊ฐ€๋ฉด์„œ๋ถ€ํ„ฐ ๋„ˆ๋ฎค ๋ฉ˜๋ถ•.. ์—ฐ์Šต ๋” ํ•ด์•ผ์ง€ ใ… ใ…  ์†์— ์ต์„ ๋•Œ๊นŒ์ง€! ํ™”์ด๋ต...! ๐Ÿฉ

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ