1 ๋ถ„ ์†Œ์š”

๊ธฐ์ดˆ SQL์— ๋Œ€ํ•ด์„œโ€ฆ

1. SQL ์ด๋ž€?

  • SQL (Structured Query Language)

    โ€˜๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ฆฌ ์‹œ์Šคํ…œ (RDBMS)โ€™ ์—์„œ ์ž๋ฃŒ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ์šฉ๋„๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ตฌ์กฐ์  ๋ฐ์ดํ„ฐ ์งˆ์˜ ์–ธ์–ด

2. ๋ช…๋ น์–ด์˜ ์ข…๋ฅ˜

  • ๋ฐ์ดํ„ฐ ์ •์˜ ์–ธ์–ด (DDL : Data Definition Language)

    CREATE, DROP, ALTER โ€ฆ

  • ๋ฐ์ดํ„ฐ ์กฐ์ž‘ ์–ธ์–ด (DML : Data Manipulation Language)

    INSERT, UPDATE, DELETE, SELECT โ€ฆ

  • ๋ฐ์ดํ„ฐ ์ œ์–ด ์–ธ์–ด (DCL : Data Control Language)

    BEGIN, COMMIT, ROLLBACK โ€ฆ


2-1. SELECT - ๋ฐ์ดํ„ฐ ์กฐํšŒ

select [ all | distinct ] ์†์„ฑ๋ช… from ํ…Œ์ด๋ธ”

all : ์ „์ฒด ์ถœ๋ ฅ, ๊ธฐ๋ณธ ๊ฐ’
distinct : ์ค‘๋ณต ์ œ๊ฑฐ

2-2. WHERE - ์กฐ๊ฑด๋ฌธ

where ์กฐ๊ฑด์‹

  • WHERE๋ฌธ์˜ ์กฐ๊ฑด์‹ ์†์„ฑ๋ช… ์—ฐ์‚ฐ์ž ๊ฐ’

    ex) price >= 10000 and price <= 20000
    10000 <= price <= 20000 ์ธ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ
    ex) publisher = โ€˜๊ตฟ์Šคํฌ์ธ โ€™ or publisher = โ€˜๋Œ€ํ•œ๋ฏธ๋””์–ดโ€™
    publisher๊ฐ€ โ€˜๊ตฟ์Šคํฌ์ธ โ€™ ํ˜น์€ โ€˜๋Œ€ํ•œ๋ฏธ๋””์–ดโ€™์ธ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ

2-3. GROUP BY - ๊ทธ๋ฃนํ™”

group by ์†์„ฑ๋ช…

ex) select custid, count(*) as โ€˜์ฃผ๋ฌธ ๊ฐœ์ˆ˜โ€™, from orders group by custid;

  • custid๋ฅผ โ€˜์ฃผ๋ฌธ ๊ฐœ์ˆ˜โ€™์— ๋Œ€ํ•ด์„œ ๊ทธ๋ฃนํ™”
  • โ€˜asโ€™๋Š” ๋ณ„์นญ(alias)์œผ๋กœ, ์ƒ๋žต์ด ๊ฐ€๋Šฅ -> count(*) โ€˜์ฃผ๋ฌธ ๊ฐœ์ˆ˜โ€™์™€ ๋™์ผ
  • ํ†ต๊ณ„์— ๋งŽ์ด ์“ฐ์ž„

2-4. HAVING

having ์กฐ๊ฑด์‹

  • having์€ ๋ณดํ†ต group by์™€ ๊ฐ™์ด ์“ฐ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Œ
  • ์ฆ‰, ํ†ต๊ณ„ํ•จ์ˆ˜(์ง‘๊ณ„ํ•จ์ˆ˜)๋ฅผ ํ†ตํ•œ ์กฐ๊ฑด์‹์ด ๋“ค์–ด๊ฐ

2-5. ORDER BY - ์ •๋ ฌ

order by ์†์„ฑ๋ช… asc|desc

asc : ์˜ค๋ฆ„์ฐจ์ˆœ (์ž‘์€ ๊ฐ’ -> ํฐ ๊ฐ’) -> ๊ธฐ๋ณธ ๊ฐ’
desc : ๋‚ด๋ฆผ์ฐจ์ˆœ (ํฐ ๊ฐ’ -> ์ž‘์€ ๊ฐ’)

2-6. LIMIT

limit ์‹œ์ž‘์œ„์น˜, ์ถ”์ถœํ•  ๊ฐœ์ˆ˜

ex) select * from book limit 0, 3;
index๋ฒˆํ˜ธ 1๋ฒˆ ~ 3๋ฒˆ๊นŒ์ง€์˜ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ


3. SQL ์—ฐ์‚ฐ์ž


|์—ฐ์‚ฐ์ž|์„ค๋ช…| |โ€”|โ€”| |=|๊ฐ™์Œ| |<> ๋˜๋Š” !=|๊ฐ™์ง€ ์•Š์Œ| |>|์ดˆ๊ณผ| |<|๋ฏธ๋งŒ| |>=|์ด์ƒ| |<=|์ดํ•˜| |BETWEEN|์ผ์ • ๋ฒ”์œ„ ์‚ฌ์ด| |LIKE|ํŒจํ„ด ๊ฒ€์ƒ‰| |IN|์ปฌ๋Ÿผ์˜ ์—ฌ๋Ÿฌ ๊ฐ€๋Šฅํ•œ ๊ฐ’๋“ค ์ง€์ •|


ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ