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๐ŸŒณ๐Ÿ˜Š

[53] 230315 SQL Ch 5. ์ง‘๊ณ„์™€ ์„œ๋ธŒ์ฟผ๋ฆฌ: 20๊ฐ• ํ–‰ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ - COUNT, 21๊ฐ• COUNT ์ด์™ธ์˜ ์ง‘๊ณ„ํ•จ์ˆ˜, 22๊ฐ• ๊ทธ๋ฃนํ™” - GROUP BY [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 53์ผ] ๋ณธ๋ฌธ

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

[53] 230315 SQL Ch 5. ์ง‘๊ณ„์™€ ์„œ๋ธŒ์ฟผ๋ฆฌ: 20๊ฐ• ํ–‰ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ - COUNT, 21๊ฐ• COUNT ์ด์™ธ์˜ ์ง‘๊ณ„ํ•จ์ˆ˜, 22๊ฐ• ๊ทธ๋ฃนํ™” - GROUP BY [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 53์ผ]

yjyuwisely 2023. 3. 15. 11:50

230315 Wed 53rd class

Ch 5. ๋ฐ์ดํ„ฐ์˜ ์ถ”๊ฐ€, ์‚ญ์ œ, ๊ฐฑ์‹ 
์ง„๋„: p.  182 ~ (๊ต์žฌ: 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

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


๋ชฐ๋ž๋˜ ๊ฒƒ ์ •๋ฆฌ

์ง‘๊ณ„ํ•จ์ˆ˜(COUNT, SUM, AVG, MIN, MAX)๋Š” ์ง‘ํ•ฉ ์•ˆ์— NULL ๊ฐ’์ด ์žˆ์„ ๊ฒฝ์šฐ ๋ฌด์‹œํ•œ๋‹ค. p. 186

๋ฌธ๋ฒ• ์ž‘์„ฑ ์ˆœ์„œ 
1. SELECT ๊ตฌ → 2. WHERE ๊ตฌ → 3. GROUP BY ๊ตฌ → 4. HAVING ๊ตฌ → 5. ORDER BY ๊ตฌ

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);

์˜ˆ์ œ 1)

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;

์˜ˆ์ œ 2)

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;

์ฐธ๊ณ : https://www.w3schools.com/sql/sql_having.asp

๋‚ด๋ถ€์ฒ˜๋ฆฌ ์ˆœ์„œ p. 200

1. WHERE ๊ตฌ → 2. GROUP BY ๊ตฌ → 3. HAVING ๊ตฌ → 4. SELECT ๊ตฌ → 5. ORDER BY ๊ตฌ


MySQL์—์„œ ์˜ค๋ฅ˜๋‚˜๋Š” ๊ฒฝ์šฐ (p. 206, p. 211)
์•„๋ž˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์จ์•ผํ•œ๋‹ค.

p. 206
p. 211


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

20๊ฐ• ํ–‰ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ - COUNT

์ง‘๊ณ„ํ•จ์ˆ˜

COUNT(์ง‘ํ•ฉ)
SUM(์ง‘ํ•ฉ)
AVG(์ง‘ํ•ฉ)
MIN(์ง‘ํ•ฉ)
MAX(์ง‘ํ•ฉ)

์ง‘๊ณ„ํ•จ์ˆ˜๋Š” ์ง‘ํ•ฉ ์•ˆ์— NULL ๊ฐ’์ด ์žˆ์„ ๊ฒฝ์šฐ ๋ฌด์‹œํ•œ๋‹ค. p. 186


20.1 COUNT๋กœ ํ–‰ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ

COUNT

COUNT(์ง‘ํ•ฉ)

์˜ˆ์ œ 5-1) sample51์˜ ํ–‰ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ

SELECT * FROM sample51;
-- COUNT๋กœ ํ–‰ ๊ฐœ์ˆ˜ ๊ณ„์‚ฐ
SELECT COUNT(*) FROM sample51;

COUNT(*)๋Š” ๋ชจ๋“  ์—ด์˜ ํ–‰์ˆ˜๋ฅผ ์นด์šดํŠธํ•˜๋ฏ€๋กœ NULL ๊ฐ’๋„ ๊ฐœ์ˆ˜์— ํฌํ•จ๋œ๋‹ค.

COUNT๋Š” ์ธ์ˆ˜๋กœ ์ง€์ •๋œ ์ง‘ํ•ฉ(์ด ๊ฒฝ์šฐ๋Š” ํ…Œ์ด๋ธ” ์ „์ฒด)์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค. sample51์—๋Š” ์ „๋ถ€ 5๊ฐœ์˜ ํ–‰์ด ์žˆ์œผ๋ฏ€๋กœ 5๊ฐ€ ๋ฐ˜ํ™˜๋˜์—ˆ๋‹ค.

Point: COUNT ์ง‘๊ณ„ํ•จ์ˆ˜๋กœ ํ–‰ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.


- WHERE ๊ตฌ ์ง€์ •ํ•˜๊ธฐ
์˜ˆ์ œ 5-2) sample51์˜ ํ–‰ ๊ฐœ์ˆ˜๋ฅผ WHERE ๊ตฌ๋ฅผ ์ง€์ •ํ•˜์—ฌ ๊ตฌํ•˜๊ธฐ

SELECT * FROM sample51 WHERE name = 'A';
SELECT COUNT(*) FROM smaple51 WHERE name = 'A';


20.2 ์ง‘๊ณ„ํ•จ์ˆ˜์™€ NULL๊ฐ’

์˜ˆ์ œ 5-3) ํ–‰ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•  ๋•Œ NULL ๊ฐ’ ๋‹ค๋ฃจ๊ธฐ

SELECT * FROM sample51;
SELECT COUNT(no), COUNT(name) FROM sample51; -- null์€ ๋ฌด์‹œ๋œ๋‹ค.

COUNT(*): ๋ชจ๋“  ์—ด์˜ ํ–‰์ˆ˜๋ฅผ ์นด์šดํŠธํ•˜๋ฏ€๋กœ NULL ๊ฐ’์ด ์žˆ์–ด๋„ ํ•ด๋‹น ์ •๋ณด๊ฐ€ ๋ฌด์‹œ๋˜์ง€ ์•Š๋Š”๋‹ค.

Point: ์ง‘๊ณ„ํ•จ์ˆ˜๋Š” ์ง‘ํ•ฉ ์•ˆ์— NULL ๊ฐ’์ด ์žˆ์„ ๊ฒฝ์šฐ ๋ฌด์‹œํ•œ๋‹ค.


20.3 DISCOUNT๋กœ ์ค‘๋ณต ์ œ๊ฑฐ

์˜ˆ์ œ 5-4) sample51 ํ…Œ์ด๋ธ”

SELECT * FROM sample51;

A ๊ฐ’์ด ์ค‘๋ณต๋œ๋‹ค.

DISTINCT: ์ค‘๋ณต๋œ ๊ฐ’์„ ์ œ๊ฑฐํ•˜๋Š” ํ•จ์ˆ˜

์˜ˆ์ œ 5-5) DISTINCT๋กœ ์ค‘๋ณต ์ œ๊ฑฐํ•˜๊ธฐ

SELECT ALL name FROM sample51;
-- DISTINCT๋ฅผ ์ง€์ •, ์ฝค๋งˆ๋ฅผ ๋ถ™์ด์ง€ ์•Š๋Š”๋‹ค.
SELECT DISTINCT name FROM sample51;

Point: DISTINCT๋กœ ์ค‘๋ณต๊ฐ’์„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค.


20.4 ์ง‘๊ณ„ํ•จ์ˆ˜์—์„œ DISTINCT

์˜ˆ์ œ 5-6) ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๋’ค ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ

SELECT COUNT(ALL name), COUNT(DISTINCT name) FROM sample51;

NULL์„ ์ œ์™ธํ•œ ๊ฐ’์˜ ๊ฐœ์ˆ˜

์•ž์˜ ์˜ˆ์ œ 5-5๋ฅผ ๋ณด๋ฉด COUNT(ALL name)์˜ ๊ตฌ์„ฑ์€ A A B C๋กœ ์ด 4๊ฐœ
COUNT(DISTINCT name)์˜ ๊ตฌ์„ฑ์€ A B C๋กœ ์ด 3๊ฐœ์ธ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.


21๊ฐ• COUNT ์ด์™ธ์˜ ์ง‘๊ณ„ํ•จ์ˆ˜ (SUM, AVG, MIN, MAX) p. 190

COUNT([ALL|DISTINCT] ์ง‘ํ•ฉ)
SUM([ALL|DISTINCT] ์ง‘ํ•ฉ)
AVG([ALL|DISTINCT] ์ง‘ํ•ฉ)
MIN([ALL|DISTINCT] ์ง‘ํ•ฉ)
MAX([ALL|DISTINCT] ์ง‘ํ•ฉ)

21.1 SUM์œผ๋กœ ํ•ฉ๊ณ„ ๊ตฌํ•˜๊ธฐ

์˜ˆ์ œ 5-7) SUM์œผ๋กœ ํ•ฉ๊ณ„ ๊ตฌํ•˜๊ธฐ

SELECT * FROM sample51;
-- SUM์œผ๋กœ quantity์—ด์˜ ํ•ฉ๊ณ„ ๊ตฌํ•˜๊ธฐ
SELECT SUM(quantity) FROM sample51;

1, 2, 10, 3 -> SUM์˜ ์ธ์ˆ˜๋กœ ์‚ฌ์šฉ๋  ์ง‘ํ•ฉ

Point: ์ง‘๊ณ„ํ•จ์ˆ˜๋กœ ์ง‘ํ•ฉ์˜ ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.


21.2 AVG๋กœ ํ‰๊ท ๋‚ด๊ธฐ

์˜ˆ์ œ 5-8) AVG๋กœ ํ‰๊ท ๊ฐ’ ๊ตฌํ•˜๊ธฐ

SELECT * FROM sample51;
SELECT AVG(quantity), SUM(quantity)/COUNT(quantity) FROM sample51;

NULL ๊ฐ’์€ ๋ฌด์‹œ๋๋‹ค. 16 / 4 = 4์ด๋‹ค.

Point: AVG ์ง‘๊ณ„ํ•จ์ˆ˜๋กœ ์ง‘ํ•ฉ์˜ ํ‰๊ท ๊ฐ’์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.


์˜ˆ์ œ 5-9) AVG๋กœ ํ‰๊ท ๊ฐ’ ๊ณ„์‚ฐ (NULL์„ 0์œผ๋กœ ๋ณ€ํ™˜)

SELECT AVG(CASE WHEN quantity IS NULL THEN 0 ELSE quantity END)
	AS avgnull0 FROM sample51;

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..

case when ์กฐ๊ฑด์‹1 then ์‹1 else ์‹3 end

์ˆ˜์—…์˜ ๋” ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•)

IFNULL ์‚ฌ์šฉ์œผ๋กœ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.


21.3 MIN, MAX๋กœ ์ตœ์†Ÿ๊ฐ’, ์ตœ๋Œ“๊ฐ’ ๊ตฌํ•˜๊ธฐ

์˜ˆ์ œ 5-10) MIN, MAX๋กœ ์ตœ์†Ÿ๊ฐ’, ์ตœ๋Œ“๊ฐ’ ๊ตฌํ•˜๊ธฐ

SELECT * FROM sample51;
SELECT MIN(quantity), MAX(qunatity), MIN(name), MAX(name) FROM sample51;

1: quantity์˜ ์ตœ์†Ÿ๊ฐ’, 10: quantity์˜ ์ตœ๋Œ“๊ฐ’


22๊ฐ• ๊ทธ๋ฃนํ™” - GROUP BY

GROUP BY

SELECT * FROM ํ…Œ์ด๋ธ”๋ช… GROUP BY ์—ด1, ์—ด2, ...

22.1 GROUP BY๋กœ ๊ทธ๋ฃนํ™”

์˜ˆ์ œ 5-11) sample51 ํ…Œ์ด๋ธ”

SELECT * FROM sample51;

-- GROUP BY ๊ตฌ์— name์—ด์„ ์ง€์ •ํ•ด ๊ทธ๋ฃนํ™”ํ•˜๊ธฐ
SELECT name FROM sample51 GROUP BY name;

์˜ˆ์ œ 5-12) name ์—ด๋กœ ๊ทธ๋ฃนํ™”ํ•˜๊ธฐ

GROUP BY: ์ง€์ •๋œ ์—ด์˜ ๊ฐ’์ด ๊ฐ™์€ ํ–‰์ด ํ•˜๋‚˜์˜ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์ธ๋‹ค.

Point: GROUP BY ๊ตฌ๋กœ ๊ทธ๋ฃนํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค.


์˜ˆ์ œ 5-13) name ์—ด์„ ๊ทธ๋ฃนํ™”ํ•ด ๊ณ„์‚ฐํ•˜๊ธฐ

-- GROUP BY ๊ตฌ์˜ ์ง‘๊ณ„ํ•จ์ˆ˜๋ฅผ ์กฐํ•ฉ
SELECT name, COUNT(name), SUM(quantity)
	FROM sample51 GROUP BY name;


22.2 HAVING ๊ตฌ๋กœ ์กฐ๊ฑด ์ง€์ •

The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions.

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);

์ฐธ๊ณ : https://www.w3schools.com/sql/sql_having.asp

Point: WHERE ๊ตฌ์—์„œ๋Š” ์ง‘๊ณ„ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.


์˜ˆ์ œ 5-14) HAVING์„ ์‚ฌ์šฉํ•ด ๊ฒ€์ƒ‰

SELECT name, COUNT(name) FROM sample51 GROUP BY name;

-- HAVING๊ตฌ๋กœ ๊ฑธ๋Ÿฌ๋‚ด๊ธฐ
SELECT name, COUNT(name) FROM sample51 
	GROUP BY name HAVING COUNT(name) = 1;

Point: ์ง‘๊ณ„ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ HAVNIG ๊ตฌ๋กœ ๊ฒ€์ƒ‰์กฐ๊ฑด์„ ์ง€์ •ํ•œ๋‹ค.

๋ฌธ๋ฒ• ์ž‘์„ฑ ์ˆœ์„œ
1. SELECT ๊ตฌ 2. WHERE ๊ตฌ 3. GROUP BY ๊ตฌ 4. HAVING ๊ตฌ → 5. ORDER BY ๊ตฌ

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);



๋‚ด๋ถ€์ฒ˜๋ฆฌ ์ˆœ์„œ

1. WHERE ๊ตฌ 2. GROUP BY ๊ตฌ 3. HAVING ๊ตฌ  4. SELECT ๊ตฌ → 5. ORDER BY ๊ตฌ


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..

SELECT SQL ๋ฌธ๋ฒ• ์ž‘์„ฑ ์ˆœ์„œ (์‚ฌ์šฉ์ž ์ž‘์„ฑ)

 1. SELECT ์—ด๋ช… (5)
 FROM ํ…Œ์ด๋ธ”๋ช… (1)
 2. WHERE ์กฐ๊ฑด์‹ (2)
 3. GROUP BY ๊ทธ๋ฃนํ™”ํ•  ์—ด๋ช… (3)
 4. HAVING ๊ทธ๋ฃนํ•˜๊ณ  ๋‚œ ํ›„์˜ ์กฐ๊ฑด์‹ (4)
 5. ORDER BY ์ •๋ ฌํ•˜๊ณ ์ž ํ•˜๋Š” ์—ด๋ช… (6)

SELECT SQL ์ฟผ๋ฆฌ ์‹คํ–‰ ์ˆœ์„œ (DBMS ๋‚ด๋ถ€ ์ฒ˜๋ฆฌ) 

 FROM: ์กฐํšŒ ํ…Œ์ด๋ธ” ํ™•์ธ 
 1. WHERE: ๋ฐ์ดํ„ฐ ์ถ”์ถœ ์กฐ๊ฑด ํ™•์ธ
 2. GROUP BY: ๊ทธ๋ฃนํ•‘
 3. HAVING: ๊ทธ๋ฃนํ•œ ํ›„ ์กฐ๊ฑด
 4. SELECT: ๋ฐ์ดํ„ฐ ์ถ”์ถœ
 5. ORDER BY: ๋ฐ์ดํ„ฐ ์ˆœ์„œ ์ •๋ ฌ 

Point: ์ง‘๊ณ„ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ HAVING ๊ตฌ๋กœ ๊ฒ€์ƒ‰์กฐ๊ฑด์„ ์ง€์ •ํ•œ๋‹ค.


22.3 ๋ณต์ˆ˜์—ด์˜ ๊ทธ๋ฃนํ™”

์ง‘๊ณ„ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ง‘ํ•ฉ์€ ํ•˜๋‚˜์˜ ๊ฐ’์œผ๋กœ ๊ณ„์‚ฐ๋˜๋ฏ€๋กœ, ๊ทธ๋ฃน๋งˆ๋‹ค ํ•˜๋‚˜์˜ ํ–‰์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.

SELECT MIN(no), name, SUM(quantity) FROM sample51 GROUP BY name;

Point: GROUP BY์—์„œ ์ง€์ •ํ•œ ์—ด ์ด์™ธ์˜ ์—ด์€ ์ง‘๊ณ„ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ์ฑ„ SELECT ๊ตฌ์— ์ง€์ •ํ•  ์ˆ˜ ์—†๋‹ค.


no์™€ quantity๋กœ ๊ทธ๋ฃนํ™”ํ•œ๋‹ค๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ํ•œ๋‹ค.

SELECT no, quantity FROM sample51 GROUP BY no, quantity;


22.4 ๊ฒฐ๊ด๊ฐ’ ์ •๋ ฌ

์˜ˆ์ œ 5-15) ์ง‘๊ณ„ํ•œ ๊ฒฐ๊ณผ ์ •๋ ฌํ•˜๊ธฐ

-- name ์—ด๋กœ ๊ทธ๋ฃนํ™”ํ•ด ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•˜๊ณ  ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ
SELECT name, COUNT(name), SUM(quantity)
	FROM sample51 GROUP BY name ORDER BY SUM(quantity) DESC;


728x90
๋ฐ˜์‘ํ˜•

'๐ŸŒณBootcamp Revision 2023โœจ > Python, Flask, SQL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[55] 230317 SQL Ch. 7 ๋ณต์ˆ˜์˜ ํ…Œ์ด๋ธ” ๋‹ค๋ฃจ๊ธฐ: 31๊ฐ• ์ง‘ํ•ฉ ์—ฐ์‚ฐ, 32๊ฐ• ํ…Œ์ด๋ธ” ๊ฒฐํ•ฉ, 33๊ฐ• ๊ด€๊ณ„ํ˜• ๋ชจ๋ธ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 54์ผ]  (0) 2023.03.16
[54] 230316 SQL Ch 5. ์ง‘๊ณ„์™€ ์„œ๋ธŒ์ฟผ๋ฆฌ: 23๊ฐ• ์„œ๋ธŒ์ฟผ๋ฆฌ, 24๊ฐ• ์ƒ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 54์ผ]  (0) 2023.03.16
*[53] 230315 SQL Ch 4. ๋ฐ์ดํ„ฐ์˜ ์ถ”๊ฐ€, ์‚ญ์ œ, ๊ฐฑ์‹ : 16๊ฐ• ํ–‰ ์ถ”๊ฐ€ํ•˜๊ธฐ - INSERT, 17๊ฐ• ์‚ญ์ œํ•˜๊ธฐ - DELETE, 18๊ฐ• ๋ฐ์ดํ„ฐ ๊ฐฑ์‹ ํ•˜๊ธฐ - UPDATE, 19๊ฐ• ๋ฌผ๋ฆฌ์‚ญ์ œ์™€ ๋…ผ๋ฆฌ์‚ญ์ œ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 53์ผ]  (0) 2023.03.15
[52] 230314 SQL Ch 3. ์ •๋ ฌ๊ณผ ์—ฐ์‚ฐ: 9๊ฐ• ์ •๋ ฌ - ORDER BY, 10๊ฐ• ๋ณต์ˆ˜์˜ ์—ด์„ ์ง€์ •ํ•ด ์ •๋ ฌํ•˜๊ธฐ, 11๊ฐ• ๊ฒฐ๊ณผ ํ–‰ ์ œํ•œํ•˜๊ธฐ - LIMIT, 12๊ฐ• ์ˆ˜์น˜ ์—ฐ์‚ฐ, 13๊ฐ• ๋ฌธ์ž์—ด ์—ฐ์‚ฐ, 14๊ฐ• ๋‚ ์งœ ์—ฐ์‚ฐ, 15 CASE ๋ฌธ์œผ๋กœ ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜ํ•˜..  (1) 2023.03.14
Comments