Notice
Recent Posts
Recent Comments
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
In Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

A Joyful AI Research Journey๐ŸŒณ๐Ÿ˜Š

[54] 230316 SQL Ch 5. ์ง‘๊ณ„์™€ ์„œ๋ธŒ์ฟผ๋ฆฌ: 23๊ฐ• ์„œ๋ธŒ์ฟผ๋ฆฌ, 24๊ฐ• ์ƒ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 54์ผ] ๋ณธ๋ฌธ

๐ŸŒณBootcamp Revision 2023โœจ/Python, Flask, SQL

[54] 230316 SQL Ch 5. ์ง‘๊ณ„์™€ ์„œ๋ธŒ์ฟผ๋ฆฌ: 23๊ฐ• ์„œ๋ธŒ์ฟผ๋ฆฌ, 24๊ฐ• ์ƒ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 54์ผ]

yjyuwisely 2023. 3. 16. 10:23

230316 Thu 54th class

Ch 5. ์ง‘๊ณ„์™€ ์„œ๋ธŒ์ฟผ๋ฆฌ
์ง„๋„: p.  216 ~ (๊ต์žฌ: SQL ์ฒซ๊ฑธ์Œ, ์ €์ž: ์•„์‚ฌ์ด ์•„์ธ ์‹œ)
MySQL Workbench Manual: https://dev.mysql.com/doc/workbench/en/
MySQL ํŠœํ† ๋ฆฌ์–ผ: https://www.tutorialspoint.com/mysql/

SQLD ์š”์•ฝ pdf: https://yurimac.tistory.com/40
์ฑ… ์š”์•ฝ (9p) pdf: https://brunch.co.kr/@ydy702/10

 

SQL ์ฒซ๊ฑธ์Œ | ์•„์‚ฌ์ด ์•„์ธ ์‹œ - ๊ต๋ณด๋ฌธ๊ณ 

SQL ์ฒซ๊ฑธ์Œ | ์™•์ดˆ๋ณด๋ฅผ ์œ„ํ•œ ์ •๋ง ์‰ฌ์šด SQL ์ž…๋ฌธ์„œ๋ฅผ ๋งŒ๋‚˜๋ณด์ž!๋ณธ๊ฒฉ์ ์ธ ๋น…๋ฐ์ดํ„ฐ ์‹œ๋Œ€์— ์ ‘์–ด๋“ค๋ฉด์„œ ๊ธฐ์กด ๋Œ€๋น„ ๋” ํšจ์œจ์ ์ด๊ณ  ํญ๋„“์€ ๋ฐ์ดํ„ฐ ๋ถ„์„์„ ์œ„ํ•œ ์ˆ˜์š”๊ฐ€ ์ปค์งˆ ๊ฒƒ์œผ๋กœ ๊ธฐ๋Œ€๋˜๋Š” ๊ฐ€์šด๋ฐ, SQ

product.kyobobook.co.kr

์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ ์ค‘ ๊ธฐ์–ตํ•  ๊ฒƒ์„ ์ •๋ฆฌํ–ˆ๋‹ค.


์š”์•ฝ

์„œ๋ธŒ์ฟผ๋ฆฌ (sub query)
์ฟผ๋ฆฌ ์•ˆ์— ๋˜ ๋‹ค๋ฅธ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑ

(select ๊ตฌ๋ฌธ)

์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ์ข…๋ฅ˜
1. ์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ (Scalar Subquery) p. 210
์ฃผ๋กœ SELECT ์ ˆ์— ์œ„์น˜ํ•˜์ง€๋งŒ ์ปฌ๋Ÿผ์ด ์˜ฌ ์ˆ˜ ์žˆ๋Š” ๋Œ€๋ถ€๋ถ„ ์œ„์น˜์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
์ปฌ๋Ÿผ ๋Œ€์‹  ์‚ฌ์šฉ๋˜๋ฏ€๋กœ ๋ฐ˜๋“œ์‹œ ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ์„ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค.

select (select * from ํ…Œ์ด๋ธ”๋ช…) from ํ…Œ์ด๋ธ”๋ช…

select (select * from ํ…Œ์ด๋ธ”๋ช…) from ํ…Œ์ด๋ธ”๋ช…


2. ์ค‘์ฒฉ ์„œ๋ธŒ์ฟผ๋ฆฌ (Nested Subquery) p. 206
WHERE ์ ˆ, HAVING ์ ˆ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

select * from ํ…Œ์ด๋ธ”๋ช… where ์—ด๋ช… = (select * from ํ…Œ์ด๋ธ”๋ช…)

select * from ํ…Œ์ด๋ธ”๋ช… where ์—ด๋ช… = (select * from ํ…Œ์ด๋ธ”๋ช…)

๊ต์žฌ p. 206 (MySQL ์‚ฌ์šฉ์ด๋ฏ€๋กœ ์ธ๋ผ์ธ ๋ทฐ๋กœ ์ž„์‹œ ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค์–ด์„œ ์ฒ˜๋ฆฌํ–ˆ๋‹ค.)

2.1 ๋‹จ์ผํ–‰ (Single Row) ์„œ๋ธŒ์ฟผ๋ฆฌ =
๋‹จ์ผ ํ–‰ ๋น„๊ต ์—ฐ์‚ฐ์ž: =, <, >, <> (๊ฐ™์ง€ ์•Š์Œ) ๋“ฑ
์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ 1๊ฑด ์ดํ•˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

where ์—ด๋ช… = (select * from ํ…Œ์ด๋ธ”๋ช…)

๊ฒฐ๊ณผ๊ฐ€ ํ•˜๋‚˜๋งŒ ๋‚˜์˜จ๋‹ค.


2.2 ๋‹ค์ค‘ํ–‰ (Multi Row) ์„œ๋ธŒ์ฟผ๋ฆฌ IN, ANY, ALL
๋‹ค์ค‘ ํ–‰ ๋น„๊ต ์—ฐ์‚ฐ์ž: IN, ALL, ANY, SOME, EXITS
์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฑด์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
๊ฒฐ๊ณผ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ๋‹ค.


IN:
OR (๊ฐ™์€ ๊ฒƒ์— ๋Œ€ํ•ด ๋น„๊ต)
ANY: OR (ํฌ๊ธฐ ๋น„๊ต)
ALL: AND 

IN 
: ๋ฉ”์ธ์ฟผ๋ฆฌ์˜ ๋น„๊ต์กฐ๊ฑด์ด ์„œ๋ธŒ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ์ค‘์—์„œ ํ•˜๋‚˜๋ผ๋„ ์ผ์น˜ํ•˜๋ฉด ์ฐธ
์„œ๋ธŒ์ฟผ๋ฆฌ ์ถœ๋ ฅ๊ฒฐ๊ณผ์™€ ํ•˜๋‚˜๋ผ๋„ ์ผ์น˜ํ•˜๋ฉด ์ฐธ์ด ๋œ๋‹ค.

๊ต์žฌ์˜ ์˜ˆ์‹œ
no = ์œผ๋กœ ์„ค์ •์‹œ ๊ฒฐ๊ณผ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์ด๋ฏ€๋กœ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๊ฐ€ ๋œฌ๋‹ค


ANY, SOME : ๋ฉ”์ธ์ฟผ๋ฆฌ์˜ ๋น„๊ต์กฐ๊ฑด์ด ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒ€์ƒ‰๊ฒฐ๊ณผ์™€ ํ•˜๋‚˜ ์ด์ƒ ์ผ์น˜ํ•˜๋ฉด ์ฐธ


ALL : ๋ฉ”์ธ์ฟผ๋ฆฌ์˜ ๋น„๊ต์กฐ๊ฑด์ด ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒ€์ƒ‰๊ฒฐ๊ณผ์™€ ๋ชจ๋‘ ์ผ์น˜ํ•˜๋ฉด ์ฐธ


EXIST : ๋ฉ”์ธ์ฟผ๋ฆฌ์˜ ๋น„๊ต์กฐ๊ฑด์ด ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ ์ค‘ ๋งŒ์กฑํ•˜๋Š” ๊ฐ’์ด ํ•˜๋‚˜๋ผ๋„ ์กด์žฌํ•˜๋ฉด ์ฐธ

where ์—ด๋ช… IN (select * from ํ…Œ์ด๋ธ”๋ช…)

์ฐธ๊ณ : https://mainichibenkyo.tistory.com/75

์ฐธ๊ณ 

2.3 ๋‹ค์ค‘ ์ปฌ๋Ÿผ (Multi Column) ์„œ๋ธŒ์ฟผ๋ฆฌ
์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ ์—ฌ๋Ÿฌ ์ปฌ๋Ÿผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ 


3. ์ธ๋ผ์ธ ๋ทฐ (Inline View) (*asํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ€์ƒ์˜ ํ…Œ์ด๋ธ”๋ช…์„ ๋ฐ˜๋“œ์‹œ ๊ผญ ์„ ์–ธํ•ด์•ผ ํ•œ๋‹ค.*) p. 212
FROM ์ ˆ ๋“ฑ ํ…Œ์ด๋ธ”๋ช…์ด ์˜ฌ ์ˆ˜ ์žˆ๋Š” ์œ„์น˜์— ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. 

select * from (select * from ํ…Œ์ด๋ธ”๋ช…) as ๊ฐ€์ƒํ…Œ์ด๋ธ”๋ช…

select * from (select * from ํ…Œ์ด๋ธ”๋ช…) as ๊ฐ€์ƒํ…Œ์ด๋ธ”๋ช…


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

์ฑ…์˜ ๋ชฉ์ฐจ
__20๊ฐ•__ํ–‰ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ - COUNT
1. COUNT๋กœ ํ–‰ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
2. ์ง‘๊ณ„ํ•จ์ˆ˜์™€ NULL๊ฐ’
3. DISCOUNT๋กœ ์ค‘๋ณต ์ œ๊ฑฐ
4. ์ง‘๊ณ„ํ•จ์ˆ˜์—์„œ DISTINCT

__21๊ฐ•__COUNT ์ด์™ธ์˜ ์ง‘๊ณ„ํ•จ์ˆ˜
1. SUM์œผ๋กœ ํ•ฉ๊ณ„ ๊ตฌํ•˜๊ธฐ
2. AVG๋กœ ํ‰๊ท ๋‚ด๊ธฐ
3. MIN, MAX๋กœ ์ตœ์†Ÿ๊ฐ’, ์ตœ๋Œ“๊ฐ’ ๊ตฌํ•˜๊ธฐ

__22๊ฐ•__๊ทธ๋ฃนํ™” - GROUP BY
1. GROUP BY๋กœ ๊ทธ๋ฃนํ™”
2. HAVING ๊ตฌ๋กœ ์กฐ๊ฑด ์ง€์ •
3. ๋ณต์ˆ˜์—ด์˜ ๊ทธ๋ฃนํ™”
4. ๊ฒฐ๊ด๊ฐ’ ์ •๋ ฌ

__23๊ฐ•__์„œ๋ธŒ์ฟผ๋ฆฌ
1. DELETE์˜ WHERE ๊ตฌ์—์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ
2. ์Šค์นผ๋ผ ๊ฐ’
3. SELECT ๊ตฌ์—์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ
4. SET ๊ตฌ์—์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ
5. FROM ๊ตฌ์—์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ
6. INSERT ๋ช…๋ น๊ณผ ์„œ๋ธŒ์ฟผ๋ฆฌ

__24๊ฐ•__์ƒ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ
1. EXISTS
2. NOT EXISTS
3. ์ƒ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ
4. IN

23๊ฐ• ์„œ๋ธŒ์ฟผ๋ฆฌ p. 204

์„œ๋ธŒ์ฟผ๋ฆฌ (sub query)
์ฟผ๋ฆฌ ์•ˆ์— ๋˜ ๋‹ค๋ฅธ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑ

(select ๊ตฌ๋ฌธ)

์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ์ข…๋ฅ˜
1. ์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ p. 210
select (select * from ํ…Œ์ด๋ธ”๋ช…) from ํ…Œ์ด๋ธ”๋ช…

2. ์ค‘์ฒฉ ์„œ๋ธŒ์ฟผ๋ฆฌ p. 206
select * from ํ…Œ์ด๋ธ”๋ช… where ์—ด๋ช… = (select * from ํ…Œ์ด๋ธ”๋ช…)

3. ์ธ๋ผ์ธ๋ทฐ (asํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ€์ƒ์˜ ํ…Œ์ด๋ธ”๋ช…์„ ๋ฐ˜๋“œ์‹œ ๊ผญ ์„ ์–ธํ•ด์•ผ ํ•œ๋‹ค.) p. 212
select * from (select * from ํ…Œ์ด๋ธ”๋ช…) as ๊ฐ€์ƒํ…Œ์ด๋ธ”๋ช…

์„œ๋ธŒ์ฟผ๋ฆฌ

(SELECT ๋ช…๋ น)

์„œ๋ธŒ ์ฟผ๋ฆฌ: SQL ํ•˜๋‚˜์˜ ๋ฌธ์•ˆ์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋˜ ๋‹ค๋ฅธ SQL๋ฌธ, ์•Œ๋ ค์ง€์ง€ ์•Š์€ ๊ธฐ์ค€์„ ์ด์šฉํ•œ ๊ฒ€์ƒ‰์— ์‚ฌ์šฉ.
- ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ์„œ ์‚ฌ์šฉํ•œ๋‹ค.
- ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ๋‹จ์ผ ํ–‰ ๋˜๋Š” ๋ณต์ˆ˜ ํ–‰ ๋น„๊ต ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค ๋‹จ์ผ ํ–‰ ๋น„๊ต ์—ฐ์‚ฐ์ž๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐ˜๋“œ์‹œ 1๊ฑด ์ดํ•˜์—ฌ์•ผ ํ•˜๊ณ  ๋ณต์ˆ˜ ํ–‰ ๋น„๊ต ์—ฐ์‚ฐ์ž๋Š” ๊ฒฐ๊ณผ ๊ฑด์ˆ˜์™€ ์ƒ๊ด€์—†๋‹ค.
- ORDER BY ์„œ๋ธŒ์ฟผ๋ฆฌ์—์„œ๋Š” ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•œ๋‹ค.
- SELECT, FROM, WHERE, HAVING, ORDER BY, INSERT-VALUES, UPDATE-SET ์ ˆ์— ์‚ฌ์šฉ ๊ฐ€๋Šฅ


23.1 DELETE์˜ WHERE ๊ตฌ์—์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ

์˜ˆ์ œ 5-16) sample54 ํ…Œ์ด๋ธ”

SELECT * FROM sample54;


์˜ˆ์ œ 5-17) sample54์—์„œ a์˜ ์ตœ์†Ÿ๊ฐ’ ๊ฒ€์ƒ‰ํ•˜๊ธฐ

SELECT MIN(a) FROM sample54;


์˜ˆ์ œ 5-18) ์ตœ์†Ÿ๊ฐ’์„ ๊ฐ€์ง€๋Š” ํ–‰ ์‚ญ์ œํ•˜๊ธฐ p. 206
[2. ์„œ๋ธŒ์ฟผ๋ฆฌ]

-- sample54 ํ…Œ์ด๋ธ”์—์„œ a์—ด์˜ ๊ฐ’์ด 20์ธ ๋ฐ์ดํ„ฐ์˜ ํ–‰์„ ์‚ญ์ œํ•˜๋ผ
delete from sample54 where a = 20;

-- sample54 ํ…Œ์ด๋ธ”์—์„œ a์—ด์˜ ๊ฐ’์ด ์ตœ์†Œ๊ฐ’์ธ ๋ฐ์ดํ„ฐ์˜ ํ–‰์„ ์‚ญ์ œํ•˜๋ผ
-- delete from sample54 where a์—ด์˜ ๊ฐ’์ด ์ตœ์†Œ๊ฐ’
-- sample54ํ…Œ์ด๋ธ”์—์„œ a์—ด์˜ ๊ฐ’์ด ์ตœ์†Œ๊ฐ’
select min(a) from sample54;

-- ๊ด„ํ˜ธ๋กœ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์ง€์ •ํ•ด ์‚ญ์ œ
DELETE FROM sample54 WHERE a = (SELECT MIN(a) FROM sample54);

SELECT * FROM sample54;

MySQL์—์„œ๋Š” ์œ„ ์ฝ”๋“œ๊ฐ€ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๋ฏ€๋กœ ์•„๋ž˜ ์ฝ”๋“œ๋กœ ์‹คํ–‰์ด ๋œ๋‹ค.
๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ๊ฐฑ์‹ ํ•  ๊ฒฝ์šฐ ๋™์ผํ•œ ํ…Œ์ด๋ธ”์„ ์„œ๋ธŒ์ฟผ๋ฆฌ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋„๋ก ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

ํ•ด๊ฒฐ๋ฒ• 1) ์ธ๋ผ์ธ ๋ทฐ๋กœ ์ž„์‹œ ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค๋„๋ก ์ฒ˜๋ฆฌํ•œ๋‹ค.

-- ์ธ๋ผ์ธ๋ทฐ (asํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ€์ƒ์˜ ํ…Œ์ด๋ธ”๋ช…์„ ๋ฐ˜๋“œ์‹œ ๊ผญ ์„ ์–ธํ•ด์•ผ ํ•œ๋‹ค.)
select *
from (select * from ํ…Œ์ด๋ธ”๋ช…) as ๊ฐ€์ƒํ…Œ์ด๋ธ”๋ช…
DELETE FROM sample54 WHERE a = (SELECT a 
	FROM (SELECT Min(a) AS a FROM sample54) AS x);
    
-- (SELECT Min(a) AS a FROM sample54): ์ธ๋ผ์ธ ๋ทฐ


ํ•ด๊ฒฐ๋ฒ• 2) 
COLUMN: ํด๋ผ์ด์–ธํŠธ ๋ณ€์ˆ˜
MySQL ํด๋ผ์ด์–ธํŠธ์— ํ•œํ•ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

@a: ๋ณ€์ˆ˜, set: ๋ณ€์ˆ˜์— ๋Œ€์ž…ํ•˜๋Š” ๋ช…๋ น

set @a = (SELECT MIN(a) FROM sample54);
DELETE FROM sample54 WHERE a = @a;


23.2 ์Šค์นผ๋ผ ๊ฐ’ p. 207

์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ: ํ•œ ํ–‰, ํ•œ ์นผ๋Ÿผ๋งŒ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ

์˜ˆ์ œ 5-19) ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ํŒจํ„ด
ํŒจํ„ด 1: ํ•˜๋‚˜์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํŒจํ„ด = ์Šค์นผ๋ผ ๊ฐ’

SELECT MIN(a) FROM sample54;


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

SELECT no FROM sample54;

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

SELECT MIN(a), Max(no) FROM sample54;

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

SELECT no, a FROM sample54;


Point: SELECT ๋ช…๋ น์ด ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์„ '์Šค์นผ๋ผ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค'๊ณ  ํ•œ๋‹ค.

Point: = ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋น„๊ตํ•  ๊ฒฝ์šฐ์—๋Š” ์Šค์นผ๋ผ ๊ฐ’๋ผ๋ฆฌ ๋น„๊ตํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค.


23.3 SELECT ๊ตฌ์—์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ

์˜ˆ์ œ 5-20์˜ SELECT ๊ตฌ์—์„œ ์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด๋ณธ๋‹ค.

์˜ˆ์ œ 5-20) SELECT ๊ตฌ์—์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ p. 210
[1. ์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ]

SELECT
    (SELECT COUNT(*) FROM sample51) AS sql1,
    (SELECT COUNT(*) FROM sample54) AS sql2;

DBMS๋Š” ์—‘์„ค๊ณผ ๋‹ค๋ฅด๊ฒŒ ๊ฐ™์€ ์ค„์— ๋‹ค ํ‘œํ˜„์ด ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค.

ํ•œ ์ค„์— ํ•œ ๊ฐ’๋งŒ ๋“ค์–ด๊ฐ„๋‹ค = ์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ

์˜ˆ์ œ 5-21) SELECT ๊ตฌ์—์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ (Oracle์˜ ๊ฒฝ์šฐ)

SELECT
    (SELECT COUNT(*) FROM sample51) AS sql1,
    (SELECT COUNT(*) FROM sample54) AS sql2 FROM DUAL;

23.4 SET ๊ตฌ์—์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ

์˜ˆ์ œ 5-22) SET ๊ตฌ์—์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ
MySQL์—์„œ๋Š” ์•ˆ ๋œ๋‹ค.

UPDATE sample54 SET a = (SELECT MAX(a) FROM sample54);

์ˆ˜์ •๋œ ์ฝ”๋“œ)

update sample54
set a = (select a from (select max(a) as a from sample54) as x);
SELECT * FROM sample54;


23.5 FROM ๊ตฌ์—์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ (์ธ๋ผ์ธ ๋ทฐ)

์ธ๋ผ์ธ ๋ทฐ: FROM ์ ˆ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ. ORDER BY๋ฅผ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
๊ฒŒ์‹œํŒ ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ ๋•Œ ์“ด๋‹ค.
limit, offset์˜ ๊ฒฝ์šฐ mySQL์—๋งŒ ์žˆ๋‹ค.

์˜ˆ์ œ 5-23) FROM ๊ตฌ์—์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ
sq = ๊ฐ€์ƒ์˜ ํ…Œ์ด๋ธ”

SELECT * FROM (SELECT * FROM sample54) sq;


์˜ˆ์ œ 5-24) FROM ๊ตฌ์—์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ (AS๋กœ ์ง€์ •) p. 212
[3. ์ธ๋ผ์ธ๋ทฐ (*as ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ€์ƒ์˜ ํ…Œ์ด๋ธ”๋ช…์„ ๋ฐ˜๋“œ์‹œ ๊ผญ ์„ ์–ธํ•ด์•ผ ํ•œ๋‹ค.)]

SELECT * FROM (SELECT * FROM sample54) AS sq;


์˜ˆ์ œ 5-25) FROM ๊ตฌ์—์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ (3๋‹จ๊ณ„)

SELECT * FROM (SELECT * FROM (SELECT * FROM sample54) sq1) sq2;


- ์‹ค์ œ ์—…๋ฌด์—์„œ FROM ๊ตฌ์— ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์ง€์ •ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ
์˜ˆ์ œ 6-26) Oracle์—์„œ LIMIT ๊ตฌ์˜ ๋Œ€์ฒด ๋ช…๋ น

SELECT * FROM (
 SELECT * FROM sample54 ORDER BY a DESC
) sq
WHERE ROWNUM <= 2;
    sample54  
    no a
@rownum:=0

@rownum(๋ณ€์ˆ˜)๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ–ˆ๋‹ค.

int rownum = 0 (Java)
mySQL์—์„œ๋Š” @๋ฅผ ๋ถ™์ธ๋‹ค.
  1 900
  2 900
  3 900

select @rownum:=@rownum + 1 rownum, no, a -- @rownum: ๋ณ€์ˆ˜ ํ˜ธ์ถœ -- +1: ์ฐจ์ด๊ฐ’
from sample54, 
(select @rownum:=0) tmp -- @rownum:=0: ๋ณ€์ˆ˜ ์„ ์–ธ, ์ž„์‹œ์˜ ํ…Œ์ด๋ธ”๋ช…์„ ์ค€๋‹ค. Aliases
;

๊ต์žฌ p. 111 ์ฐธ๊ณ 

2023.03.14 - [๐ŸŒณK-Digital Revision 2023โœจ/SQL, MariaDB] - [52] 230314 [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 52์ผ] SQL Ch 3. ์ •๋ ฌ๊ณผ ์—ฐ์‚ฐ: 9๊ฐ• ์ •๋ ฌ - ORDER BY, 10๊ฐ• ๋ณต์ˆ˜์˜ ์—ด์„ ์ง€์ •ํ•ด ์ •๋ ฌํ•˜๊ธฐ, 11๊ฐ• ๊ฒฐ๊ณผ ํ–‰ ์ œํ•œํ•˜๊ธฐ - LIMIT, 12๊ฐ• ์ˆ˜์น˜ ์—ฐ์‚ฐ, 13๊ฐ• ๋ฌธ์ž์—ด ์—ฐ์‚ฐ, 14๊ฐ• ๋‚ ์งœ ์—ฐ์‚ฐ, 15..

๊ฒŒ์‹œํŒ board          
ROWNUM no title content regdate writer count
1 1 ์ œ๋ชฉ1 ๋‚ด์šฉ1 2023-03-15 ์ •์ž๋ฐ” 1
์ค‘๊ฐ„์— ๊ธ€ ์‚ญ์ œ ๋จ
10 12          

1~10, 11~20, 21~30์ฒ˜๋Ÿผ 10๋‹จ์œ„์”ฉ 1 page, 2 page, 3 page๊ฐ€ ๋œ๋‹ค. 

1PAGE NO  
  1 10
2PAGE NO  
  11 20
3PAGE NO  
  21 30

๋งŒ์•ฝ ๊ธ€ ํ•˜๋‚˜๋ฅผ ์‚ญ์ œ๋ฅผ ํ•˜๋ฉด ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. 
∴ ROWNUM์ด ํ•„์š”ํ•˜๋‹ค.

1PAGE ROWNUM  
  1 10
2PAGE ROWNUM  
  11 20
3PAGE ROWNUM
 
  21 30

23.6 INSERT ๋ช…๋ น๊ณผ ์„œ๋ธŒ์ฟผ๋ฆฌ

์˜ˆ์ œ 5-27) VALUES ๊ตฌ์—์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ
VALUES ๊ตฌ์˜ ๊ฐ’์œผ๋กœ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ

INSERT INTO sample541 VALUES(
 (SELECT COUNT(*) FROM sample51),
 (SELECT COUNT(*) FROM sample54)
);
SELECT * FROM sample541;

- INSERT SELECT 
์˜ˆ์ œ 5-28) SELECT ๊ฒฐ๊ณผ๋ฅผ INSERTํ•˜๊ธฐ

INSERT INTO sample541 SELECT 1, 2;
SELECT * FROM sample541;

์•„๋ž˜์˜ ์ฝ”๋“œ์™€ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ™๋‹ค. 

INSERT INTO sample541 VALUES (1, 2)

์—ด ๊ตฌ์„ฑ์ด ๋˜‘๊ฐ™์€ ํ…Œ์ด๋ธ” ์‚ฌ์ด์—๋Š” INSERT SELECT ๋ช…๋ น์œผ๋กœ ํ–‰์„ ๋ณต์‚ฌํ•  ์ˆ˜ ์žˆ๋‹ค.
์˜ˆ์ œ 5-22) ํ…Œ์ด๋ธ”์˜ ํ–‰ ๋ณต์‚ฌํ•˜๊ธฐ

INSERT INTO sample542 SELECT * FROM sample541;

24๊ฐ• ์ƒ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ p. 216

์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ์ผ์ข…์ธ '์ƒ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ'๋ฅผ EXITS ์ˆ ์–ด๋กœ ์กฐํ•ฉ์‹œ์ผœ์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๊ด€ํ•ด ์•Œ์•„๋ณธ๋‹ค.

exists
select ํ–ˆ์„ ๋•Œ ๊ทธ ํ…Œ์ด๋ธ”์— ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€๋ฅผ ํŒ๋ณ„ํ•  ๋•Œ
์ถ”๊ฐ€ํ•˜๋ ค๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ํ…Œ์ด๋ธ”์— ์žˆ๋Š”์ง€๋ฅผ ํŒ๋ณ„ํ•ด์„œ
์—†์œผ๋ฉด insert
์žˆ์œผ๋ฉด update
UPDATE sample551 SET a = '์žˆ์Œ' WHERE EXISTS (
SELECT * FROM sample552 WHERE no2 = no -- ์„œ๋ธŒ์ฟผ๋ฆฌ, no: sample551์˜ no ์—ด
);
-- no2์˜ a์—ด ๊ฐ’๊ณผ no์˜ a์—ด ๊ฐ’์ด ๊ฐ™์œผ๋ฉด a๋ฅผ '์žˆ์Œ'์œผ๋กœ ๋ฐ”๊พผ๋‹ค.

EXITS

EXISTS (SELECT๋ช…๋ น)

The EXISTS operator is used to test for the existence of any record in a subquery.
The EXISTS operator returns TRUE if the subquery returns one or more records.


24.1 EXISTS (์กด์žฌํ•˜๋ฉด)

์˜ˆ์ œ 5-30) sample551 ํ…Œ์ด๋ธ”๊ณผ sample552 ํ…Œ์ด๋ธ”

SELECT * FROM sample551;
SELECT * FROM sample552;

sample552 ํ…Œ์ด๋ธ”์— ์žˆ๋Š”์ง€๋ฅผ ์กฐ์‚ฌํ•œ ๊ฒฐ๊ด๊ฐ’์„ ๋„ฃ๊ณ  ์‹ถ๋‹ค.

์˜ˆ์ œ 5-30) sample551 ํ…Œ์ด๋ธ”๊ณผ sample552 ํ…Œ์ด๋ธ”

-- sample551ํ…Œ์ด๋ธ”์˜ a์—ด ๊ฐ’์ด sample552ํ…Œ์ด๋ธ”์˜ a2์—ด์— ์กด์žฌํ•˜๋ฉด
UPDATE sample551 SET a = '์žˆ์Œ' WHERE
EXISTS (SELECT * FROM sample552 WHERE no2 = no);

SELECT * FROM sample551;

'์žˆ์Œ'์œผ๋กœ ๊ฐฑ์‹ ๋˜์—ˆ๋‹ค.


24.2 NOT EXISTS 

์˜ˆ์ œ 5-32) NOT EXISTS๋ฅผ ์‚ฌ์šฉํ•ด '์—†์Œ'์œผ๋กœ ๊ฐฑ์‹ ํ•˜๊ธฐ

UPDATE sample551 SET a = '์—†์Œ' WHERE
	NOT EXISTS (SELECT * FROM sample552 WHERE no2 = no);

SELECT * FROM sample551;

noํ–‰์˜ 1, 2, 4์—ด์ด '์—†์Œ'์œผ๋กœ ๊ฐฑ์‹ ๋˜์—ˆ๋‹ค.


24.3 ์ƒ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ

๋ถ€๋ชจ ๋ช…๋ น๊ณผ ์ž์‹์ธ ์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ ํŠน์ • ๊ด€๊ณ„๋ฅผ ๋งบ๋Š” ๊ฒƒ์„ '์ƒ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ'๋ผ ๋ถ€๋ฅธ๋‹ค.
์ƒ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ์—์„œ๋Š” ๋ถ€๋ชจ ๋ช…๋ น๊ณผ ์—ฐ๊ด€๋˜์–ด ์ฒ˜๋ฆฌ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋ธŒ์ฟผ๋ฆฌ ๋ถ€๋ถ„๋งŒ์„ ๋”ฐ๋กœ ๋–ผ์–ด๋‚ด์–ด ์‹คํ–‰์‹œํ‚ฌ ์ˆ˜ ์—†๋‹ค.

UPDATE sample551 SET a = '์žˆ์Œ' WHERE
 EXISTS (SELECT * FROM sample552 WHERE no2 = no);

SELECT * FROM sample552 WHERE no2 = no;
-- ->์—๋Ÿฌ: no2๊ฐ€ ๋ถˆ๋ช…ํ™•ํ•˜๋‹ค.

- ํ…Œ์ด๋ธ”๋ช… ๋ถ™์ด๊ธฐ
์–‘์ชฝ ํ…Œ์ด๋ธ” ๋ชจ๋‘ ๊ฐ™์€ ์ด๋ฆ„์˜ ์—ด๋กœ ๋˜์–ด์žˆ๋‹ค๋ฉด ์ž˜ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค.

์˜ˆ์ œ 5-33) ์—ด์— ํ…Œ์ด๋ธ”๋ช… ๋ถ™์ด๊ธฐ
์—ด๋ช… ์•ž์— 'ํ…Œ์ด๋ธ”๋ช….'์„ ๋ถ™์ธ๋‹ค.

sample552.no2
sample551.no

MySQL์—์„œ๋Š” WHERE sample552.no = sample552.no๊ฐ€ ๋˜์–ด ์กฐ๊ฑด์‹์€ ํ•ญ์ƒ ์ฐธ์ด ๋œ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋ชจ๋“  ํ–‰์€ a์—ด ๊ฐ’์ด '์žˆ์Œ'์œผ๋กœ ๊ฐฑ์‹ ๋œ๋‹ค.

์•„๋ž˜ ์ฝ”๋“œ๋Š” ๋ฌธ์ œ ์—†์ด ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

UPDATE sample551 SET a = '์žˆ์Œ' WHERE
 EXISTS (SELECT * FROM sample552 WHERE sample552.no2 = sample551.no);


24.4 IN (์—ด๋ช… IN (์ง‘ํ•ฉ)) p. 221

IN์—ฐ์‚ฐ์ž
WHERE ์กฐ๊ฑด์‹ OR ์กฐ๊ฑด์‹
WHERE IN (์กฐ๊ฑด์‹, ์กฐ๊ฑด์‹)

IN์„ ์‚ฌ์šฉํ•˜๋ฉด ์ง‘ํ•ฉ ์•ˆ์˜ ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€๋ฅผ ์กฐ์‚ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

IN

์—ด๋ช… IN(์ง‘ํ•ฉ)

IN์—์„œ๋Š” ์˜ค๋ฅธ์ชฝ์— ์ง‘ํ•ฉ์„ ์ง€์ •ํ•œ๋‹ค. ์™ผ์ชฝ์— ์ง€์ •๋œ ๊ฐ’๊ณผ ๊ฐ™์€ ๊ฐ’์ด ์ง‘ํ•ฉ ์•ˆ์— ์กด์žฌํ•˜๋ฉด ์ฐธ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
OR๋กœ ๊ธฐ์ˆ ํ–ˆ์„ ๋•Œ์™€ ๋ณ„ ์ฐจ์ด๊ฐ€ ์—†๋Š” ๊ฒƒ ๊ฐ™์ง€๋งŒ, ๊ฐ’์„ ์—ฌ๋Ÿฌ๊ฐœ ์ง€์ •ํ•  ๊ฒฝ์šฐ์—๋Š” ์กฐ๊ฑด์‹์ด ์ƒ๋‹นํžˆ ๊น”๋”ํ•ด์ง„๋‹ค.

์˜ˆ์ œ 5-34) IN์„ ์‚ฌ์šฉํ•ด ์กฐ๊ฑด์‹ ๊ธฐ์ˆ 

 SELECT * FROM sample551 WHERE no IN(3, 5);

์ง€์ •๋œ ๊ฐ’์ด ๋ฐ˜ํ™˜๋œ๋‹ค. ํ…Œ์ด๋ธ” sample551์˜ no์—ด์— ์—†๋Š” ๊ฐ’์„ ๋„ฃ์–ด๋„ ์žˆ๋Š” ๊ฐ’๋งŒ ๋ฐ˜ํ™˜๋œ๋‹ค. 

์˜ˆ์ œ 5-35) IN์˜ ์˜ค๋ฅธ์ชฝ์„ ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ ์ง€์ •ํ•˜๊ธฐ

SELECT * FROM sample551 WHERE no IN
	(SELECT no2 FROM sample552)


- IN๊ณผ NULL

IN์—์„œ๋Š” ์ง‘ํ•ฉ ์•ˆ์— NULL ๊ฐ’์ด ์žˆ์–ด๋„ ๋ฌด์‹œํ•˜์ง€๋Š” ์•Š๋Š”๋‹ค. NULL์„ ๋น„๊ตํ•  ๋•Œ๋Š” IS NULL์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

MySQL์—์„œ ์ง‘ํ•ฉ์— NULL์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ, ์กฐ๊ฑด์‹ IN์€ ์™ผ์ชฝ ๊ฐ’์ด ์ง‘ํ•ฉ์— ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉด ์ฐธ์„, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด NULL์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.  ์กฐ๊ฑด์‹ NOT IN์€ ์™ผ์ชฝ ๊ฐ’์ด ์ง‘ํ•ฉ์— ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉด ๊ฑฐ์ง“์„, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด NULL์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
NULL์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค: ๋น„๊ต๋ฅผ ํ•  ์ˆ˜ ์—†๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.


728x90
๋ฐ˜์‘ํ˜•
Comments