๐Ÿ“š Computer Science/CS

[Database] SQL ๋ฌธ ์ •๋ฆฌ(์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ๋Œ€๋น„)

ibelieveinme 2021. 7. 20. 02:16
728x90

1. SELECT(๊ฒ€์ƒ‰)

 

1) ํ…Œ์ด๋ธ”์˜ ํŠน์ • ์นผ๋Ÿผ์„ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ

SELECT ์นผ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ”๋ช…;

์—ฌ๋Ÿฌ ๊ฐœ์˜ ์นผ๋Ÿผ์„ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ๋Š” ์ฝค๋งˆ(,)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

SELECT ์นผ๋Ÿผ๋ช…1, ์นผ๋Ÿผ๋ช…2, ์นผ๋Ÿผ๋ช…3 FROM ํ…Œ์ด๋ธ”๋ช…;

๋ชจ๋“  ์นผ๋Ÿผ์€ * ๊ธฐํ˜ธ ์‚ฌ์šฉ

SELECT * FROM ํ…Œ์ด๋ธ”๋ช…;

AS ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ณ„๋ช…(์ƒˆ๋กœ์šด ์ด๋ฆ„)์„ ๋ถ™์ผ ์ˆ˜ ์žˆ๋‹ค.

SELECT ์นผ๋Ÿผ1 AS ๋ณ„๋ช…1 FROM ํ…Œ์ด๋ธ”๋ช…;

์ค‘๋ณต์„ ์ œ์™ธํ•˜๊ณ  ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ์„ ๋•Œ๋Š” DISTINCT ์—ฐ์‚ฐ์ž๋ฅผ ์“ด๋‹ค. ๋ฐ์ดํ„ฐ๋Š” ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ๋‹ค.

SELECT DISTINCT ์นผ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ”๋ช…;

 

2) ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์นผ๋Ÿผ์„ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ

SELECT ์นผ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ”๋ช… WHERE ์นผ๋Ÿผ๋ช… = ๊ฐ’; /*์กฐ๊ฑด๋น„๊ต๋Š” =, !=, <, >, <=, >=, IS NULL ๋“ฑ์ด ์žˆ์Œ*/
SELECT ์นผ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ”๋ช… WHERE ์นผ๋Ÿผ๋ช… LIKE ํŒจํ„ด; /*ํŒจํ„ด๊ธฐํ˜ธ๋Š” %(๋ชจ๋“ ๋ฌธ์ž), _(์ž๋ฆฌ์ˆ˜)๊ฐ€ ์žˆ๋‹ค.*/

์กฐ๊ฑด์ด ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๋•Œ๋Š” AND, OR ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

SELECT ์นผ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ”๋ช… WHERE ์นผ๋Ÿผ๋ช…1 = ๊ฐ’1 AND ์นผ๋Ÿผ๋ช…2 = ๊ฐ’2; /*์กฐ๊ฑด์ด ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๊ฒฝ์šฐ AND, OR ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ*/

 

3) ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ ฌํ•˜์—ฌ ๋ถˆ๋Ÿฌ์˜ค๊ณ  ์‹ถ์„ ๋•Œ

SELECT ์นผ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ”๋ช… WHERE ์นผ๋Ÿผ๋ช… = ๊ฐ’ ORDER BY ์นผ๋Ÿผ๋ช… ASC or DESC;

๋ณต์ˆ˜ ์ •๋ ฌํ•  ๋•Œ๋Š” ์ฝค๋งˆ(,)๋ฅผ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค.

SELECT ์นผ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ”๋ช… WHERE ์นผ๋Ÿผ๋ช… = ๊ฐ’ ORDER BY ์นผ๋Ÿผ๋ช…1 ASC, ์นผ๋Ÿผ๋ช…2 DESC;

 

4) ํŠน์ • ๊ฐœ์ˆ˜๊นŒ์ง€๋งŒ ๋ถˆ๋Ÿฌ์˜ค๊ณ  ์‹ถ์„ ๋•Œ

SELECT ์นผ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ”๋ช… WHERE ์นผ๋Ÿผ๋ช… = ๊ฐ’ ORDER BY ์นผ๋Ÿผ๋ช… ASC or DESC LIMIT ๊ฐœ์ˆ˜;

 

2. INSERT(์‚ฝ์ž…)

 

1) ํ…Œ์ด๋ธ”์— ์นผ๋Ÿผ๋ช…์— ๋งž๊ฒŒ ๊ฐ’์„ ์ž…๋ ฅํ•  ๋•Œ(์นผ๋Ÿผ๋ช…๊ณผ ๊ฐ’์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋™์ผํ•ด์•ผ ํ•จ)

INSERT INTO ํ…Œ์ด๋ธ”๋ช…(์นผ๋Ÿผ๋ช…1, ์นผ๋Ÿผ๋ช…2, ์นผ๋Ÿผ๋ช…2) VALUES(๊ฐ’1, ๊ฐ’2, ๊ฐ’3);

 

2) ๋ฌธ์ž๋ฅผ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ '' ์•ˆ์— ๋ฌธ์ž์—ด์„ ๋„ฃ์–ด์•ผ ํ•จ.

INSERT INTO TABLE_FRIEND(NAME, AGE, PHONE) VALUES('Mina', 20, '010-1111-1111');

 

3) ์นผ๋Ÿผ ๊ฐ’์˜ ์ˆœ์„œ์™€ ๊ฐœ์ˆ˜๊ฐ€ ๋งž๋‹ค๋ฉด ์นผ๋Ÿผ๋ช…์„ ์ž…๋ ฅํ•˜์ง€ ์•Š์•„๋„ ๊ฐ€๋Šฅ

INSERT INTO ํ…Œ์ด๋ธ”๋ช… VALUES(๊ฐ’1, ๊ฐ’2, ๊ฐ’3);

 

3. UPDATE(์ˆ˜์ •)

 

1) ํ…Œ์ด๋ธ”์— ์žˆ๋Š” ํŠน์ • ์นผ๋Ÿผ์˜ ๊ฐ’์„ ์ „๋ถ€ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์„ ๋•Œ

UPDATE ํ…Œ์ด๋ธ”๋ช… SET ์ปฌ๋Ÿผ๋ช… = ๋ณ€๊ฒฝํ•  ๊ฐ’;

 

2) ์กฐ๊ฑด์— ๋งž๋Š” ๋ฐ์ดํ„ฐ๋งŒ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์„ ๋•Œ

UPDATE ํ…Œ์ด๋ธ”๋ช… SET ์นผ๋Ÿผ๋ช… = ๋ณ€๊ฒฝํ•  ๊ฐ’ WHERE ์นผ๋Ÿผ๋ช… = ๊ฐ’;

๋ณ€๊ฒฝํ•  ์นผ๋Ÿผ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ด๋ฉด ์ฝค๋งˆ(,)๋ฅผ ์ด์šฉํ•œ๋‹ค.

UPDATE ํ…Œ์ด๋ธ”๋ช… SET ์นผ๋Ÿผ๋ช…1 = ๋ณ€๊ฒฝํ•  ๊ฐ’1, ์นผ๋Ÿผ๋ช…2 = ๋ณ€๊ฒฝํ•  ๊ฐ’2 WHERE ์นผ๋Ÿผ๋ช… = ๊ฐ’;

 

4. DELETE(์‚ญ์ œ)

 

1) ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์‚ญ์ œ

DELETE from ํ…Œ์ด๋ธ”๋ช…;

 

2) ์กฐ๊ฑด์— ๋งž๋Š” ๋ฐ์ดํ„ฐ๋งŒ ์‚ญ์ œ

DELETE from ํ…Œ์ด๋ธ”๋ช… WHERE ์นผ๋Ÿผ๋ช… = ๊ฐ’;

[์ถ”๊ฐ€ ๋‚ด์šฉ]

 

1. SQL ํ•จ์ˆ˜

1) ํŠน์ • ์นผ๋Ÿผ์—์„œ ์ตœ๋Œ€๊ฐ’ ๊ตฌํ•˜๊ธฐ(์นผ๋Ÿผ๋ช…์ด MAX(์นผ๋Ÿผ๋ช…)์œผ๋กœ ๋‚˜์˜ด)

SELECT MAX(์นผ๋Ÿผ๋ช…) FROM ํ…Œ์ด๋ธ”๋ช…;

 

2) ํŠน์ • ์นผ๋Ÿผ์—์„œ ์ตœ์†Œ๊ฐ’ ๊ตฌํ•˜๊ธฐ(์นผ๋Ÿผ๋ช…์ด MIN(์นผ๋Ÿผ๋ช…)์œผ๋กœ ๋‚˜์˜ด)

SELECT MIN(์นผ๋Ÿผ๋ช…) FROM ํ…Œ์ด๋ธ”๋ช…;

 

3) ์นผ๋Ÿผ์˜ ํ•ฉ ๊ตฌํ•˜๊ธฐ(์นผ๋Ÿผ๋ช…์ด SUM(์นผ๋Ÿผ๋ช…)์œผ๋กœ ๋‚˜์˜ด)

SELECT SUM(์นผ๋Ÿผ๋ช…) FROM ํ…Œ์ด๋ธ”๋ช…;

๊ทธ ์™ธ ํ‰๊ท ์„ ๊ตฌํ•˜๋Š” AVG(์นผ๋Ÿผ๋ช…), ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” COUNT(์นผ๋Ÿผ๋ช…),

        ๋Œ€๋ฌธ์ž๋กœ ๋ฐ”๊พธ๋Š” UPPER(์นผ๋Ÿผ๋ช…), ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พธ๋Š” LOWER(์นผ๋Ÿผ๋ช…) ๋„ ์žˆ์Œ!!

 

4) ๊ทธ๋ฃน๋ณ„ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ

: ํŠน์ • ์ปฌ๋Ÿผ์„ ๊ทธ๋ฃนํ™” ํ•˜๋Š” GROUP BY ํ‚ค์›Œ๋“œ์™€ ํŠน์ • ์นผ๋Ÿผ์„ ๊ทธ๋ฃนํ™”ํ•œ ๊ฒฐ๊ณผ์— ์กฐ๊ฑด์„ ๊ฑฐ๋Š” HAVING ํ‚ค์›Œ๋“œ ์‚ฌ์šฉ

SELECT ์ปฌ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ”๋ช… GROUP BY ๊ทธ๋ฃนํ™”ํ•  ์ปฌ๋Ÿผ๋ช… HAVING ์กฐ๊ฑด์‹;

 

2. JOIN

๋‘ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์˜ ํ–‰์„ ๊ฒฐํ•ฉํ•ด์„œ ํ•˜๋‚˜์˜ ํ–‰์ธ ๊ฒƒ์ฒ˜๋Ÿผ ์‚ฌ์šฉ

SELECT ํ…Œ์ด๋ธ” ๋ณ„๋ช…1.์นผ๋Ÿผ์ด๋ฆ„1, ์กฐ์ธํ…Œ์ด๋ธ” ๋ณ„๋ช…2.์นผ๋Ÿผ์ด๋ฆ„2
FROM ํ…Œ์ด๋ธ”๋ช… (AS) ํ…Œ์ด๋ธ” ๋ณ„๋ช… JOIN ์กฐ์ธํ…Œ์ด๋ธ”๋ช… (AS) ์กฐ์ธํ…Œ์ด๋ธ” ๋ณ„๋ช…
ON(WHERE) ํ…Œ์ด๋ธ” ๋ณ„๋ช….๊ธฐ์ค€ํ‚ค = ํ…Œ์ด๋ธ” ๋ณ„๋ช….๊ธฐ์ค€ํ‚ค....

JOIN ํ‚ค์›Œ๋“œ ๋Œ€์‹  ์ฝค๋งˆ(,)๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋จ

SELECT ํ…Œ์ด๋ธ” ๋ณ„๋ช…1.์นผ๋Ÿผ์ด๋ฆ„1, ์กฐ์ธํ…Œ์ด๋ธ” ๋ณ„๋ช…2.์นผ๋Ÿผ์ด๋ฆ„2
FROM ํ…Œ์ด๋ธ”๋ช… (AS) ํ…Œ์ด๋ธ” ๋ณ„๋ช…, ์กฐ์ธํ…Œ์ด๋ธ”๋ช… (AS) ์กฐ์ธํ…Œ์ด๋ธ” ๋ณ„๋ช…
ON(WHERE) ํ…Œ์ด๋ธ” ๋ณ„๋ช….๊ธฐ์ค€ํ‚ค = ํ…Œ์ด๋ธ” ๋ณ„๋ช….๊ธฐ์ค€ํ‚ค....

 

1) INNER JOIN: ๊ต์ง‘ํ•ฉ. ์ผ๋ฐ˜์ ์ธ JOIN์€ INNER JOIN์„ ๋งํ•œ๋‹ค. EQUAL JOIN์ด๋ผ ๋ถ€๋ฅด๊ธฐ๋„ ํ•จ

SELECT ํ…Œ์ด๋ธ”๋ณ„๋ช….์นผ๋Ÿผ์ด๋ฆ„ as ์ƒˆ๋กœ์ง€์„ ์นผ๋Ÿผ๋ช…, ์กฐ์ธํ…Œ์ด๋ธ”๋ณ„๋ช….์นผ๋Ÿผ์ด๋ฆ„ as ์ƒˆ๋กœ์ง€์„ ์นผ๋Ÿผ๋ช…
FROM ํ…Œ์ด๋ธ”๋ช… ํ…Œ์ด๋ธ”๋ณ„๋ช… INNER JOIN ์กฐ์ธํ…Œ์ด๋ธ”๋ช… ์กฐ์ธํ…Œ์ด๋ธ”๋ณ„๋ช…
ON(WHERE) ํ…Œ์ด๋ธ”๋ณ„๋ช….๊ธฐ์ค€ํ‚ค = ์กฐ์ธํ…Œ์ด๋ธ”๋ณ„๋ช….๊ธฐ์ค€ํ‚ค....

 

2) LEFT/RIGHT JOIN:  ๋ถ€๋ถ„์ง‘ํ•ฉ

  2)-1 LEFT JOIN: JOIN ๊ธฐ์ค€ ์™ผ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋‘ ํฌํ•จํ•œ๋‹ค.

SELECT ํ…Œ์ด๋ธ”๋ช….์นผ๋Ÿผ์ด๋ฆ„, ์กฐ์ธํ…Œ์ด๋ธ”๋ช….์นผ๋Ÿผ์ด๋ฆ„
FROM ํ…Œ์ด๋ธ”๋ช… LEFT JOIN ์กฐ์ธํ…Œ์ด๋ธ”๋ช…
ON(WHERE) ํ…Œ์ด๋ธ”๋ช….๊ธฐ์ค€ํ‚ค = ์กฐ์ธํ…Œ์ด๋ธ”๋ช….๊ธฐ์ค€ํ‚ค....

  2)-2 RIGHT JOIN: JOIN ๊ธฐ์ค€ ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋‘ ํฌํ•จํ•œ๋‹ค.

SELECT ํ…Œ์ด๋ธ”๋ช….์นผ๋Ÿผ์ด๋ฆ„, ์กฐ์ธํ…Œ์ด๋ธ”๋ช….์นผ๋Ÿผ์ด๋ฆ„
FROM ํ…Œ์ด๋ธ”๋ช… RIGHT JOIN ์กฐ์ธํ…Œ์ด๋ธ”๋ช…
ON(WHERE) ํ…Œ์ด๋ธ”๋ช….๊ธฐ์ค€ํ‚ค = ์กฐ์ธํ…Œ์ด๋ธ”๋ช….๊ธฐ์ค€ํ‚ค....

 

3) OUTER JOIN: ํ•ฉ์ง‘ํ•ฉ

SELECT ํ…Œ์ด๋ธ”๋ช….์นผ๋Ÿผ์ด๋ฆ„, ์กฐ์ธํ…Œ์ด๋ธ”๋ช….์นผ๋Ÿผ์ด๋ฆ„
FROM ํ…Œ์ด๋ธ”๋ช… OUTER JOIN ์กฐ์ธํ…Œ์ด๋ธ”๋ช…
ON(WHERE) ํ…Œ์ด๋ธ”๋ช….๊ธฐ์ค€ํ‚ค = ์กฐ์ธํ…Œ์ด๋ธ”๋ช….๊ธฐ์ค€ํ‚ค....

 

3. UNION / UNION ALL

: ๋‘ ๊ฐœ์˜ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์„ ๊ฒฐํ•ฉํ•  ๋•Œ ์‚ฌ์šฉ(JOIN๊ณผ ์œ ์‚ฌํ•จ)

: UNION ์„ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ, ์ค‘๋ณต ๋ฐ์ดํ„ฐ๋Š” ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค.(DISTINCT์™€ ์œ ์‚ฌ)

: UNION๊ณผ UNION ALL์˜ ์ฐจ์ด๋Š” UNION ALL์€ ๋ฐ์ดํ„ฐ ๊ฐ’์ด ์ค‘๋ณต๋˜๋”๋ผ๋„ ์กฐ๊ฑด์— ์ผ์น˜ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋‘ ํ‘œ์‹œํ•œ๋‹ค๋Š” ์ !

 

* UNION ์ƒ์„ฑ ์กฐ๊ฑด

1) UNION ๋‚ด์— ๊ฐ SELECT ๋ฌธ์€ ๊ฐ™์€ ์ˆ˜์˜ ์—ด์„ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค.

2) ์—ด์€ ์œ ์‚ฌํ•œ ๋ฐ์ดํ„ฐ ํ˜•์‹์„ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค.

3) ๊ฐ SELECT ๋ฌธ์˜ ์—ด์€ ๋™์ผํ•œ ์ˆœ์„œ๋กœ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

[SQL๋ฌธ 1]
UNION
[SQL๋ฌธ 2]

์˜ˆ์‹œ) 

TABLE1

ID NAME
1 LEE
2 KIM
3 YOON

TABLE2

ID NAME
2 KIM
4 PARK

[SQL ๋ฌธ]

 

SELECT ID, NAME
FROM TABLE1;

UNION

SELECT ID, NAME
FROM TABLE2;

[๊ฒฐ๊ณผ]

ID NAME
1 KIM
2 LEE
3 YOON
4 PARK

 


[๊ด€๋ จ ๋ฌธ์ œ]

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์—†์–ด์ง„ ๊ธฐ๋ก ์ฐพ๊ธฐ | ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค (programmers.co.kr)

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์—†์–ด์ง„ ๊ธฐ๋ก ์ฐพ๊ธฐ

ANIMAL_INS ํ…Œ์ด๋ธ”์€ ๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋™๋ฌผ์˜ ์ •๋ณด๋ฅผ ๋‹ด์€ ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค. ANIMAL_INS ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ, ANIMAL_ID, ANIMAL_TYPE, DATETIME, INTAKE_CONDITION, NAME, SEX_UPON_INTAKE๋Š” ๊ฐ๊ฐ ๋™๋ฌผ์˜ ์•„์ด๋””

programmers.co.kr

[๋‹ต]

SELECT OUTS.ANIMAL_ID, OUTS.NAME 
FROM ANIMAL_OUTS OUTS LEFT OUTER JOIN ANIMAL_INS INS
ON OUTS.ANIMAL_ID = INS.ANIMAL_ID
WHERE INS.ANIMAL_ID IS NULL
ORDER BY OUTS.ANIMAL_ID ASC
728x90