[DB][MySQL][๊ฐœ๋…]๐ŸŒŸ MySQL ๋‚ด์žฅ ํ•จ์ˆ˜ ์ด์ •๋ฆฌ ๐ŸŒŸ

๊น€์ƒ์šฑยท2024๋…„ 9์›” 10์ผ
0
post-thumbnail

๐ŸŒŸ MySQL ๋‚ด์žฅ ํ•จ์ˆ˜ ์ด์ •๋ฆฌ ๐ŸŒŸ

MySQL์—์„œ ๋‹จ์ผํ–‰ ํ•จ์ˆ˜์™€ ๋‹ค์ค‘ํ–‰ ํ•จ์ˆ˜๋Š” ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ์— ํ•„์ˆ˜์ ์ด์ฃ !
์˜ค๋Š˜์€ ๊ทธ ํ•จ์ˆ˜๋“ค์— ๋Œ€ํ•ด ํ•˜๋‚˜ํ•˜๋‚˜ ์•Œ์•„๋ณผ ๊ฑฐ์˜ˆ์š”. ์ž์„ธํ•œ ์˜ˆ์ œ์™€ ํ•จ๊ป˜ ์‰ฝ๊ฒŒ ์„ค๋ช…ํ•  ํ…Œ๋‹ˆ ๋๊นŒ์ง€ ๋”ฐ๋ผ์™€ ์ฃผ์„ธ์š”! ๐Ÿ˜Ž


โœ… ๋‹จ์ผํ–‰ ํ•จ์ˆ˜(Single-Row Function)

๋‹จ์ผํ–‰ ํ•จ์ˆ˜๋Š” ๊ฐ ํ–‰์—์„œ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
์ฆ‰, ๊ฐ ํ–‰์˜ ๊ฐ’์„ ํŠน์ • ๋ฐฉ์‹์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ํ•œ ๊ฐœ์˜ ๊ฒฐ๊ณผ๋งŒ ์–ป์„ ์ˆ˜ ์žˆ์–ด์š”!


โœ… ๋‹ค์ค‘ํ–‰ ํ•จ์ˆ˜(Multi-Row Function)

๋‹ค์ค‘ํ–‰ ํ•จ์ˆ˜๋Š” ์—ฌ๋Ÿฌ ํ–‰์„ ๋ฐ›์•„์„œ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๋Š” ํ•จ์ˆ˜์˜ˆ์š”.
๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋ฃนํ™”ํ•œ ํ›„ ๊ณ„์‚ฐ์„ ์ง„ํ–‰ํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ต๋‹ˆ๋‹ค!


๐Ÿ”ข ์ˆซ์ž ๊ด€๋ จ ํ•จ์ˆ˜

1. ABS(์ˆซ์ž) : ์ ˆ๋Œ€๊ฐ’ ๊ตฌํ•˜๊ธฐ

SELECT ABS(-10) AS absolute_value;
-- ๊ฒฐ๊ณผ: 10

2. CEILING(์ˆซ์ž) : ์†Œ์ˆ˜์  ์˜ฌ๋ฆผ

SELECT CEILING(4.3) AS ceiling_value;
-- ๊ฒฐ๊ณผ: 5

3. FLOOR(์ˆซ์ž) : ์†Œ์ˆ˜์  ๋‚ด๋ฆผ

SELECT FLOOR(4.7) AS floor_value;
-- ๊ฒฐ๊ณผ: 4

4. ROUND(์ˆซ์ž, ์ž๋ฆฟ์ˆ˜) : ์†Œ์ˆ˜์  ๋ฐ˜์˜ฌ๋ฆผ

SELECT ROUND(123.456, 2) AS rounded_value;
-- ๊ฒฐ๊ณผ: 123.46

5. TRUNCATE(์ˆซ์ž, ์ž๋ฆฟ์ˆ˜) : ์†Œ์ˆ˜์  ์ž๋ฅด๊ธฐ

SELECT TRUNCATE(123.456, 2) AS truncated_value;
-- ๊ฒฐ๊ณผ: 123.45

6. POW(X, Y) : X์˜ Y ์ œ๊ณฑ

SELECT POW(2, 3) AS power_value;
-- ๊ฒฐ๊ณผ: 8

7. MOD(๋ถ„์ž, ๋ถ„๋ชจ) : ๋‚˜๋จธ์ง€ ๊ตฌํ•˜๊ธฐ

SELECT MOD(10, 3) AS mod_value;
-- ๊ฒฐ๊ณผ: 1

8. GREATEST(์ˆซ์ž1, ์ˆซ์ž2, ...) : ๊ฐ€์žฅ ํฐ ๊ฐ’ ์ฐพ๊ธฐ

SELECT GREATEST(10, 20, 5, 30) AS greatest_value;
-- ๊ฒฐ๊ณผ: 30

9. LEAST(์ˆซ์ž1, ์ˆซ์ž2, ...) : ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’ ์ฐพ๊ธฐ

SELECT LEAST(10, 20, 5, 30) AS least_value;
-- ๊ฒฐ๊ณผ: 5

10. RAND() : ๋‚œ์ˆ˜ ์ƒ์„ฑ

SELECT RAND() AS random_value;
-- ๊ฒฐ๊ณผ: 0๊ณผ 1 ์‚ฌ์ด์˜ ๋‚œ์ˆ˜ (์˜ˆ: 0.345679)

๐Ÿงก ๋ฌธ์ž ๊ด€๋ จ ํ•จ์ˆ˜

1. ASCII(๋ฌธ์ž) : ์•„์Šคํ‚ค ์ฝ”๋“œ ๊ตฌํ•˜๊ธฐ

SELECT ASCII('A') AS ascii_value;
-- ๊ฒฐ๊ณผ: 65

2. CHAR(ASCII) : ์•„์Šคํ‚ค ์ฝ”๋“œ๋กœ ๋ฌธ์ž ๋ณ€ํ™˜

SELECT CHAR(65) AS char_value;
-- ๊ฒฐ๊ณผ: A

3. CONCAT('๋ฌธ์ž์—ด1', '๋ฌธ์ž์—ด2', ...) : ๋ฌธ์ž์—ด ๊ฒฐํ•ฉ

SELECT CONCAT('Hello', ' ', 'World') AS concatenated_string;
-- ๊ฒฐ๊ณผ: 'Hello World'

4. CONCAT_WS('๊ตฌ๋ถ„์ž', '๋ฌธ์ž์—ด1', ...) : ๊ตฌ๋ถ„์ž๋ฅผ ์‚ฌ์šฉํ•œ ๋ฌธ์ž์—ด ๊ฒฐํ•ฉ

SELECT CONCAT_WS('-', '2024', '09', '10') AS concatenated_with_separator;
-- ๊ฒฐ๊ณผ: '2024-09-10'

5. INSERT('๋ฌธ์ž์—ด', ์‹œ์ž‘์œ„์น˜, ๊ธธ์ด, '์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด') : ๋ฌธ์ž์—ด์˜ ์ผ๋ถ€ ๋Œ€์น˜

SELECT INSERT('Hello World', 7, 5, 'Softeer') AS inserted_string;
-- ๊ฒฐ๊ณผ: 'Hello Softeer'

6. REPLACE('๋ฌธ์ž์—ด', '๊ธฐ์กด๋ฌธ์ž์—ด', '๋ฐ”๊ฟ€๋ฌธ์ž์—ด') : ๋ฌธ์ž์—ด ์น˜ํ™˜

SELECT REPLACE('Hyundai Softeer', 'Hyundai', 'Samsung') AS replaced_string;
-- ๊ฒฐ๊ณผ: 'Samsung Softeer'

7. INSTR('๋ฌธ์ž์—ด', '์ฐพ๋Š” ๋ฌธ์ž์—ด') : ๋ฌธ์ž์—ด์˜ ์œ„์น˜ ์ฐพ๊ธฐ

SELECT INSTR('Hyundai Softeer', 'Softeer') AS position;
-- ๊ฒฐ๊ณผ: 9

8. MID('๋ฌธ์ž์—ด', ์‹œ์ž‘์œ„์น˜, ๊ฐœ์ˆ˜) : ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ์ถ”์ถœ (MySQL์—์„œ ์‚ฌ์šฉ)

SELECT MID('Hyundai Softeer', 9, 7) AS mid_string;
-- ๊ฒฐ๊ณผ: 'Softeer'

9. SUBSTRING('๋ฌธ์ž์—ด', ์‹œ์ž‘์œ„์น˜, ๊ฐœ์ˆ˜) : ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ์ถ”์ถœ (๋ชจ๋“  DBMS์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ)

SELECT SUBSTRING('Hyundai Softeer', 9, 7) AS substring_value;
-- ๊ฒฐ๊ณผ: 'Softeer'

10. FORMAT(์ˆซ์ž, ์ž๋ฆฟ์ˆ˜) : ์ˆซ์ž๋ฅผ 3์ž๋ฆฌ๋งˆ๋‹ค ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„

SELECT FORMAT(1234567.891, 2) AS formatted_number;
-- ๊ฒฐ๊ณผ: '1,234,567.89'

11. LTRIM('๋ฌธ์ž์—ด') : ์™ผ์ชฝ ๊ณต๋ฐฑ ์ œ๊ฑฐ

SELECT LTRIM('   Hello World') AS left_trimmed_string;
-- ๊ฒฐ๊ณผ: 'Hello World'

12. RTRIM('๋ฌธ์ž์—ด') : ์˜ค๋ฅธ์ชฝ ๊ณต๋ฐฑ ์ œ๊ฑฐ

SELECT RTRIM('Hello World   ') AS right_trimmed_string;
-- ๊ฒฐ๊ณผ: 'Hello World'

13. TRIM('๋ฌธ์ž์—ด') : ์–‘์ชฝ ๊ณต๋ฐฑ ์ œ๊ฑฐ

SELECT TRIM('   Hello World   ') AS trimmed_string;
-- ๊ฒฐ๊ณผ: 'Hello World'

14. LCASE('๋ฌธ์ž์—ด') ๋˜๋Š” LOWER('๋ฌธ์ž์—ด') : ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜

SELECT LOWER('HELLO WORLD') AS lower_case_string;
-- ๊ฒฐ๊ณผ: 'hello world'

15. UCASE('๋ฌธ์ž์—ด') ๋˜๋Š” UPPER('๋ฌธ์ž์—ด') : ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜

SELECT UPPER('hello world') AS upper_case_string;
-- ๊ฒฐ๊ณผ: 'HELLO WORLD'

16. LEFT('๋ฌธ์ž์—ด', ๊ฐœ์ˆ˜) : ์™ผ์ชฝ์—์„œ ์ง€์ •๋œ ๊ฐœ์ˆ˜๋งŒํผ ๋ฌธ์ž์—ด ์ถ”์ถœ

SELECT LEFT('Hyundai Softeer', 7) AS left_string;
-- ๊ฒฐ๊ณผ: 'Hyundai'

17. RIGHT('๋ฌธ์ž์—ด', ๊ฐœ์ˆ˜) : ์˜ค๋ฅธ์ชฝ์—์„œ ์ง€์ •๋œ ๊ฐœ์ˆ˜๋งŒํผ ๋ฌธ์ž์—ด ์ถ”์ถœ

SELECT RIGHT('Hyundai Softeer', 7) AS right_string;
-- ๊ฒฐ๊ณผ: 'Softeer'

18. REVERSE('๋ฌธ์ž์—ด') : ๋ฌธ์ž์—ด์„ ๋’ค์ง‘๊ธฐ

SELECT REVERSE('Hyundai Softeer') AS reversed_string;
-- ๊ฒฐ๊ณผ: 'reetfoS iadnyuH'

19. LENGTH('๋ฌธ์ž์—ด') : ๋ฌธ์ž์—ด์˜ ๋ฐ”์ดํŠธ ๊ธธ์ด ๊ตฌํ•˜๊ธฐ

SELECT LENGTH('Hello') AS length_string;
-- ๊ฒฐ๊ณผ: 5

SELECT LENGTH('ํ•œ๊ธ€') AS length_korean_string;
-- ๊ฒฐ๊ณผ: 6 (ํ•œ๊ธ€์€ UTF-8์—์„œ 3๋ฐ”์ดํŠธ)

20. BIT_LENGTH('๋ฌธ์ž์—ด') : ๋ฌธ์ž์—ด์˜ ๋น„ํŠธ ๊ธธ์ด ๊ตฌํ•˜๊ธฐ

SELECT BIT_LENGTH('Hello') AS bit_length_string;
-- ๊ฒฐ๊ณผ: 40 (5 characters * 8 bits)

21. CHAR_LENGTH('๋ฌธ์ž์—ด') : ๋ฌธ์ž์—ด์˜ ๋ฌธ์ž ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ

SELECT CHAR_LENGTH('Hello') AS char_length_string;
-- ๊ฒฐ๊ณผ: 5

SELECT CHAR_LENGTH('ํ•œ๊ธ€') AS char_length_korean_string;
-- ๊ฒฐ๊ณผ: 2 (ํ•œ๊ธ€์€ 2๊ฐœ์˜ ๋ฌธ์ž)

๐Ÿ“… ๋‚ ์งœ ๊ด€๋ จ ํ•จ์ˆ˜

1. NOW() : ํ˜„์žฌ ๋‚ ์งœ์™€ ์‹œ๊ฐ„ ๋ฐ˜ํ™˜

SELECT NOW() AS current_datetime;
-- ๊ฒฐ๊ณผ: '2024-09-10 14:23:56'

2. SYSDATE() : ์‹œ์Šคํ…œ ๋‚ ์งœ์™€ ์‹œ๊ฐ„ ๋ฐ˜ํ™˜

SELECT SYSDATE() AS system_date_time;
-- ๊ฒฐ๊ณผ: '2024-09-10 14:23:56'

3. CURRENT_TIMESTAMP() : ํ˜„์žฌ ํƒ€์ž„์Šคํƒฌํ”„ ๋ฐ˜ํ™˜

SELECT CURRENT_TIMESTAMP() AS current_timestamp;
-- ๊ฒฐ๊ณผ: '2024-09-10 14:23:56'

4. CURDATE() ๋˜๋Š” CURRENT_DATE() : ํ˜„์žฌ ๋‚ ์งœ ๋ฐ˜ํ™˜

SELECT CURDATE() AS current_date;
-- ๊ฒฐ๊ณผ: '2024-09-10'

5. CURTIME() ๋˜๋Š” CURRENT_TIME() : ํ˜„์žฌ ์‹œ๊ฐ„ ๋ฐ˜ํ™˜

SELECT CURTIME() AS current_time;
-- ๊ฒฐ๊ณผ: '14:23:56'

6. DATE_ADD(๋‚ ์งœ, INTERVAL ๊ธฐ์ค€ ๊ฐ’) : ๋‚ ์งœ์— ํŠน์ • ๊ธฐ๊ฐ„ ๋”ํ•˜๊ธฐ

SELECT DATE_ADD('2024-09-10', INTERVAL 5 DAY) AS added_date;
-- ๊ฒฐ๊ณผ: '2024-09-15'

7. ADDDATE(๋‚ ์งœ, INTERVAL ๊ธฐ์ค€ ๊ฐ’) : ๋‚ ์งœ์— ๊ธฐ๊ฐ„ ๋”ํ•˜๊ธฐ

SELECT ADDDATE('2024-09-10', INTERVAL 1 MONTH) AS added_date;
-- ๊ฒฐ๊ณผ: '2024-10-10'

8. DATE_SUB(๋‚ ์งœ, INTERVAL ๊ธฐ์ค€ ๊ฐ’) : ๋‚ ์งœ์—์„œ ํŠน์ • ๊ธฐ๊ฐ„ ๋นผ๊ธฐ

SELECT DATE_SUB('2024-09-10', INTERVAL 7 DAY) AS subtracted_date;
-- ๊ฒฐ๊ณผ: '2024-09-03'

9. SUBDATE(๋‚ ์งœ, INTERVAL ๊ธฐ์ค€ ๊ฐ’) : ํŠน์ • ๋‚ ์งœ์—์„œ ๊ธฐ๊ฐ„ ๋นผ๊ธฐ

SELECT SUBDATE('2024-09-10', INTERVAL 1 MONTH) AS subtracted_date;
-- ๊ฒฐ๊ณผ: '2024-08-10'

10. YEAR(๋‚ ์งœ) : ๋‚ ์งœ์—์„œ ์—ฐ๋„๋งŒ ๋ฐ˜ํ™˜

SELECT YEAR('2024-09-10') AS year_value;
-- ๊ฒฐ๊ณผ: 2024

11. MONTH(๋‚ ์งœ) : ๋‚ ์งœ์—์„œ ์›”๋งŒ ๋ฐ˜ํ™˜

SELECT MONTH('2024-09-10') AS month_value;
-- ๊ฒฐ๊ณผ: 9

12. MONTHNAME(๋‚ ์งœ) : ๋‚ ์งœ์—์„œ ์›”์˜ ์ด๋ฆ„ ๋ฐ˜ํ™˜

SELECT MONTHNAME('2024-09-10') AS month_name;
-- ๊ฒฐ๊ณผ: 'September'

13. DAYNAME(๋‚ ์งœ) : ๋‚ ์งœ์—์„œ ์š”์ผ ์ด๋ฆ„ ๋ฐ˜ํ™˜

SELECT DAYNAME('2024-09-10') AS day_name;
-- ๊ฒฐ๊ณผ: 'Tuesday'

14. DAYOFMONTH(๋‚ ์งœ) : ๋‚ ์งœ์—์„œ ํ•ด๋‹น ์ผ(day)๋งŒ ๋ฐ˜ํ™˜

SELECT DAYOFMONTH('2024-09-10') AS day_of_month;
-- ๊ฒฐ๊ณผ: 10

15. DAYOFWEEK(๋‚ ์งœ) : ๋‚ ์งœ์˜ ์š”์ผ์„ ์ˆซ์ž๋กœ ๋ฐ˜ํ™˜ (1: ์ผ์š”์ผ, 7: ํ† ์š”์ผ)

SELECT DAYOFWEEK('2024-09-10') AS day_of_week;
-- ๊ฒฐ๊ณผ: 3 (ํ™”์š”์ผ)

16. WEEKDAY(๋‚ ์งœ) : ๋‚ ์งœ์˜ ์š”์ผ์„ ์ˆซ์ž๋กœ ๋ฐ˜ํ™˜ (0: ์›”์š”์ผ, 6: ์ผ์š”์ผ)

SELECT WEEKDAY('2024-09-10') AS week_day;
-- ๊ฒฐ๊ณผ: 1 (ํ™”์š”์ผ)

17. DAYOFYEAR(๋‚ ์งœ) : ๋‚ ์งœ๊ฐ€ ํ•ด๋‹น ์—ฐ๋„์˜ ๋ช‡ ๋ฒˆ์งธ ๋‚ ์ธ์ง€ ๋ฐ˜ํ™˜

SELECT DAYOFYEAR('2024-09-10') AS day_of_year;
-- ๊ฒฐ๊ณผ: 254

18. WEEK(๋‚ ์งœ) : ๋‚ ์งœ์˜ ์ฃผ์ฐจ ๋ฐ˜ํ™˜

SELECT WEEK('2024-09-10') AS week_number;
-- ๊ฒฐ๊ณผ: 36

19. FROM_DAYS(๋‚ ์ˆ˜) : ์ฃผ์–ด์ง„ ์ผ์ˆ˜๋ฅผ ๋‚ ์งœ๋กœ ๋ณ€ํ™˜

SELECT FROM_DAYS(738000) AS date_from_days;
-- ๊ฒฐ๊ณผ: '2021-09-29'

20. TO_DAYS(๋‚ ์งœ) : ๋‚ ์งœ๋ฅผ ์ผ์ˆ˜๋กœ ๋ณ€ํ™˜

SELECT TO_DAYS('2024-09-10') AS days_value;
-- ๊ฒฐ๊ณผ: 739320

21. LAST_DAY(๋‚ ์งœ) : ํ•ด๋‹น ๋‚ ์งœ์˜ ๋‹ฌ ๋งˆ์ง€๋ง‰ ๋‚  ๋ฐ˜ํ™˜

SELECT LAST_DAY('2024-09-10') AS last_day_of_month;
-- ๊ฒฐ๊ณผ: '2024-09-30'

22. DATE_FORMAT(๋‚ ์งœ, 'ํ˜•์‹') : ๋‚ ์งœ๋ฅผ ์ง€์ •ํ•œ ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜

SELECT DATE_FORMAT('2024-09-10', '%Y-%m-%d') AS formatted_date;
-- ๊ฒฐ๊ณผ: '2024-09-10'

23. DATEDIFF(๋‚ ์งœ1, ๋‚ ์งœ2) : ๋‘ ๋‚ ์งœ ์‚ฌ์ด์˜ ์ฐจ์ด ๊ณ„์‚ฐ

SELECT DATEDIFF('2024-09-15', '2024-09-10') AS date_difference;
-- ๊ฒฐ๊ณผ: 5

24. TIMEDIFF(์‹œ๊ฐ„1, ์‹œ๊ฐ„2) : ๋‘ ์‹œ๊ฐ„์˜ ์ฐจ์ด ๊ณ„์‚ฐ

SELECT TIMEDIFF('12:00:00', '08:00:00') AS time_difference;
-- ๊ฒฐ๊ณผ: '04:00:00'

๐Ÿ” ๋…ผ๋ฆฌ ๊ด€๋ จ ํ•จ์ˆ˜

๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ด๋‚˜ ์กฐ๊ฑด ์ฒ˜๋ฆฌ์—์„œ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋“ค๋„ ์•Œ์•„๋ณผ๊ฒŒ์š”!

1. IF(๋…ผ๋ฆฌ์‹, ๊ฐ’1, ๊ฐ’2) : ๋…ผ๋ฆฌ์‹์ด ์ฐธ์ด๋ฉด ๊ฐ’1, ๊ฑฐ์ง“์ด๋ฉด ๊ฐ’2 ๋ฐ˜ํ™˜

SELECT IF(1 > 0, '์ฐธ', '๊ฑฐ์ง“') AS result;
-- ๊ฒฐ๊ณผ: '์ฐธ'

2. IFNULL(๊ฐ’1, ๊ฐ’2) : ๊ฐ’1์ด NULL์ด๋ฉด ๊ฐ’2๋กœ ๋Œ€์ฒด, NULL์ด ์•„๋‹ˆ๋ฉด ๊ฐ’1 ๋ฐ˜ํ™˜

SELECT IFNULL(NULL, '๋Œ€์ฒด ๊ฐ’') AS result;
-- ๊ฒฐ๊ณผ: '๋Œ€์ฒด ๊ฐ’'

3. NULLIF(๊ฐ’1, ๊ฐ’2) : ๊ฐ’1์ด ๊ฐ’2์™€ ๊ฐ™์œผ๋ฉด NULL ๋ฐ˜ํ™˜, ๋‹ค๋ฅด๋ฉด ๊ฐ’1 ๋ฐ˜ํ™˜

SELECT NULLIF(5, 5) AS result;
-- ๊ฒฐ๊ณผ: NULL

๐Ÿ“Š ์ง‘๊ณ„ ํ•จ์ˆ˜(Aggregate Function)

์ง‘๊ณ„ ํ•จ์ˆ˜๋Š” ์—ฌ๋Ÿฌ ํ–‰์„ ๋ฌถ์–ด ์š”์•ฝํ•˜๊ณ  ํ†ต๊ณ„๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•ด์š”.

1. COUNT(ํ•„๋“œ๋ช…) : NULL์ด ์•„๋‹Œ ๋ ˆ์ฝ”๋“œ ์ˆ˜ ๋ฐ˜ํ™˜

SELECT COUNT(column_name) AS count_value
FROM table_name;

2. SUM(ํ•„๋“œ๋ช…) : ํ•„๋“œ ๊ฐ’์˜ ํ•ฉ๊ณ„ ๋ฐ˜ํ™˜

SELECT SUM(column_name) AS total_sum
FROM table_name;

3. AVG(ํ•„๋“œ๋ช…) : ํ•„๋“œ ๊ฐ’์˜ ํ‰๊ท  ๋ฐ˜ํ™˜

SELECT AVG(column_name) AS average_value
FROM table_name;

4. MAX(ํ•„๋“œ๋ช…) : ํ•„๋“œ ๊ฐ’ ์ค‘ ์ตœ๋Œ€๊ฐ’ ๋ฐ˜ํ™˜

SELECT MAX(column_name) AS max_value
FROM table_name;

5. MIN(ํ•„๋“œ๋ช…) : ํ•„๋“œ ๊ฐ’ ์ค‘ ์ตœ์†Œ๊ฐ’ ๋ฐ˜ํ™˜

SELECT MIN(column_name) AS min_value
FROM table_name;

๐Ÿ“š GROUP BY์™€ HAVING ์ ˆ ๊ฐœ๋… ์ •๋ฆฌ

GROUP BY ์ ˆ์€ SQL์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ํŠน์ • ๊ธฐ์ค€์œผ๋กœ ๊ทธ๋ฃนํ™”ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฃนํ™”๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ง‘๊ณ„ ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜์—ฌ ์š”์•ฝ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
GROUP BY ์ ˆ์„ ์‚ฌ์šฉํ•  ๋•Œ, ํ•จ๊ป˜ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ์ง‘๊ณ„ ํ•จ์ˆ˜๋กœ๋Š” COUNT(), SUM(), AVG(), MAX(), MIN() ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

  • GROUP BY ์ ˆ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ:
    GROUP BY๋Š” ํŠน์ • ํ•„๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋ฃนํ™”ํ•˜์—ฌ, ๊ฐ ๊ทธ๋ฃน์— ๋Œ€ํ•ด ์ง‘๊ณ„ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์ง‘๊ณ„ ํ•จ์ˆ˜์˜ ์ ์šฉ:
    GROUP BY์™€ ํ•จ๊ป˜ ์ง‘๊ณ„ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ๊ฐ ๊ทธ๋ฃน์— ๋Œ€ํ•ด ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, COUNT() ํ•จ์ˆ˜๋Š” ๊ฐ ๊ทธ๋ฃน์— ์†ํ•œ ํ–‰์˜ ์ˆ˜๋ฅผ ์„ธ๊ณ , SUM() ํ•จ์ˆ˜๋Š” ๊ทธ๋ฃน ๋‚ด ์ˆซ์ž ๊ฐ’์˜ ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•ด์ค๋‹ˆ๋‹ค.

  • GROUP BY ์ ˆ์„ ์ƒ๋žตํ•˜๋Š” ๊ฒฝ์šฐ:
    GROUP BY ์ ˆ์„ ์ƒ๋žตํ•˜๋ฉด, ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์˜ ๊ทธ๋ฃน์œผ๋กœ ๊ฐ„์ฃผํ•˜์—ฌ ์ง‘๊ณ„ ํ•จ์ˆ˜๊ฐ€ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ์š”์•ฝํ•œ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

  • SELECT ์ ˆ์˜ ์กฐ๊ฑด:
    GROUP BY ์ ˆ์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š”, SELECT ์ ˆ์— ๋ฐ˜๋“œ์‹œ GROUP BY๋กœ ๋ฌถ์ธ ํ•„๋“œ์™€ ํ•จ๊ป˜ ์ง‘๊ณ„ ํ•จ์ˆ˜ ๋˜๋Š” ์ƒ์ˆ˜๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์ด์ง€ ์•Š์€ ํ•„๋“œ๋ฅผ SELECT ์ ˆ์— ํฌํ•จํ•˜๋ ค๋ฉด, ๋ฐ˜๋“œ์‹œ ๊ทธ ํ•„๋“œ์— ์ง‘๊ณ„ ํ•จ์ˆ˜๊ฐ€ ์ ์šฉ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.


1. GROUP BY ์—†์ด ์ง‘๊ณ„ ํ•จ์ˆ˜ ์‚ฌ์šฉ

์˜ˆ๋ฅผ ๋“ค์–ด, ํ…Œ์ด๋ธ” employees๊ฐ€ ์žˆ๊ณ , ๊ฐ ํ–‰์—๋Š” ์ง์›์˜ ๋ถ€์„œ์™€ ๊ธ‰์—ฌ ์ •๋ณด๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์ „์ฒด ์ง์›์˜ ๊ธ‰์—ฌ ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š” GROUP BY๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ง‘๊ณ„ ํ•จ์ˆ˜ SUM()์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

SELECT SUM(salary) AS total_salary
FROM employees;
-- ๊ฒฐ๊ณผ: ๋ชจ๋“  ์ง์›์˜ ๊ธ‰์—ฌ ํ•ฉ๊ณ„

์ด ์ฟผ๋ฆฌ๋Š” GROUP BY ์—†์ด ์ „์ฒด ํ…Œ์ด๋ธ”์˜ ๊ธ‰์—ฌ ํ•ฉ๊ณ„๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.


2. GROUP BY ์‚ฌ์šฉ

๋‹ค์Œ์€ ๋ถ€์„œ๋ณ„๋กœ ๊ธ‰์—ฌ ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. ๊ฐ ๋ถ€์„œ์— ์†ํ•œ ์ง์›๋“ค์˜ ๊ธ‰์—ฌ ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด GROUP BY department๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

SELECT department, SUM(salary) AS total_salary
FROM employees
GROUP BY department;
-- ๊ฒฐ๊ณผ: ๊ฐ ๋ถ€์„œ๋ณ„ ๊ธ‰์—ฌ ํ•ฉ๊ณ„

์ด ์ฟผ๋ฆฌ๋Š” ๋ถ€์„œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ทธ๋ฃน์„ ๋ฌถ๊ณ , ๊ฐ ๋ถ€์„œ์— ์†ํ•œ ์ง์›๋“ค์˜ ๊ธ‰์—ฌ ํ•ฉ๊ณ„๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.


3. GROUP BY์™€ COUNT ์‚ฌ์šฉ

๋ถ€์„œ๋ณ„ ์ง์› ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š”, COUNT() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ๋ถ€์„œ์— ์†ํ•œ ์ง์› ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;
-- ๊ฒฐ๊ณผ: ๊ฐ ๋ถ€์„œ๋ณ„ ์ง์› ์ˆ˜

์ด ์ฟผ๋ฆฌ๋Š” ๊ฐ ๋ถ€์„œ์— ์†ํ•œ ์ง์› ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. COUNT(*)๋Š” ํ•ด๋‹น ๋ถ€์„œ์— ์†ํ•œ ํ–‰์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์–ด์ค๋‹ˆ๋‹ค.


4. GROUP BY์™€ ์—ฌ๋Ÿฌ ์ง‘๊ณ„ ํ•จ์ˆ˜ ์‚ฌ์šฉ

๋‹ค์Œ์€ ๋ถ€์„œ๋ณ„๋กœ ๊ธ‰์—ฌ์˜ ์ตœ๋Œ€๊ฐ’, ์ตœ์†Œ๊ฐ’, ํ‰๊ท ๊ฐ’์„ ๊ตฌํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. ํ•œ ๋ฒˆ์˜ ์ฟผ๋ฆฌ๋กœ ์—ฌ๋Ÿฌ ์ง‘๊ณ„ ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

SELECT department, MAX(salary) AS max_salary, MIN(salary) AS min_salary, AVG(salary) AS avg_salary
FROM employees
GROUP BY department;
-- ๊ฒฐ๊ณผ: ๊ฐ ๋ถ€์„œ๋ณ„ ์ตœ๋Œ€ ๊ธ‰์—ฌ, ์ตœ์†Œ ๊ธ‰์—ฌ, ํ‰๊ท  ๊ธ‰์—ฌ

์ด ์ฟผ๋ฆฌ๋Š” ๋ถ€์„œ๋ณ„๋กœ ์ตœ๋Œ€ ๊ธ‰์—ฌ, ์ตœ์†Œ ๊ธ‰์—ฌ, ํ‰๊ท  ๊ธ‰์—ฌ๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.


์˜ˆ์ œ: ํ•™์ƒ๋ณ„ ์ตœ๊ณ  ์ ์ˆ˜๋ฅผ ๊ธฐ๋กํ•œ ๊ณผ๋ชฉ ์กฐํšŒ

์ด๋ฒˆ์—๋Š” ํ•™์ƒ๋ณ„๋กœ ์ตœ๊ณ  ์ ์ˆ˜๋ฅผ ๊ธฐ๋กํ•œ ๊ณผ๋ชฉ์„ ์กฐํšŒํ•˜๋Š” ์˜ˆ์ œ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋จผ์ € ํ•™์ƒ๋“ค์˜ ์ ์ˆ˜๋ฅผ ์ €์žฅํ•œ ํ…Œ์ด๋ธ” scores์—์„œ ํ•™์ƒ๋ณ„๋กœ ์ตœ๊ณ  ์ ์ˆ˜๋ฅผ ๊ตฌํ•œ ํ›„, ํ•ด๋‹น ์ ์ˆ˜๋ฅผ ๋ฐ›์€ ๊ณผ๋ชฉ์„ ์กฐํšŒํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

SELECT a.student_id, s.student_name, s.subject, a.max_score
FROM scores s
JOIN (
    SELECT student_id, MAX(score) AS max_score
    FROM scores
    GROUP BY student_id
) a
ON s.student_id = a.student_id
AND s.score = a.max_score;

์ด ์ฟผ๋ฆฌ๋Š” ํ•™์ƒ๋ณ„๋กœ ๊ฐ€์žฅ ๋†’์€ ์ ์ˆ˜๋ฅผ ๊ธฐ๋กํ•œ ๊ณผ๋ชฉ๊ณผ ๊ทธ ์ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋‚ด๋ถ€์˜ GROUP BY student_id๋กœ ํ•™์ƒ๋ณ„๋กœ ์ตœ๊ณ  ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ ํ›„, ์™ธ๋ถ€ ์ฟผ๋ฆฌ์—์„œ ํ•ด๋‹น ์ ์ˆ˜์— ํ•ด๋‹นํ•˜๋Š” ๊ณผ๋ชฉ์„ ์ฐพ์•„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.


์˜ˆ์ œ: ํ•™์ƒ๋ณ„๋กœ ๊ฐ€์žฅ ๋†’์€ ์ ์ˆ˜๋ฅผ ๋ฐ›์€ ๊ณผ๋ชฉ ์กฐํšŒ

๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ํ•™์ƒ๋ณ„ ์ตœ๊ณ  ์ ์ˆ˜๋ฅผ ๋ฐ›์€ ๊ณผ๋ชฉ์„ ์กฐํšŒํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

SELECT student_id, subject, score
FROM scores
WHERE (student_id, score) IN (
    SELECT student_id, MAX(score)
    FROM scores
    GROUP BY student_id
)
ORDER BY student_id;

์ด ์ฟผ๋ฆฌ์—์„œ๋Š” WHERE ์ ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ตœ๊ณ  ์ ์ˆ˜๋ฅผ ๋ฐ›์€ ํ•™์ƒ๋“ค์˜ ์ •๋ณด๋ฅผ ์กฐํšŒํ•œ ํ›„, ๊ฐ ํ•™์ƒ์˜ ์ตœ๊ณ  ์ ์ˆ˜์— ํ•ด๋‹นํ•˜๋Š” ๊ณผ๋ชฉ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.


๐Ÿ“š GROUP BY์™€ HAVING ์ ˆ ๊ฐœ๋… ์ •๋ฆฌ

  • HAVING ์ ˆ ์‚ฌ์šฉ:
    GROUP BY ์ ˆ๋กœ ๊ทธ๋ฃนํ™”ํ•œ ๊ฒฐ๊ณผ์— ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•  ๋•Œ๋Š” HAVING ์ ˆ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. HAVING ์ ˆ์€ ๊ทธ๋ฃนํ™”๋œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ์กฐ๊ฑด์„ ํ•„ํ„ฐ๋งํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.

  • WHERE ์ ˆ๊ณผ HAVING ์ ˆ์˜ ์ฐจ์ด:
    WHERE ์ ˆ์€ ๊ทธ๋ฃนํ™”๋˜๊ธฐ ์ „์— ๊ฐœ๋ณ„ ํ–‰์— ์กฐ๊ฑด์„ ์ ์šฉํ•˜์—ฌ ํ•„ํ„ฐ๋งํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, HAVING ์ ˆ์€ ๊ทธ๋ฃนํ™”๋œ ํ›„ ์ง‘๊ณ„๋œ ๊ฒฐ๊ณผ์— ์กฐ๊ฑด์„ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ง‘๊ณ„ ํ•จ์ˆ˜(SUM, AVG, COUNT ๋“ฑ)๊ฐ€ ์‚ฌ์šฉ๋œ ์กฐ๊ฑด์€ ๋ฐ˜๋“œ์‹œ HAVING ์ ˆ์— ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  • Query ์‹คํ–‰ ์ˆœ์„œ:
    SQL ์ฟผ๋ฆฌ ์‹คํ–‰ ์ˆœ์„œ๋ฅผ ๋ณด๋ฉด GROUP BY ์ ˆ์€ WHERE ์ ˆ ์ดํ›„์— ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ GROUP BY๋กœ ๊ทธ๋ฃนํ™”๋œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ํ•„ํ„ฐ๋ง์€ WHERE ์ ˆ์ด ์•„๋‹Œ HAVING ์ ˆ์—์„œ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ์ง‘๊ณ„๋œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์กฐ๊ฑด์€ HAVING ์ ˆ์„ ํ†ตํ•ด ์ฒ˜๋ฆฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.


HAVING ์ ˆ ์‚ฌ์šฉ

HAVING ์ ˆ์€ ๊ทธ๋ฃนํ™”๋œ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด ์กฐ๊ฑด์„ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค.
์ด๋Š” ๊ทธ๋ฃนํ™” ์ด์ „์— ์กฐ๊ฑด์„ ๊ฑฐ๋Š” WHERE ์ ˆ๊ณผ ์ฐจ์ด์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ 1: GROUP BY์™€ HAVING ์‚ฌ์šฉ

๋‹ค์Œ์€ ํ•™์ƒ๋ณ„๋กœ ์‹œํ—˜์—์„œ ํ‰๊ท  ์ ์ˆ˜๊ฐ€ 70์  ์ด์ƒ์ธ ํ•™์ƒ์˜ ์ •๋ณด๋ฅผ ์กฐํšŒํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

SELECT student_id, AVG(score) AS average_score
FROM scores
GROUP BY student_id
HAVING AVG(score) >= 70;
  • ์„ค๋ช…:
    • GROUP BY student_id๋กœ ํ•™์ƒ๋ณ„ ๊ทธ๋ฃน์„ ๋ฌถ๊ณ , ๊ฐ ํ•™์ƒ์˜ ํ‰๊ท  ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
    • HAVING AVG(score) >= 70์„ ํ†ตํ•ด ํ‰๊ท  ์ ์ˆ˜๊ฐ€ 70์  ์ด์ƒ์ธ ํ•™์ƒ๋งŒ ํ•„ํ„ฐ๋งํ•˜์—ฌ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.

WHERE ์ ˆ๊ณผ HAVING ์ ˆ์˜ ์ฐจ์ด

WHERE ์ ˆ์€ ๊ทธ๋ฃนํ™” ์ด์ „์— ์กฐ๊ฑด์„ ๊ฑธ๊ณ ,
HAVING ์ ˆ์€ ๊ทธ๋ฃนํ™” ์ดํ›„์— ์กฐ๊ฑด์„ ๊ฑธ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์˜ˆ์ œ 2: WHERE์™€ HAVING ๋น„๊ต

๋‹ค์Œ์€ 100์  ๋งŒ์ ์—์„œ 50์  ์ด์ƒ์„ ๋ฐ›์€ ํ•™์ƒ๋“ค ์ค‘์—์„œ ํ‰๊ท  ์ ์ˆ˜๊ฐ€ 70์  ์ด์ƒ์ธ ํ•™์ƒ๋“ค์„ ์กฐํšŒํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

SELECT student_id, AVG(score) AS average_score
FROM scores
WHERE score >= 50
GROUP BY student_id
HAVING AVG(score) >= 70;
  • ์„ค๋ช…:
    • WHERE score >= 50์—์„œ ๊ฐ ํ•™์ƒ์˜ ๊ฐœ๋ณ„ ์‹œํ—˜ ์ ์ˆ˜๊ฐ€ 50์  ์ด์ƒ์ธ ๊ฒฝ์šฐ๋งŒ ํ•„ํ„ฐ๋ง๋ฉ๋‹ˆ๋‹ค.
    • GROUP BY student_id๋กœ ํ•™์ƒ๋ณ„๋กœ ๊ทธ๋ฃน์„ ๋‚˜๋ˆˆ ํ›„, HAVING AVG(score) >= 70์„ ํ†ตํ•ด ํ‰๊ท  ์ ์ˆ˜๊ฐ€ 70์  ์ด์ƒ์ธ ํ•™์ƒ๋งŒ ์กฐํšŒ๋ฉ๋‹ˆ๋‹ค.

์ด ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด, WHERE ์ ˆ๊ณผ HAVING ์ ˆ์ด ์ ์šฉ๋˜๋Š” ์‹œ์ ๊ณผ ๊ทธ ์ฐจ์ด์ ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์ฆ‰, WHERE๋Š” ๊ฐœ๋ณ„ ํ–‰์— ์กฐ๊ฑด์„ ๊ฑธ๊ณ , HAVING์€ ๊ทธ๋ฃนํ™”๋œ ๊ฒฐ๊ณผ์— ์กฐ๊ฑด์„ ์ ์šฉํ•ด์š”!


์ด๋ ‡๊ฒŒ GROUP BY์™€ HAVING ์ ˆ์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด, ๋ฐ์ดํ„ฐ๋ฅผ ๋”์šฑ ์ •๊ตํ•˜๊ฒŒ ํ•„ํ„ฐ๋งํ•˜๊ณ  ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€