Notice
Recent Posts
Recent Comments
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
Archives
Today
In Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

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

[52] 230314 SQL Ch 3. ์ •๋ ฌ๊ณผ ์—ฐ์‚ฐ: 9๊ฐ• ์ •๋ ฌ - ORDER BY, 10๊ฐ• ๋ณต์ˆ˜์˜ ์—ด์„ ์ง€์ •ํ•ด ์ •๋ ฌํ•˜๊ธฐ, 11๊ฐ• ๊ฒฐ๊ณผ ํ–‰ ์ œํ•œํ•˜๊ธฐ - LIMIT, 12๊ฐ• ์ˆ˜์น˜ ์—ฐ์‚ฐ, 13๊ฐ• ๋ฌธ์ž์—ด ์—ฐ์‚ฐ, 14๊ฐ• ๋‚ ์งœ ์—ฐ์‚ฐ, 15 CASE ๋ฌธ์œผ๋กœ ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜ํ•˜.. ๋ณธ๋ฌธ

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

[52] 230314 SQL Ch 3. ์ •๋ ฌ๊ณผ ์—ฐ์‚ฐ: 9๊ฐ• ์ •๋ ฌ - ORDER BY, 10๊ฐ• ๋ณต์ˆ˜์˜ ์—ด์„ ์ง€์ •ํ•ด ์ •๋ ฌํ•˜๊ธฐ, 11๊ฐ• ๊ฒฐ๊ณผ ํ–‰ ์ œํ•œํ•˜๊ธฐ - LIMIT, 12๊ฐ• ์ˆ˜์น˜ ์—ฐ์‚ฐ, 13๊ฐ• ๋ฌธ์ž์—ด ์—ฐ์‚ฐ, 14๊ฐ• ๋‚ ์งœ ์—ฐ์‚ฐ, 15 CASE ๋ฌธ์œผ๋กœ ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜ํ•˜..

yjyuwisely 2023. 3. 14. 09:50

230314 Tue 52nd class

Ch 3. ์ •๋ ฌ๊ณผ ์—ฐ์‚ฐ
์ง„๋„: p. 94 ~ (๊ต์žฌ: 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

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


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

- ์šฉ์–ด

In computing, a client is a piece of computer hardware or software that accesses a service made available by a server as part of the client–server model of computer networks.

์ปดํ“จํ„ฐ ๋„คํŠธ์›Œํฌ์—์„œ ํด๋ผ์ด์–ธํŠธ(client, ๋ฌธํ™”์–ด: ์˜๋ขฐ๊ธฐ)๋Š” ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•˜์—ฌ ์„œ๋ฒ„๋ผ๋Š” ๋‹ค๋ฅธ ์ปดํ“จํ„ฐ ์‹œ์Šคํ…œ ์ƒ์˜ ์›๊ฒฉ ์„œ๋น„์Šค์— ์ ‘์†ํ•  ์ˆ˜ ์žˆ๋Š” ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์ด๋‚˜ ์„œ๋น„์Šค๋ฅผ ๋งํ•œ๋‹ค.

์ฟผ๋ฆฌ๋Š” ์›น ์„œ๋ฒ„์— ํŠน์ •ํ•œ ์ •๋ณด๋ฅผ ๋ณด์—ฌ๋‹ฌ๋ผ๋Š” ์›น ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ(์ฃผ๋กœ ๋ฌธ์ž์—ด์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ์š”์ฒญ์ด๋‹ค)์— ์˜ํ•œ ์ฒ˜๋ฆฌ์ด๋‹ค. 

A query in a database is a request for information from a database management system (DBMS), which is the software program that maintains data. Users can make a query to retrieve data or change information in a database, such as adding or removing data.


- ํ•จ์ˆ˜
-
decimal(10,2) means you can have a decimal number with a maximal total precision of 10 digits. 2 of them after the decimal point and 8 before. (10,2) defines the precision of the decimal value. 10 digits and 2 digits after the comma.

- Point: SELECT ๊ตฌ์—์„œ ์ง€์ •ํ•œ ๋ณ„๋ช…์€ WHERE ๊ตฌ ์•ˆ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. p. 122

- Point: ORDER BY ๊ตฌ์—์„œ๋Š” SELECT ๊ตฌ์—์„œ ์ง€์ •ํ•œ ๋ณ„๋ช…์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. p. 125


- The IFNULL() function returns a specified value if the expression is NULL. If the expression is NOT NULL, this function returns the expression.

SELECT IFNULL(NULL, 500);
-- 500

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

- The 
COALESCE() function returns the first non-null value in a list.

COALESCE(val1, val2, ...., val_n)
SELECT COALESCE(NULL, 1, 2, 'W3Schools.com');
-- 1

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

CAOLESCE ์˜ˆ์‹œ


๋‹ค์‹œ ๊ธฐ์–ตํ•˜๊ธฐ

2023.03.13 - [๐ŸŒณK-Digital Revision 2023โœจ/SQL, MariaDB] - [51] 230313 [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 51์ผ] Ch. 2 ํ…Œ์ด๋ธ”์—์„œ ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰: 4๊ฐ• Hello World ์‹คํ–‰ํ•˜๊ธฐ, 5๊ฐ• ํ…Œ์ด๋ธ” ๊ตฌ์กฐ ์ฐธ์กฐํ•˜๊ธฐ, 6๊ฐ• ๊ฒ€์ƒ‰ ์กฐ๊ฑด ์ง€์ •ํ•˜๊ธฐ, 7๊ฐ• ์กฐ๊ฑด ์กฐํ•ฉํ•˜๊ธฐ, 8๊ฐ• ํŒจํ„ด ๋งค์นญ์— ์˜ํ•œ ๊ฒ€์ƒ‰ (LIKE)

ํ…Œ์ด๋ธ” ๊ตฌ์กฐ ์ฐธ์กฐํ•˜๊ธฐ (DESC = DESCRIBE) 

DESC ๋ช…๋ น: ํ…Œ์ด๋ธ”์˜ ๊ตฌ์กฐ๋ฅผ ์ฐธ์กฐํ•œ๋‹ค. 
์ฐธ์กฐ: ์ฐธ๊ณ ๋กœ ๋น„๊ตํ•˜๊ณ  ๋Œ€์กฐํ•˜์—ฌ ๋ณด๋‹ค.

The DESC command is used to sort the data returned in descending order.
The DESC command is a short form of the DESCRIBE command.

DESC ํ…Œ์ด๋ธ”๋ช…

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

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

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

 FROM
: ์กฐํšŒ ํ…Œ์ด๋ธ” ํ™•์ธ 

 WHERE: ๋ฐ์ดํ„ฐ ์ถ”์ถœ ์กฐ๊ฑด ํ™•์ธ
 GROUP BY: ๊ทธ๋ฃนํ•‘
 HAVING: ๊ทธ๋ฃนํ•œ ํ›„ ์กฐ๊ฑด
 SELECT: ๋ฐ์ดํ„ฐ ์ถ”์ถœ
 ORDER BY: ๋ฐ์ดํ„ฐ ์ˆœ์„œ ์ •๋ ฌ 

Ch 3. ์ •๋ ฌ๊ณผ ์—ฐ์‚ฐ

์ฑ…์˜ ๋ชฉ์ฐจ
__09๊ฐ•__์ •๋ ฌ - ORDER BY
1. ORDER BY๋กœ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ์ •๋ ฌํ•˜๊ธฐ
2. ORDER BY DESC๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ
3. ๋Œ€์†Œ๊ด€๊ณ„
4. ORDER BY๋Š” ํ…Œ์ด๋ธ”์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๋Š”๋‹ค.

__10๊ฐ•__๋ณต์ˆ˜์˜ ์—ด์„ ์ง€์ •ํ•ด ์ •๋ ฌํ•˜๊ธฐ
1. ๋ณต์ˆ˜ ์—ด๋กœ ์ •๋ ฌ ์ง€์ •
2. ์ •๋ ฌ๋ฐฉ๋ฒ• ์ง€์ •ํ•˜๊ธฐ
3. NULL ๊ฐ’์˜ ์ •๋ ฌ ์ˆœ์„œ

__11๊ฐ•__๊ฒฐ๊ณผ ํ–‰ ์ œํ•œํ•˜๊ธฐ - LIMIT
1. ํ–‰์ˆ˜ ์ œํ•œ
2. ์˜คํ”„์…‹ ์ง€์ •

__12๊ฐ•__์ˆ˜์น˜ ์—ฐ์‚ฐ
1. ์‚ฌ์น™ ์—ฐ์‚ฐ
2. SELECT ๊ตฌ๋กœ ์—ฐ์‚ฐํ•˜๊ธฐ
3. ์—ด์˜ ๋ณ„๋ช…
4. WHERE ๊ตฌ์—์„œ ์—ฐ์‚ฐํ•˜๊ธฐ
5. NULL ๊ฐ’์˜ ์—ฐ์‚ฐ
6. ORDER BY ๊ตฌ์—์„œ ์—ฐ์‚ฐํ•˜๊ธฐ
7. ํ•จ์ˆ˜
8. ROUND ํ•จ์ˆ˜

__13๊ฐ•__๋ฌธ์ž์—ด ์—ฐ์‚ฐ
1. ๋ฌธ์ž์—ด ๊ฒฐํ•ฉ
2. SUBSTRING ํ•จ์ˆ˜
3. TRIM ํ•จ์ˆ˜
4. CHARACTER_LENGTH ํ•จ์ˆ˜

__14๊ฐ•__๋‚ ์งœ ์—ฐ์‚ฐ
1. SQL์—์„œ์˜ ๋‚ ์งœ
2. ๋‚ ์งœ์˜ ๋ง์…ˆ๊ณผ ๋บ„์…ˆ

__15๊ฐ•__CASE ๋ฌธ์œผ๋กœ ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜ํ•˜๊ธฐ
1. CASE ๋ฌธ

2. ๋˜ ํ•˜๋‚˜์˜ CASE ๋ฌธ
3. CASE๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์ฃผ์˜์‚ฌํ•ญ

9๊ฐ• ์ •๋ ฌ - ORDER BY

ORDER BY ๊ตฌ

SELECT ์—ด๋ช… FROM ํ…Œ์ด๋ธ”๋ช… WHERE ์กฐ๊ฑด์‹ ORDER BY ์—ด๋ช…
์ •๋ ฌ
๋ฐ์ดํ„ฐ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ ๋˜๋Š” ๋‚ด๋ฆผ์ฐจ์ˆœ ๋ฐ์ดํ„ฐ๋ฅผ ์žฌ๋ฐฐ์น˜

์˜ค๋ฆ„์ฐจ์ˆœ: a-z, 1-9, ใ„ฑ-ใ…Ž
๋‚ด๋ฆผ์ฐจ์ˆœ: z-a, 9-1, ใ…Ž-ใ„ฑ

9.1 ORDER BY๋กœ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ์ •๋ ฌํ•˜๊ธฐ

WHERE ๊ตฌ ๋’ค์— ORDER BY ๊ตฌ๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ฒฝ์šฐ 
์ˆœ์„œ๋Š” ๋ฐ”๋€Œ์ง€ ์•Š๋Š”๋‹ค.

SELECT ์—ด๋ช… FROM ํ…Œ์ด๋ธ”๋ช… WHERE ์กฐ๊ฑด์‹ ORDER BY ์—ด๋ช…

 

FROM ๊ตฌ ๋’ค์— ORDER BY ๊ตฌ๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ฒฝ์šฐ (๊ฒ€์ƒ‰ ์กฐ๊ฑด์ด ํ•„์š” ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” WHERE ๊ตฌ ์ƒ๋žต)

SELECT ์—ด๋ช… FROM ํ…Œ์ด๋ธ”๋ช… ORDER BY ์—ด๋ช…

์“ฐ์ด๋Š” ๊ฒฝ์šฐ
ex) ์‡ผํ•‘๋ชฐ ์ฟ ํŒก: ์ฟ ํŒก ๋žญํ‚น์ˆœ | ๋‚ฎ์€ ๊ฐ€๊ฒฉ์ˆœ | ๋†’์€ ๊ฐ€๊ฒฉ์ˆœ | ํŒ๋งค๋Ÿ‰์ˆœ | ์ตœ์‹ ์ˆœ 


์˜ˆ์ œ) age ์—ด๋กœ ์ •๋ ฌํ•˜๊ธฐ

SELECT * FROM sample31;
-- age ์—ด์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ
SELECT * FROM sample31 ORDER BY age; -- ๋‚˜์ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ

์˜ˆ์ œ) address ์—ด๋กœ ์ •๋ ฌํ•˜๊ธฐ

SELECT * FROM sample31;
-- address ์—ด๋กœ ์ •๋ ฌํ•˜๊ธฐ
SELECT * FROM sample31 ORDER BY address; -- ์ฃผ์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ


9.2 ORDER BY DESC๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ

๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ (DESC = descendant(ํ•˜๊ฐ•))

SELECT ์—ด๋ช… FROM ํ…Œ์ด๋ธ”๋ช… ORDER BY ์—ด๋ช… DESC

์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ (ASC = ascendant(์ƒ์Šน))

SELECT ์—ด๋ช… FROM ํ…Œ์ด๋ธ”๋ช… ORDER BY ์—ด๋ช… ASC

Point: DESC๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค. 
ASC๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค. 

ORDER BY์˜ ๊ธฐ๋ณธ ์ •๋ ฌ๋ฐฉ๋ฒ•์€ ์˜ค๋ฆ„์ฐจ์ˆœ์ด๋‹ค. 

์˜ˆ์ œ) DESC์™€ ASC

-- age ์—ด์˜ ๊ฐ’์„ DESC๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ธฐ
SELECT * FROM sample31 ORDER BY age DESC;

-- age ์—ด์˜ ๊ฐ’์„ ASC๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ธฐ
SELECT * FROM sample31 ORDER BY age ASC;


9.3 ๋Œ€์†Œ๊ด€๊ณ„

๋‚ ์งœ์‹œ๊ฐ„ํ˜• ๋ฐ์ดํ„ฐ์˜ ๋Œ€์†Œ๊ด€๊ณ„
(์ž‘๋‹ค = ์ด์ „) 1999๋…„ < ... < 2013๋…„ < 2014๋…„ (ํฌ๋‹ค = ์ตœ๊ทผ)

Point: ๋ฌธ์ž์—ดํ˜• ๋ฐ์ดํ„ฐ์˜ ๋Œ€์†Œ๊ด€๊ณ„๋Š” ์‚ฌ์ „์‹ ์ˆœ์„œ์— ์˜ํ•ด ๊ฒฐ์ •๋œ๋‹ค. 
์˜ˆ์ œ) sample311 ํ…Œ์ด๋ธ”

-- sample311 ํ…Œ์ด๋ธ”์˜ ๋‚ด์šฉ ์ฐธ์กฐํ•˜๊ธฐ
SELECT * FROM sample311;

a ์—ด์ด ๋ฌธ์ž์—ดํ˜•(VARCHAR), b ์—ด์ด ์ˆ˜์น˜ํ˜•(INTEGER)์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ํ…Œ์ด๋ธ”์ด๋‹ค.

์˜ˆ์ œ) sample311์„ a ์—ด๋กœ ์ •๋ ฌํ•˜๊ธฐ

-- a ์—ด์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ
SELECT * FROM sample311 ORDER BY a;

a ์—ด์ด ๋ฌธ์ž์—ดํ˜•์œผ๋กœ ๋˜์–ด ์žˆ์–ด ๋Œ€์†Œ๊ด€๊ณ„๋ฅผ ์‚ฌ์ „์‹ ์ˆœ์„œ๋กœ ๋น„๊ตํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐœ์ƒํ•˜๋Š” ํ˜„์ƒ์ด๋‹ค.
1 < 10 < 11 < 2 (๋ฌธ์ž์—ดํ˜•)
b ์—ด๋กœ ์ •๋ ฌํ•ด๋ณด๋ฉด b ์—ด์€ ์ˆ˜์น˜ํ˜• ์—ด์ด๋ฏ€๋กœ ๊ฒฐ๊ณผ๊ฐ€ ์˜ˆ์ƒ๋Œ€๋กœ๋‹ค.

Point: ์ˆ˜์น˜ํ˜•๊ณผ ๋ฌธ์ž์—ดํ˜• ๋ฐ์ดํ„ฐ๋Š” ๋Œ€์†Œ๊ด€๊ณ„์˜ ๊ณ„์‚ฐ ๋ฐฉ๋ฒ•์ด ๋‹ค๋ฅด๋‹ค.


9.4 ORDER BY๋Š” ํ…Œ์ด๋ธ”์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๋Š”๋‹ค.

ORDER BY๋ฅผ ์ด์šฉํ•˜๋ฉด ์„œ๋ฒ„์—์„œ ํด๋ผ์ด์–ธํŠธ๋กœ ํ–‰ ์ˆœ์„œ๋ฅผ ๋ฐ”๊พธ์–ด ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ๋ฟ, ์ €์žฅ์žฅ์น˜์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ์˜ ํ–‰ ์ˆœ์„œ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค.

SELECT ๋ช…๋ น์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ๋ช…๋ น์ด๋‹ค. ์ด๋Š” ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐธ์กฐ๋งŒ ํ•  ๋ฟ์ด๋ฉฐ ๋ณ€๊ฒฝ์€ ํ•˜์ง€ ์•Š๋Š”๋‹ค.
์ฐธ์กฐ: ์ฐธ๊ณ ๋กœ ๋น„๊ตํ•˜๊ณ  ๋Œ€์กฐํ•˜์—ฌ ๋ณด๋‹ค.


10๊ฐ• ๋ณต์ˆ˜์˜ ์—ด์„ ์ง€์ •ํ•ด ์ •๋ ฌํ•˜๊ธฐ (ORDER BY) p. 102

-- ORDER BY ๊ตฌ
SELECT ์—ด๋ช… FROM ํ…Œ์ด๋ธ”๋ช… WHERE ์กฐ๊ฑด์‹
ORDER BY ์—ด๋ช…1 [ASC|DESC], ์—ด๋ช…2 [ASCDESC] ...

10.1 ๋ณต์ˆ˜ ์—ด๋กœ ์ •๋ ฌ ์ง€์ •

์˜ˆ์ œ) sample32 ํ…Œ์ด๋ธ”

-- sample32 ํ…Œ์ด๋ธ”์˜ ๋‚ด์šฉ ์ฐธ์กฐํ•˜๊ธฐ
SELECT * FROM sample32;

๋‘˜๋‹ค INTํƒ€์ž…์ด๋‹ค.

์˜ˆ์ œ) a ์—ด๋กœ ์ •๋ ฌํ•˜๊ธฐ

-- sample32๋ฅผ a์—ด๋งŒ์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ
SELECT * FROM sample32 ORDER BY a;

b์—ด์€ ์ •๋ ฌ๋˜์ง€ ์•Š๋Š”๋‹ค.

- ORDER BY๋กœ ๋ณต์ˆ˜ ์—ด ์ง€์ •ํ•˜๊ธฐ

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

์˜ˆ์ œ) a ์—ด๊ณผ b ์—ด๋กœ ์ •๋ ฌํ•˜๊ธฐ

-- sample32๋ฅผ a ์—ด๊ณผ b ์—ด๋กœ ์ •๋ ฌํ•˜๊ธฐ
SELECT * FROM sample32 ORDER BY a, b;

1์ฐจ๋กœ a์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœํ•˜๊ณ , 2์ฐจ๋กœ b์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœํ•œ๋‹ค.

Point: ORDER BY ๊ตฌ์— ๋ณต์ˆ˜์˜ ์—ด์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

์˜ˆ์ œ) b ์—ด๊ณผ a ์—ด๋กœ ์ •๋ ฌํ•˜๊ธฐ

-- sample32๋ฅผ b ์—ด๊ณผ a ์—ด๋กœ ์ •๋ ฌํ•˜๊ธฐ
SELECT * FROM sample32 ORDER BY b, a;

1์ฐจ๋กœ b์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœํ•˜๊ณ , 2์ฐจ๋กœ a์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœํ•œ๋‹ค.

๋จผ์ € b ์—ด์—์„œ ๊ฐ’์˜ ํฌ๊ธฐ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ๋˜๊ณ , ๊ฐ’์ด ๊ฐ™์€ ๋ถ€๋ถ„์€ a ์—ด๋กœ ์ •๋ ฌ๋œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.


10.2 ์ •๋ ฌ๋ฐฉ๋ฒ• ์ง€์ •ํ•˜๊ธฐ p. 106

๋ณต์ˆ˜ ์—ด ์ •๋ ฌ

SELECT ์—ด๋ช… FROM ํ…Œ์ด๋ธ”๋ช…
			ORDER BY ์—ด๋ช…1 [ASC|DESC], ์—ด๋ช…2 [ASC|DESC] ...

์˜ˆ์ œ) a ์—ด์„ ASC๋กœ b ์—ด์„ DESC๋กœ ์ •๋ ฌํ•˜๊ธฐ

-- sample32๋ฅผ a ์—ด ASC๋กœ, b ์—ด DESC๋กœ ์ •๋ ฌํ•˜๊ธฐ
SELECT * FROM sample32 ORDER BY a ASC, b DESC;

๋ฌธ์žฅ์˜ ๊ฐ€๋…์„ฑ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด์„œ๋ผ๋„ ๊ฐ€๋Šฅํ•œ ํ•œ ์ •๋ ฌ๋ฐฉ๋ฒ•์„ ์ƒ๋žตํ•˜์ง€ ๋ง๊ณ  ์ง€์ •ํ•˜๋„๋ก ํ•œ๋‹ค. 


10.3 NULL ๊ฐ’์˜ ์ •๋ ฌ ์ˆœ์„œ

ORDER BY๋กœ ์ง€์ •ํ•œ ์—ด์—์„œ NULL ๊ฐ’์„ ๊ฐ€์ง€๋Š” ํ–‰์€ ๊ฐ€์žฅ ๋จผ์ € ํ‘œ์‹œ๋˜๊ฑฐ๋‚˜ ๊ฐ€์žฅ ๋‚˜์ค‘์— ํ‘œ์‹œ๋œ๋‹ค.
MySQL์˜ ๊ฒฝ์šฐ NULL ๊ฐ’์„ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์œผ๋กœ ์ทจ๊ธ‰ํ•ด ASC(์˜ค๋ฆ„์ฐจ์ˆœ)์—์„œ๋Š” ๊ฐ€์žฅ ๋จผ์ €, DESC(๋‚ด๋ฆผ์ฐจ์ˆœ)์—์„œ๋Š” ๊ฐ€์žฅ ๋‚˜์ค‘์— ํ‘œ์‹œํ•œ๋‹ค.


11๊ฐ• ๊ฒฐ๊ณผ ํ–‰ ์ œํ•œํ•˜๊ธฐ - LIMIT p. 108

LIMIT ๊ตฌ

SELECT ์—ด๋ช… FROM ํ…Œ์ด๋ธ”๋ช… LIMIT ํ–‰์ˆ˜ [OFFSET ์‹œ์ž‘ํ–‰]
ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ์— ์“ฐ์ธ๋‹ค.
- LIMIT(ํ‘œ์ค€SQL์ด ์•„๋‹ˆ๋‹ค. DBMS์— ๋”ฐ๋ผ ์ง€์›์ด ์•ˆ ๋  ์ˆ˜ ์žˆ๋‹ค.)
- ROWNUM(ํ‘œ์ค€SQL์ด๋‹ค.)

11.1 ํ–‰์ˆ˜ ์ œํ•œ

LIMIT ๊ตฌ๋Š” ํ‘œ์ค€ SQL์€ ์•„๋‹ˆ๋‹ค. MySQL๊ณผ PostgreSQL์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ๋ฒ•์ด๋‹ค.

LIMIT ๊ตฌ

SELECT ์—ด๋ช… FROM ํ…Œ์ด๋ธ”๋ช… WHERE ์กฐ๊ฑด์‹ ORDER BY ์—ด๋ช… LIMIT ํ–‰์ˆ˜

์˜ˆ์ œ) sample33 ํ…Œ์ด๋ธ”

-- sample33 ํ…Œ์ด๋ธ”์˜ ๋‚ด์šฉ ์ฐธ์กฐํ•˜๊ธฐ
SELECT * FROM sample33;


์˜ˆ์ œ) LIMIT๋กœ ํ–‰์ˆ˜ ์ œํ•œํ•˜๊ธฐ

-- sample33์— LIMIT 3์œผ๋กœ ์ƒ์œ„ 3๊ฑด๋งŒ ์ทจ๋“ํ•˜๊ธฐ
SELECT * FROM sample33 LIMIT 3;

3๊ฐœ์˜ ํ–‰์ด ๋ฐ˜ํ™˜๋˜์—ˆ๋‹ค. LIMIT๋กœ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์€ '์ตœ๋Œ€ ํ–‰์ˆ˜'์ด๋‹ค.
๋งŒ์•ฝ ํ…Œ์ด๋ธ”์— ํ•˜๋‚˜์˜ ํ–‰๋งŒ ์žˆ๋‹ค๋ฉด LIMIT 3์œผ๋กœ ์ง€์ •ํ•ด๋„ 1๊ฐœ์˜ ํ–‰์ด ๋ฐ˜ํ™˜๋œ๋‹ค.

Point: LIMIT ๊ตฌ๋กœ ๋ฐ˜ํ™˜๋  ํ–‰์ˆ˜๋ฅผ ์ œํ•œํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‹จ์ˆœํ•˜๊ฒŒ WHERE ์ ˆ์„ ์“ธ ๊ฒฝ์šฐ ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ ๋•Œ ๊ธ€ ์‚ญ์ œํ•˜๋ฉด ์ œ๋Œ€๋กœ ์•ˆ ๋˜๋ฏ€๋กœ LIMIT๋ฅผ ์“ด๋‹ค.


1) ์ •๋ ฌํ•œ ํ›„ ์ œํ•œํ•˜๊ธฐ
์˜ˆ์ œ) ์ •๋ ฌ ํ›„ LIMIT๋กœ ํ–‰์ˆ˜ ์ œํ•œํ•˜๊ธฐ

-- sample33์„ ์ •๋ ฌ ํ›„ LIMIT 3์œผ๋กœ ์ƒ์œ„ 3๊ฑด๋งŒ ์ทจ๋“ํ•˜๊ธฐ
SELECT * FROM sample33 ORDER BY no DESC LIMIT 3;

no ์—ด์„ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ฆฌํ•œ ๋’ค์— ์ƒ์œ„ 3๊ฐœ ํ–‰์— ํ•ด๋‹นํ•˜๋Š” '7, 6, 5'๊ฐ€ ํ‘œ์‹œ๋˜์—ˆ๋‹ค.

2) LIMIT๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ์˜ ํ–‰ ์ œํ•œ p. 111

SQL Server

SELECT TOP 3 * FROM sample33;

Oracle

SELECT * FROM sample33 WHERE ROWNUM <= 3;

ROWNUM: ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐ˜ํ™˜๋  ๋•Œ ๊ฐ ํ–‰์— ํ• ๋‹น๋˜๋Š” ํ–‰ ๋ฒˆํ˜ธ
๋‹ค์‹œ ์ˆซ์ž๊ฐ€ ์žฌ์ •๋น„๋˜๋ฏ€๋กœ ๊ฒฐ๊ด๊ฐ’์ด ๋‹ค๋ฅด๋‹ค.

ROWNUM no
1 1
2 3
3 4
4 5
5 6
6 7

ex) ๋„ค์ด๋ฒ„ ์นดํŽ˜ ๊ฒŒ์‹œํŒ ๊ธ€ ๋ฒˆํ˜ธ๊ฐ€ 990์—์„œ 1004๋กœ ๋„˜์–ด๊ฐ„๋‹ค.
ํ•˜์ง€๋งŒ, ํŽ˜์ด์ง• ์ฒ˜๋ฆฌํ•  ๋•Œ๋Š” ROWNUM์œผ๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค. 

In computing, a client is a piece of computer hardware or software that accesses a service made available by a server as part of the client–server model of computer networks.


11.2 ์˜คํ”„์…‹ ์ง€์ • p. 112

๊ทธ๋ฆผ) ํŽ˜์ด์ง€ ๋ฐ”๊พธ๊ธฐ

1 [0] LIMIT 5
(5๊ฐœ์”ฉ ํ‘œ์‹œ)
6 <- OFFSET 5 [5] (๋ฐฐ์—ด 5) LIMIT 5
(5๊ฐœ์”ฉ ํ‘œ์‹œ)
2 [1] 7 [6]
3 [2] 8 [7]
4 [3] 9 [8]
5 [4] 10 [9]
< 1 2 3 > 1 ํŽ˜์ด์ง€   < 1 2 3 > 2 ํŽ˜์ด์ง€  

LIMIT 5 OFFSET 5: 2ํŽ˜์ด์ง€์˜ ๊ฒฝ์šฐ, ๋ฐฐ์—ด ์ธ๋ฑ์Šค 5(∴6ํ–‰)๋ถ€ํ„ฐ 5๊ฑด ํ‘œ์‹œ
LIMIT 5: ๋ณด์—ฌ์ฃผ๋Š” ๊ฐœ์ˆ˜ 5๊ฐœ
OFFSET 5: ์–ด๋””์„œ ์‹œ์ž‘ํ•˜๋Š”์ง€ ์ธ๋ฑ์Šค 5์—์„œ ์‹œ์ž‘ํ•œ๋‹ค. (์ž๋ฐ” ๋ฐฐ์—ด ์ธ๋ฑ์Šค์™€ ๋น„์Šทํ•˜๋‹ค.) 

ํ•œ ํŽ˜์ด์ง€ ๋‹น ๋ฐ์ดํ„ฐ 3๊ฑด์”ฉ ์ด 3๊ฐœ ํŽ˜์ด์ง€์— ๋‚˜๋ˆ  ํ‘œ์‹œํ•œ๋‹ค.

์˜ˆ์ œ) LIMIT๋กœ ์ฒซ ๋ฒˆ์งธ ํŽ˜์ด์ง€์— ํ‘œ์‹œํ•  ๋ฐ์ดํ„ฐ ์ทจ๋“ํ•˜๊ธฐ

-- sample33์—์„œ LIMIT 3 OFFSET 0์œผ๋กœ ์ฒซ ๋ฒˆ์งธ ํŽ˜์ด์ง€ ํ‘œ์‹œ
SELECT * FROM sample33 LIMIT 3 OFFSET 0;

0๋ถ€ํ„ฐ 3๊ฐœ์”ฉ์ด๋ฏ€๋กœ 1, 2, 3

LIMIT 3: 3๊ฐœ ํ‘œ์‹œ
OFFSET 0: 0๋ถ€ํ„ฐ (์ž๋ฐ” ๋ฐฐ์—ด ์ธ๋ฑ์Šค์™€ ๋น„์Šทํ•˜๋‹ค.) 

no ๋ฐฐ์—ด ์ธ๋ฑ์Šค
1 0
2 1
3 2
4 3
5 4
6 5
7 6

๋ฐฐ์—ด(array)์€ ๊ฐ™์€ ํƒ€์ž…์˜ ๋ณ€์ˆ˜๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ ์œ ํ•œ ์ง‘ํ•ฉ์œผ๋กœ ์ •์˜๋ฉ๋‹ˆ๋‹ค. ๋ฐฐ์—ด์„ ๊ตฌ์„ฑํ•˜๋Š” ๊ฐ๊ฐ์˜ ๊ฐ’์„ ๋ฐฐ์—ด ์š”์†Œ(element)๋ผ๊ณ  ํ•˜๋ฉฐ, ๋ฐฐ์—ด์—์„œ์˜ ์œ„์น˜๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ˆซ์ž๋Š” ์ธ๋ฑ์Šค(index)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.


OFFSET ์ง€์ •

SELECT ์—ด๋ช… FROM ํ…Œ์ด๋ธ”๋ช… LIMIT ํ–‰์ˆ˜ OFFSET ์œ„์น˜


์˜ˆ์ œ) LIMIT๋กœ ๋‘ ๋ฒˆ์งธ ํŽ˜์ด์ง€์— ํ‘œ์‹œํ•  ๋ฐ์ดํ„ฐ ์ทจ๋“ํ•˜๊ธฐ

-- sample33์—์„œ LIMIT 3 OFFSET 0์œผ๋กœ ๋‘ ๋ฒˆ์งธ ํŽ˜์ด์ง€ ํ‘œ์‹œ
SELECT * FROM sample33 LIMIT 3 OFFSET 3;

 

3๋ถ€ํ„ฐ 3๊ฐœ์”ฉ์ด๋ฏ€๋กœ 4, 5, 6

LIMIT 3: 3๊ฐœ ํ‘œ์‹œ
OFFSET 3:
3๋ถ€ํ„ฐ

no ๋ฐฐ์—ด ์ธ๋ฑ์Šค
1 0
2 1
3 2
4 3
5 4
6 5
7 6

12๊ฐ• ์ˆ˜์น˜ ์—ฐ์‚ฐ p. 114

์‚ฐ์ˆ  ์—ฐ์‚ฐ

+ - * / % MOD

-์—ฐ์‚ฐ ํ›„ CREATE TABLE๋กœ ํ…Œ์ด๋ธ”(์—ด)์„ ์•ˆ ๋งŒ๋“ค์–ด๋„ ๋œ๋‹ค. 
- SELECT ์ ˆ์— (SELECT ์˜ค๋ฅธ์ชฝ์—) ์—ฐ์‚ฐ์„ ๋„ฃ์–ด์„œ ์—ฐ์‚ฐ์„ ํ•œ๋‹ค. 


12.1 ์‚ฌ์น™ ์—ฐ์‚ฐ

+
-
*
/
% (๋‚˜๋จธ์ง€)

Point: ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์—ฐ์‚ฐ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

์šฐ์„ ์ˆœ์œ„ *, /, % > +, -


12.2 SELECT ๊ตฌ๋กœ ์—ฐ์‚ฐํ•˜๊ธฐ

SELECT ๊ตฌ

SELECT ์‹ 1, ์‹ 2 ... FROM ํ…Œ์ด๋ธ”๋ช…

์˜ˆ์ œ) sample34 ํ…Œ์ด๋ธ”

-- sample34 ํ…Œ์ด๋ธ”์˜ ๋‚ด์šฉ ์ฐธ์กฐํ•˜๊ธฐ
SELECT * FROM sample34;

 

์˜ˆ์ œ) SELECT ๊ตฌ๋กœ ๊ธˆ์•ก ๊ณ„์‚ฐํ•˜๊ธฐ
SELECT *
์˜†์— , ์ ๊ณ  ์—ฐ์‚ฐ์‹ ์“ด๋‹ค.

-- ๊ฐ€๊ฒฉ x ์ˆ˜๋Ÿ‰์œผ๋กœ ๊ธˆ์•ก ๊ณ„์‚ฐํ•˜๊ธฐ
SELECT *, price * quantity FROM sample34;


12.3 ์—ด์˜ ๋ณ„๋ช…(Aliases) (, price * quantity AS amount) p. 118

SQL aliases are used to give a table, or a column in a table, a temporary name.
Aliases are often used to make column names more readable.
An alias only exists for the duration of that query.
An alias is created with the AS keyword.

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

price * qunatity์™€ ๊ฐ™์ด ์—ด ์ด๋ฆ„์ด ๊ธธ๊ณ  ์•Œ์•„๋ณด๊ธฐ ์–ด๋ ค์šด ๊ฒฝ์šฐ๋Š” ๋ณ„๋ช…์„ ๋ถ™์—ฌ ์—ด๋ช…์„ ์žฌ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

์˜ˆ์ œ) SELECT ๊ตฌ์—์„œ ์‹์— ๋ณ„๋ช… ๋ถ™์ด๊ธฐ

-- price * quantity ์‹์— amount๋ผ๋Š” ๋ณ„๋ช… ๋ถ™์ด๊ธฐ
SELECT *, price * quantity AS amount FROM sample34;

ํ‚ค์›Œ๋“œ AS๋Š” ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค. 

์—์ผ๋ฆฌ์–ด์Šค(alias)๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๋Š” ๋ณ„๋ช…์€ ์˜์–ด, ์ˆซ์ž, ํ•œ๊ธ€ ๋“ฑ์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹จ, ๋ณ„๋ช…์„ ํ•œ๊ธ€๋กœ ์ง€์ •ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€๋กœ ์˜ค์ž‘๋™ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์œผ๋ฏ€๋กœ ๋”๋ธ”์ฟผํŠธ(MySQL์—์„œ๋Š” ๋ฐฑ์ฟผ๋“œ)๋กœ ๋‘˜๋Ÿฌ์‹ธ์„œ ์ง€์ •ํ•œ๋‹ค.

SELECT price * quantity "๊ธˆ์•ก" FROM sample34;


Point: ์ด๋ฆ„์— ASCII ๋ฌธ์ž ์ด์™ธ์˜ ๊ฒƒ์„ ํฌํ•จํ•  ๊ฒฝ์šฐ๋Š” ๋”๋ธ”์ฟผํŠธ๋กœ ๋‘˜๋Ÿฌ์‹ธ์„œ ์ง€์ •ํ•œ๋‹ค.

๋”๋ธ”์ฟผํŠธ๋กœ ๋‘˜๋Ÿฌ์‹ธ๋ฉด ๋ช…๋ น๊ตฌ๋ฌธ์„ ๋ถ„์„ํ•  ๋•Œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐ์ฒด์˜ ์ด๋ฆ„์ด๋ผ๊ณ  ๊ฐ„์ฃผํ•œ๋‹ค.
MySQL์—์„œ๋Š” ๋ฐฑ์ฟผํŠธ(``)๋กœ ๊ฐ์‹ธ์ค€๋‹ค.

ํ•œํŽธ ์‹ฑ๊ธ€์ฟผํŠธ๋กœ ๋‘˜๋Ÿฌ์‹ธ๋Š” ๊ฒƒ์€ ๋ฌธ์ž์—ด ์ƒ์ˆ˜์ด๋‹ค.
์ƒ์ˆ˜: ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๋ณ€์ˆ˜

-- ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐ์ฒด๋ช…
"sample21" "sample34" -- ๋”๋ธ”์ฟผํŠธ๋กœ ๋‘˜๋Ÿฌ์‹ผ๋‹ค.

-- ๋ฌธ์ž์—ด ์ƒ์ˆ˜
'ABC' '๋ฐ•์ค€์šฉ' -- ์‹ฑ๊ธ€์ฟผํŠธ๋กœ ๋‘˜๋Ÿฌ์‹ผ๋‹ค.

๋”๋ธ”์ฟผํŠธ๋กœ ๋‘˜๋Ÿฌ์‹ธ์„œ ์ง€์ •ํ•˜๋ฉด ์˜ˆ์•ฝ์–ด์™€ ๊ฐ™์€ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

SELECT price * quantity AS "SELECT" FROM sample34;

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐ์ฒด๋ช…์€ ์ˆซ์ž๋กœ ์‹œ์ž‘ํ•ด์„œ๋Š” ์•ˆ ๋œ๋‹ค๋Š” ์ œ์•ฝ์ด ์žˆ๋‹ค.

MySQL: ์ˆซ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฐ์ฒด๋ช…์ด ํ—ˆ์šฉ๋˜์ง€๋งŒ ์ˆซ์ž๋งŒ์œผ๋กœ ๊ตฌ์„ฑ๋˜๋Š” ๊ฐ์ฒด๋ช…์€ ํ—ˆ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.
Oracle: ์ˆซ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ด๋ฆ„์€ ํ—ˆ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค. 

Point: ์ด๋ฆ„์„ ์ง€์ •ํ•˜๋Š” ๊ฒฝ์šฐ ์ˆซ์ž๋กœ ์‹œ์ž‘๋˜์ง€ ์•Š๋„๋ก ํ•œ๋‹ค. 


12.4 WHERE ๊ตฌ์—์„œ ์—ฐ์‚ฐํ•˜๊ธฐ (WHERE price * quantity >= 2000)

์˜ˆ์ œ) WHERE ๊ตฌ์—์„œ ๊ณ„์‚ฐ, ๊ฒ€์ƒ‰ํ•˜๊ธฐ

SELECT *, price * quantity AS amount FROM sample34;

-- WHERE ๊ตฌ์—์„œ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•˜๊ณ  2000์› ์ด์ƒ์ธ ํ–‰ ๊ฒ€์ƒ‰ํ•˜๊ธฐ
SELECT *, price * quantity AS amount FROM sample34 
	WHERE price * quantity >= 2000;

WHERE ๊ตฌ → SELECT ๊ตฌ์˜ ์ˆœ์„œ๋กœ ์ฒ˜๋ฆฌ๋˜๋ฏ€๋กœ WHERE ๊ตฌ ์•ˆ์— amount๋ฅผ ์“ฐ๋ฉด amount๋ผ๋Š” ์—ด์€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

Point: SELECT ๊ตฌ์—์„œ ์ง€์ •ํ•œ ๋ณ„๋ช…์€ WHERE ๊ตฌ ์•ˆ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. 


12.5 NULL ๊ฐ’์˜ ์—ฐ์‚ฐ (NULL + 1 = NULL)

SQL์—์„œ๋Š” NULL ๊ฐ’์ด 0์œผ๋กœ ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š๋Š”๋‹ค.

๋‹ค์Œ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ ๋˜ํ•œ ๋ชจ๋‘ NULL์ด ๋œ๋‹ค.

NULL + 1
1 + NULL
1 + 2 * NULL
1 / NULL -- NULL์ด 0์œผ๋กœ ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š์•„ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ณ  ๊ฒฐ๊ณผ๋Š” NULL์ด ๋œ๋‹ค.


Point: NULL๋กœ ์—ฐ์‚ฐํ•˜๋ฉด ๊ฒฐ๊ณผ๋Š” NULL์ด ๋œ๋‹ค.


12.6 ORDER BY ๊ตฌ์—์„œ ์—ฐ์‚ฐํ•˜๊ธฐ (ORDER BY amount DESC)

์˜ˆ์ œ) ๊ธˆ์•ก์„ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ

SELECT *, price * quantity AS amount FROM sample34;
-- ORDER BY ๊ตฌ์—์„œ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•˜๊ณ  ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ
SELECT *, price * quantity AS amount FROM sample34 ORDER BY price * quantity DESC;


์˜ˆ์ œ) ๊ธˆ์•ก์˜ ๋ณ„๋ช…์„ ์‚ฌ์šฉํ•ด ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ

SELECT *, price * quantity AS amount FROM sample34;
-- ORDER BY ๊ตฌ์—์„œ ๋ณ„๋ช…์„ ์‚ฌ์šฉํ•ด ์ •๋ ฌํ•˜๊ธฐ
SELECT *, price * quantity AS amount FROM sample34 BY amount DESC;

WHERE ๊ตฌ → SELECT ๊ตฌ (์—ฌ๊ธฐ์„œ ๋ณ„๋ช…์„ ์ง€์ •) → ORDER BY ๊ตฌ

Point: ORDER BY ๊ตฌ์—์„œ๋Š” SELECT ๊ตฌ์—์„œ ์ง€์ •ํ•œ ๋ณ„๋ช…์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.


12.7 ํ•จ์ˆ˜

ํ•จ์ˆ˜

ํ•จ์ˆ˜๋ช… (์ธ์ˆ˜1, ์ธ์ˆ˜2 ...)

Point: ํ•จ์ˆ˜๋„ ์—ฐ์‚ฐ์ž๋„ ํ‘œ๊ธฐ ๋ฐฉ๋ฒ•์ด ๋‹ค๋ฅผ ๋ฟ, ๊ฐ™์€ ๊ฒƒ์ด๋‹ค.


12.8 ROUND ํ•จ์ˆ˜

create table sample341(
	price int,
	quantity int,
	amount decimal(10,2)
);

decimal(10,2) means you can have a decimal number with a maximal total precision of 10 digits. 2 of them after the decimal point and 8 before. (10,2) defines the precision of the decimal value. 10 digits and 2 digits after the comma.

์˜ˆ์ œ) ROUND๋กœ ๋ฐ˜์˜ฌ๋ฆผํ•˜๊ธฐ

SELECT * FROM sample341;
-- ROUND๋กœ ๋ฐ˜์˜ฌ๋ฆผ
SELECT amount, ROUND(amount) FROM sample341;

- ๋ฐ˜์˜ฌ๋ฆผ ์ž๋ฆฟ์ˆ˜ ์ง€์ • 

9 6 1 . 6 0
-3 = 100 ๋‹จ์œ„ -2 = 10 ๋‹จ์œ„ -1 = 1๋‹จ์œ„   0 = ์†Œ์ˆ˜์  ์ฒซ์งธ ์ž๋ฆฌ 1 = ์†Œ์ˆ˜์  ๋‘˜์งธ ์ž๋ฆฌ


์˜ˆ์ œ) ์†Œ์ˆ˜์  ๋‘˜์งธ ์ž๋ฆฌ์—์„œ ๋ฐ˜์˜ฌ๋ฆผํ•˜๊ธฐ

-- ROUND ํ•จ์ˆ˜์˜ ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋ฅผ ์ง€์ •ํ•ด, ์†Œ์ˆ˜์  ๋‘˜์งธ ์ž๋ฆฌ๋ฅผ ๋ฐ˜์˜ฌ๋ฆผ
SELECT amount, ROUND(amount, 1) FROM sample341;

์˜ˆ์ œ) 10 ๋‹จ์œ„๋ฅผ ๋ฐ˜์˜ฌ๋ฆผํ•˜๊ธฐ

-- ROUND ํ•จ์ˆ˜์˜ ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋ฅผ ์ง€์ •ํ•ด 10๋‹จ์œ„๋ฅผ ๋ฐ˜์˜ฌ๋ฆผ
SELECT amount, ROUND(amount, -2) FROM sample341;


13๊ฐ• ๋ฌธ์ž์—ด ์—ฐ์‚ฐ p. 150

๋ฌธ์ž์—ด ์—ฐ์‚ฐ

+|| CONCAT SUBSTRING TRIM CHARACTER_LENGTH

13.1 ๋ฌธ์ž์—ด ๊ฒฐํ•ฉ (CONCAT)

๋ฌธ์ž์—ด ๊ฒฐํ•ฉ ์‚ฌ๋ก€
'ABC' || '1234' → 'ABC1234'

์—ฐ์‚ฐ์ž/ํ•จ์ˆ˜ ์—ฐ์‚ฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค
+ ๋ฌธ์ž์—ด ๊ฒฐํ•ฉ SQL Server
|| ๋ฌธ์ž์—ด ๊ฒฐํ•ฉ Oracle, DB2, PostgreSQL
CONCAT ๋ฌธ์ž์—ด ๊ฒฐํ•ฉ MySQL

Point: + ์—ฐ์‚ฐ์ž, || ์—ฐ์‚ฐ์ž, CONCAT ํ•จ์ˆ˜๋กœ ๋ฌธ์ž์—ด์„ ๊ฒฐํ•ฉํ•  ์ˆ˜ ์žˆ๋‹ค. 
concatenate: to put things together as a connected series

์˜ˆ์ œ) ๋ฌธ์ž์—ด ๊ฒฐํ•ฉ

SELECT * FROM sample35;
-- ๋ฌธ์ž์—ด ๊ฒฐํ•ฉ์œผ๋กœ ๋‹จ์œ„๋ฅผ ์—ฐ๊ฒฐํ•ด ๊ฒฐ๊ณผ ์–ป๊ธฐ
SELECT CONCAT(quantity, unit) FROM sample35;


13.2 SUBSTRING ํ•จ์ˆ˜ (๋ฌธ์ž์—ด์˜ ์ผ๋ถ€๋ถ„์„ ๊ณ„์‚ฐํ•ด์„œ ๋ฐ˜ํ™˜)

SUBSTRING: ๋ฌธ์ž์—ด์˜ ์ผ๋ถ€๋ถ„์„ ๊ณ„์‚ฐํ•ด์„œ ๋ฐ˜ํ™˜ํ•ด ์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค.

-- ์•ž 4์ž๋ฆฌ(๋…„) ์ถ”์ถœ
SUBSTRING('20140125001', 1, 4) -> '2014'

-- 5์งธ ์ž๋ฆฌ๋ถ€ํ„ฐ 2์ž๋ฆฌ(์›”) ์ถ”์ถœ
SUBSTRING('20140125001', 5, 2) -> '01'

์ฃผ๋ฏผ๋“ฑ๋ก๋ฒˆํ˜ธ ๋“ฑ์— ์“ฐ์ธ๋‹ค.


13.3 TRIM ํ•จ์ˆ˜ (์ŠคํŽ˜์ด์Šค ์ œ๊ฑฐ)

TRIM: ๋ฌธ์ž์—ด์˜ ์•ž๋’ค๋กœ ์—ฌ๋ถ„์˜ ์ŠคํŽ˜์ด์Šค๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ด๋ฅผ ์ œ๊ฑฐํ•ด ์ฃผ๋Š” ํ•จ์ˆ˜๋กœ ๋ฌธ์ž์—ด ๋„์ค‘์— ์กด์žฌํ•˜๋Š” ์ŠคํŽ˜์ด์Šค๋Š” ์ œ๊ฑฐ๋˜์ง€ ์•Š๋Š”๋‹ค.

-- TRIM์œผ๋กœ ์ŠคํŽ˜์ด์Šค ์ œ๊ฑฐํ•˜๊ธฐ
TRIM('ABC   ') -> 'ABC'


13.4 CHARACTER_LENGTH ํ•จ์ˆ˜ (๋ฌธ์ž์—ด์˜ ๊ธธ์ด)

CHARACTER_LENGTH: ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๊ณ„์‚ฐํ•ด ๋Œ๋ ค์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค.
OCTET_LENGTH: ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๊ณ„์‚ฐํ•ด ๋Œ๋ ค์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค.
๋ฌธ์ž ํ•˜๋‚˜์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ช‡ ๋ฐ”์ดํŠธ์˜ ์ €์žฅ๊ณต๊ฐ„์„ ํ•„์š”๋กœ ํ•˜๋Š”์ง€ '์ธ์ฝ”๋“œ ๋ฐฉ์‹'์— ๋”ฐ๋ผ ๊ฒฐ์ •๋œ๋‹ค. 

Point: ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด๋Š” ๋ฌธ์ž์„ธํŠธ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๋‹ค.


14๊ฐ• ๋‚ ์งœ ์—ฐ์‚ฐ p. 136

๋‚ ์งœ ์—ฐ์‚ฐ

CURRENT_TIMESTAMP CURRENT_DATE INTERVAL

14.1 SQL์—์„œ์˜ ๋‚ ์งœ

1) ์‹œ์Šคํ…œ ๋‚ ์งœ

์˜ˆ์ œ) ์‹œ์Šคํ…œ ๋‚ ์งœ ํ™•์ธํ•˜๊ธฐ (CURRENT_TIMESTAMP)

-- CURRENT_TIMESTAMP๋กœ ์‹œ์Šคํ…œ ๋‚ ์งœ ํ™•์ธ
SELECT CURRENT_TIMESTAMP;

Oracle: SYSDATE ํ•จ์ˆ˜
SQL Server: GETDATE ํ•จ์ˆ˜


2) ๋‚ ์งœ ์„œ์‹ (date_format(hiredate, "%y/%m/%d"))

์†Œ๋ฌธ์ž y๋Š” ๋ ๋‘์ž๋ฆฌ ์—ฐ๋„, ๋Œ€๋ฌธ์ž Y๋Š” ์ „์ฒด ์—ฐ๋„ 4๊ธ€์ž๊ฐ€ ๋‚˜์˜จ๋‹ค.
/ ๋Œ€์‹  .์„ ์“ฐ๋‹ˆ๊นŒ ์ € ํ˜•ํƒœ๋กœ ๋‚˜์˜จ๋‹ค.


Oracle์˜ ๊ฒฝ์šฐ TO_DATE ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ๋ฅผ ๋‚ ์งœํ˜• ๋ฐ์ดํ„ฐ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

TO_DATE('2014/01/25', 'YYYY/MM/DD')

Point: ๋‚ ์งœ ๋ฐ์ดํ„ฐ๋Š” ์„œ์‹์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.


14.2 ๋‚ ์งœ์˜ ๋ง์…ˆ๊ณผ ๋บ„์…ˆ

์˜ˆ์ œ) ์‹œ์Šคํ…œ ๋‚ ์งœ์˜ 1์ผ ํ›„๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ (CURRENT_DATE + INTERVAL 1 DAY)

-- ๋‚ ์งœ๋ฅผ ์—ฐ์‚ฐํ•ด ์‹œ์Šคํ…œ ๋‚ ์งœ์˜ 1์ผ ํ›„๋ฅผ ๊ฒ€์ƒ‰
SELECT CURRENT_DATE + INTERVAL 1 DAY;


- ๋‚ ์งœํ˜• ๊ฐ„์˜ ๋บ„์…ˆ (DATEDIFF)

MySQL

DATEDIFF('2014-02-28', '2014-01-01')
SELECT DATEDIFF(month, '2017/08/25', '2011/08/25') AS DateDiff;
-- ๊ฒฐ๊ณผ -72 (๋‹ฌ)
SELECT DATEDIFF(hour, '2017/08/25 07:00', '2017/08/25 12:45') AS DateDiff;
-- ๊ฒฐ๊ณผ 5 (์‹œ๊ฐ„)

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

Oracle

'2014-02-28' - '2014-01-01'

15๊ฐ• CASE ๋ฌธ์œผ๋กœ ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜ํ•˜๊ธฐ p. 140

IF์™€ ์œ ์‚ฌํ•˜๋‹ค.

CASE ๋ฌธ

CASE WHEN ์กฐ๊ฑด์‹1 THEN ์‹1
	[WHEN ์กฐ๊ฑด์‹2 THEN ์‹2 ...]
    [ELSE ์‹3]
END

15.1 CASE ๋ฌธ p. 141

CASE ๋ฌธ

CASE WHEN ์กฐ๊ฑด์‹1 THEN ์‹1
	[WHEN ์กฐ๊ฑด์‹2 THEN ์‹2 ...]
    [ELSE ์‹3]
END

์˜ˆ์ œ) NULL ๊ฐ’์„ 0์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ

NULL๊ฐ’์ด ์žˆ์œผ๋ฉด ์—ฐ์‚ฐ์ด ์•ˆ ๋˜๋ฏ€๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.

SELECT a FROM sample37;
-- CASE๋กœ NULL ๊ฐ’์„ 0์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ
SELECT a, CASE WHEN a IS NULL THEN 0 ELSE a END "a(null=0)" FROM sample37;

 

The IFNULL() function returns a specified value if the expression is NULL. If the expression is NOT NULL, this function returns the expression.

SELECT IFNULL(NULL, 500);
-- 500

IFNULL ์˜ˆ์‹œ


- COALESCE ํ•จ์ˆ˜ p. 142

SELECT a, COALESCE(a, 0) FROM sample37;

์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ธ์ˆ˜๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. NULL์ด ์•„๋‹Œ ๊ฐ’์— ๋Œ€ํ•ด์„œ๋Š” ๊ฐ€์žฅ ๋จผ์ € ์ง€์ •๋œ ์ธ์ˆ˜์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 
a๊ฐ€ NULL์ด ์•„๋‹ˆ๋ฉด a๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด (a๊ฐ€ NULL์ด๋ฉด) 0์„ ์ถœ๋ ฅํ•œ๋‹ค. 

COALESCE(val1, val2, ...., val_n)

The COALESCE() function returns the first non-null value in a list.

SELECT COALESCE(NULL, 1, 2, 'W3Schools.com');
-- 1

CAOLESCE ์˜ˆ์‹œ


15.2 ๋˜ ํ•˜๋‚˜์˜ CASE ๋ฌธ

WHEN a = 1 THEN '๋‚จ์ž'
WHEN a = 2 THEN '์—ฌ์ž'

Point: CASE ๋ฌธ์—๋Š” 2๊ฐœ์˜ ๊ตฌ๋ฌธ์ด ์žˆ๋‹ค.

[1] ๋‹จ์ˆœ CASE ์‹ - NULL๊ฐ’ ๋น„๊ต ๋ถˆ๊ฐ€ 

CASE ์‹1
	WHEN ์‹2 THEN ์‹3
    [WHEN ์‹4 THEN ์‹5 ... ]
    [ELSE ์‹6]
END

์˜ˆ์ œ) ์„ฑ๋ณ„ ์ฝ”๋“œ ๋ณ€ํ™˜ํ•˜๊ธฐ ([2] ๊ฒ€์ƒ‰ CASE - NULL๊ฐ’ ๋น„๊ต ๊ฐ€๋Šฅ)

-- ๊ฒ€์ƒ‰ CASE๋กœ ์„ฑ๋ณ„ ์ฝ”๋“œ๋ฅผ ๋‚จ์ž, ์—ฌ์ž๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ
SELECT a AS "์ฝ”๋“œ",
CASE
	WHEN a = 1 THEN '๋‚จ์ž'
    WHEN a = 2 THEN '์—ฌ์ž'
    ELSE '๋ฏธ์ง€์ •' -- ์—ด์ด 1 ๋˜๋Š” 2๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ
END AS "์„ฑ๋ณ„" FROM sample37;


์˜ˆ์ œ) ์„ฑ๋ณ„ ์ฝ”๋“œ ๋ณ€ํ™˜ํ•˜๊ธฐ ([1] ๋‹จ์ˆœ CASE - NULL๊ฐ’ ๋น„๊ต ๋ถˆ๊ฐ€)

-- ๋‹จ์ˆœ CASE๋กœ ์„ฑ๋ณ„ ์ฝ”๋“œ๋ฅผ ๋‚จ์ž, ์—ฌ์ž๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ
SELECT a AS "์ฝ”๋“œ",
CASE a			-- CASE ๋’ค์—๋Š” ๋Œ€์ƒ์„ ์ ๋Š”๋‹ค.
	WHEN 1 THEN '๋‚จ์ž' -- WHEN ๋’ค์—๋Š” ๊ฐ’๋งŒ ์ ๋Š”๋‹ค.
    WHEN 2 THEN '์—ฌ์ž'
    ELSE '๋ฏธ์ง€์ •'
END AS "์„ฑ๋ณ„" FROM sample37;

NULL๊ฐ’ ๋น„๊ต๋Š” IS NULL์ด๋ฏ€๋กœ NULL๊ฐ’์ด ์žˆ์œผ๋ฉด ์‚ฌ์šฉ ์•ˆ ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.


15.3 CASE๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์ฃผ์˜์‚ฌํ•ญ p. 145

1) ELSE ์ƒ๋žต
ELSE๋ฅผ ์ƒ๋žตํ•˜๋ฉด ELSE NULL์ด ๋˜๋Š” ๊ฒƒ์— ์ฃผ์˜ํ•œ๋‹ค.
POINT: CASE ๋ฌธ์˜ ELSE๋Š” ์ƒ๋žตํ•˜์ง€ ์•Š๋Š” ํŽธ์ด ๋‚ซ๋‹ค.


2) WHEN์—์„œ NULL ์ง€์ •ํ•˜๊ธฐ

-- ๋‹จ์ˆœ CASE ๋ฌธ์—์„œ WHEN ์ ˆ์— NULL ์ง€์ •ํ•˜๊ธฐ - NULL ๊ฐ’ ๋น„๊ต ๋ถˆ๊ฐ€๋Šฅ
CASE a
	WHEN 1 THEN '๋‚จ์ž'
    WHEN 2 THEN '์—ฌ์ž'
    WHEN NULL THEN '๋ฐ์ดํ„ฐ ์—†์Œ'
    ELSE '๋ฏธ์ง€์ •'
END

๋‹จ์ˆœ CASE ๋ฌธ์œผ๋กœ๋Š” NULL์„ ๋น„๊ตํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๋ฌธ์ œ์ ์ด ์žˆ๋‹ค.


์˜ˆ์ œ) [2] ๊ฒ€์ƒ‰ CASE ๋ฌธ์œผ๋กœ NULL ํŽธ์ •ํ•˜๊ธฐ - NULL ๊ฐ’ ๋น„๊ต ๊ฐ€๋Šฅ

CASE 
	WHEN a = 1 THEN '๋‚จ์ž'
    WHEN a = 2 THEN '์—ฌ์ž'
    WHEN a IS NULL THEN '๋ฐ์ดํ„ฐ ์—†์Œ'
    ELSE '๋ฏธ์ง€์ •'
END

NULL = ๋ฐ์ดํ„ฐ ์—†์Œ์œผ๋กœ ๋‚˜์˜จ๋‹ค.


Point: [1] ๋‹จ์ˆœ CASE ๋ฌธ์œผ๋กœ๋Š” NULL ๊ฐ’์„ ๋น„๊ตํ•  ์ˆ˜ ์—†๋‹ค.


3) DECODE, NVL (Oracle)

Oracle: ๋””์ฝ”๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” DECODE ํ•จ์ˆ˜๊ฐ€ ๋‚ด์žฅ๋˜์–ด ์žˆ๋‹ค. 
- NVL: NULL ๊ฐ’์„ ๋ณ€ํ™˜ํ•œ๋‹ค. 
NVL lets you replace null (returned as a blank) with a string in the results of a query.

SQL Server
- ISNULL: NULL ๊ฐ’์„ ๋ณ€ํ™˜ํ•œ๋‹ค. 

ํ‘œ์ค€ SQL: COALESCE ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.


728x90
๋ฐ˜์‘ํ˜•

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

[53] 230315 SQL Ch 5. ์ง‘๊ณ„์™€ ์„œ๋ธŒ์ฟผ๋ฆฌ: 20๊ฐ• ํ–‰ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ - COUNT, 21๊ฐ• COUNT ์ด์™ธ์˜ ์ง‘๊ณ„ํ•จ์ˆ˜, 22๊ฐ• ๊ทธ๋ฃนํ™” - GROUP BY [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 53์ผ]  (1) 2023.03.15
*[53] 230315 SQL Ch 4. ๋ฐ์ดํ„ฐ์˜ ์ถ”๊ฐ€, ์‚ญ์ œ, ๊ฐฑ์‹ : 16๊ฐ• ํ–‰ ์ถ”๊ฐ€ํ•˜๊ธฐ - INSERT, 17๊ฐ• ์‚ญ์ œํ•˜๊ธฐ - DELETE, 18๊ฐ• ๋ฐ์ดํ„ฐ ๊ฐฑ์‹ ํ•˜๊ธฐ - UPDATE, 19๊ฐ• ๋ฌผ๋ฆฌ์‚ญ์ œ์™€ ๋…ผ๋ฆฌ์‚ญ์ œ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 53์ผ]  (0) 2023.03.15
[50~55] 230310~230317 SQL ๋ฌธ์ œ ํ’€์–ด๋ณด๊ธฐ (๋ฐ์ดํ„ฐ: Employer, Department) [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 50์ผ~55์ผ]  (1) 2023.03.13
[51] 230313 SQL Ch. 2 ํ…Œ์ด๋ธ”์—์„œ ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰: 4๊ฐ• Hello World ์‹คํ–‰ํ•˜๊ธฐ, 5๊ฐ• ํ…Œ์ด๋ธ” ๊ตฌ์กฐ ์ฐธ์กฐํ•˜๊ธฐ, 6๊ฐ• ๊ฒ€์ƒ‰ ์กฐ๊ฑด ์ง€์ •ํ•˜๊ธฐ, 7๊ฐ• ์กฐ๊ฑด ์กฐํ•ฉํ•˜๊ธฐ, 8๊ฐ• ํŒจํ„ด ๋งค์นญ์— ์˜ํ•œ ๊ฒ€์ƒ‰ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 51์ผ]  (0) 2023.03.13
Comments