๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
IT์ž๊ฒฉ์ฆ ๊ณต๋ถ€/SQLD ์ž๊ฒฉ์ฆ

[SQL ์ „๋ฌธ๊ฐ€] ๊ณผ๋ชฉII. ์ œ1์žฅ SQL ๊ธฐ๋ณธ

by yunamom 2022. 4. 13.
๋ฐ˜์‘ํ˜•

๐Ÿ“–์ œ1์žฅ SQL ๊ธฐ๋ณธ

โœจ์ œ1์ ˆ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐœ์š”


1. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค (DBMS : Database Management System) 

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋ฐœ์ „
1960๋…„๋Œ€ ํ”Œ๋กœ์šฐ์ฐจํŠธ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ–ˆ์Œ๋ฉฐ ํŒŒ์ผ ๊ตฌ์กฐ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ, ๊ด€๋ฆฌํ–ˆ๋‹ค.
1970๋…„๋Œ€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ฆฌ ๊ธฐ๋ฒ•์ด ํƒœ๋™ํ–ˆ๋˜ ์‹œ๊ธฐ์˜€์œผ๋ฉฐ ๊ณ„์ธตํ˜•(Hierarchical) ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ๋งํ˜•(Network) ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐ™์€ ์ œํ’ˆ๋“ค์ด ์ƒ์šฉํ™”๊ฐ€ ๋๋‹ค.
1980๋…„๋Œ€ ํ˜„์žฌ ๋Œ€๋ถ€๋ถ„์˜ ๊ธฐ์—…์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์ƒ์šฉํ™”๊ฐ€ ๋๋‹ค.
Oracle, Sybase, DB2์™€ ๊ฐ™์€ ์ œํ’ˆ์ด ์‚ฌ์šฉ๋๋‹ค.
1990๋…„๋Œ€ Oracle, Sybase, Informix, DB2, Teradata, SQL Server ์™ธ ๋งŽ์€ ์ œํ’ˆ์ด ๋” ํ–ฅ์ƒ๋œ ๊ธฐ๋Šฅ์œผ๋กœ ์ •๋ณด์‹œ์Šคํ…œ์˜ ํ•ต์‹ฌ ์†”๋ฃจ์…˜์œผ๋กœ ์ž๋ฆฌ์žก์•˜๋‹ค.
์ธํ„ฐ๋„ท ํ™˜๊ฒฝ์˜ ๊ธ‰์†ํ•œ ๋ฐœ์ „์— ๋ฐœ๋งž์ถฐ ๊ฐ์ฒด์ง€ํ–ฅ ์ •๋ณด๋ฅผ ์ง€์›ํ•˜๊ธฐ์œ„ํ•ด ๊ฐ์ฒด ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ ๋ฐœ์ „ํ–ˆ๋‹ค.

โˆ™๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

1970๋…„ ์˜๊ตญ์˜ ์ˆ˜ํ•™์ž์˜€๋˜ E.F. Godd ๋ฐ•์‚ฌ์˜ ๋…ผ๋ฌธ์—์„œ ์ฒ˜์Œ์„ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค(Relational Database) ๊ฐ€ ์†Œ๊ฐœ๋œ ์ดํ›„, IBM์˜ SQL ๊ฐœ๋ฐœ ๋‹จ๊ณ„๋ฅผ ๊ฑฐ์ณ Oracle์„ ์„ ๋ฐœ๋กœ ์—ฌ๋Ÿฌ ํšŒ์‚ฌ์—์„œ ์ƒ์šฉํ™”ํ•œ ์ œํ’ˆ์„ ๋‚ด๋†“์•˜๋‹ค.

์ดํ›„ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์—ฌ๋Ÿฌ ์žฅ์ ์ด ์•Œ๋ ค์ง€๋ฉด์„œ ๊ธฐ์กด์˜ ํŒŒ์ผ ์‹œ์Šคํ…œ๊ณผ ๊ณ„์ธตํ˜•, ๋งํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๋Œ€๋ถ€๋ถ„ ๋Œ€์ฒดํ•˜๋ฉด์„œ ์ฃผ๋ ฅ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ๋๋‹ค.

 

2. SQL (Structured Query Language) 

๋ช…๋ น์–ด์˜ ์ข…๋ฅ˜ ๋ช…๋ น์–ด ์„ค๋ช…
๋ฐ์ดํ„ฐ ์กฐ์ž‘์–ด
(DML, Data Manipulation Language)
SELECT ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋“ค์–ด ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๊ฑฐ๋‚˜ ๊ฒ€์ƒ‰ํ•˜๊ธฐ ์œ„ํ•œ ๋ช…๋ น์–ด๋กœ RETRIEVE๋ผ๊ณ ๋„ ํ•œ๋‹ค.
INSERT
UPDATE
DELETE
๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ํ…Œ์ด๋ธ”์— ๋“ค์–ด ์žˆ๋Š” ๋ฐ์ดํ„ฐ์— ๋ณ€ํ˜•์„ ๊ฐ€ํ•˜๋Š” ์ข…๋ฅ˜์˜ ๋ช…๋ น์–ด๋“ค์„ ๋งํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ…Œ์ด๋ธ”์— ์ƒˆ๋กœ์šด ํ–‰์„ ์ง‘์–ด๋„ฃ๊ฑฐ๋‚˜, ๊ธฐ์กด ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜ ์›ํ•˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•˜๋Š” ๊ฒƒ๋“ค์˜ ๋ช…๋ น์–ด๋“ค์„ DML ์ด๋ผ๊ณ  ํ•œ๋‹ค.
๋ฐ์ดํ„ฐ ์ •์˜์–ด
(DDL, Data Definition Language)
CREATE
ALTER
DROP
RENAME
ํ…Œ์ด๋ธ”๊ณผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๋ช…๋ น์–ด๋“ค์ด๋‹ค.
๊ตฌ์กฐ๋ฅผ ์ƒ์„ฑ, ๋ณ€๊ฒฝ, ์‚ญ์ œํ•˜๊ฑฐ๋‚˜ ์ด๋ฆ„์„ ๋ฐ”๊พธ๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์™€ ๊ด€๋ จ๋œ ๋ช…๋ น์–ด๋“ค์„ DDL์ด๋ผ๊ณ  ํ•œ๋‹ค.
๋ฐ์ดํ„ฐ ์ œ์–ด์–ด
(DCL, Data Control Language)
GRANT (๊ถŒํ•œ๋ถ€์—ฌ TO)
REVOKE (๊ถŒํ•œํšŒ์ˆ˜ FROM)
๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘๊ทผํ•˜๊ณ  ๊ฐ์ฒด๋“ค์„ ์‚ฌ์šฉํ•˜๋„๋ก ๊ถŒํ•œ์„ ์ฃผ๊ณ  ํšŒ์ˆ˜ํ•˜๋Š” ๋ช…๋ น์–ด๋ฅผ DCL์ด๋ผ๊ณ  ํ•œ๋‹ค.
ํŠธ๋žœ์žญ์…˜ ์ œ์–ด์–ด
(TCL, Transaction Control Language)
COMMIT
ROLLBACK
๋…ผ๋ฆฌ์ ์ธ ์ž‘์—… ๋‹จ์œ„๋ฅผ ๋ฌถ์–ด์„œ DML์— ์˜ํ•ด ์กฐ์ž‘๋œ ๊ฒฐ๊ณผ๋ฅผ ์ž‘์—…๋‹จ์œ„(ํŠธ๋žœ์žญ์…˜)๋ณ„๋กœ ์ œ์–ดํ•˜๋Š” ๋ช…๋ น์–ด๋ฅผ ๋งํ•œ๋‹ค.

 

3. STANDARD SQL ๊ฐœ์š” 

1970๋…„ Dr. E.F.Codd ๊ด€๊ณ„ํ˜• DBMS(Relational DB) ๋…ผ๋ฌธ ๋ฐœํ‘œ
1974๋…„ IBM SQL ๊ฐœ๋ฐœ
1979๋…„ Oracle ์ƒ์šฉ DBMS ๋ฐœํ‘œ
1980๋…„ Sybase SQL Server ๋ฐœํ‘œ(์ดํ›„ Sybase ASE๋กœ ๊ฐœ๋ช…)
1983๋…„ IBM DB2 ๋ฐœํ‘œ
1986๋…„ ANSI/ISO SQL ํ‘œ์ค€ ์ตœ์ดˆ ์ œ์ •(SQL-86, SQL1)
1992๋…„ ANSI/ISO SQL ํ‘œ์ค€ ๊ฐœ์ •(SQL-92, SQL2)
1993๋…„ MS SQL Server ๋ฐœํ‘œ(Windows OS, Sybase Code ํ™œ์šฉ)
1999๋…„ ANSI/ISO SQL ํ‘œ์ค€ ๊ฐœ์ •(SQL-99, SQL3)
2003๋…„ ANSI/ISO SQL ํ‘œ์ค€ ๊ฐœ์ •(SQL:2003)
2006๋…„ ANSI/ISO SQL ํ‘œ์ค€ ๊ฐœ์ •(SQL:2006)
2008๋…„ ANSI/ISO SQL ํ‘œ์ค€ ๊ฐœ์ •(SQL:2008)
2011๋…„ ANSI/ISO SQL ํ‘œ์ค€ ๊ฐœ์ •(SQL:2011)
2016๋…„ ANSI/ISO SQL ํ‘œ์ค€ ๊ฐœ์ •(SQL:2016)

๊ตญ๋‚ด๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ „ ์„ธ๊ณ„์ ์œผ๋กœ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ์˜ค๋ธŒ์ ํŠธ ๊ฐœ๋…์„ ํฌํ•จํ•œ ์—ฌ๋Ÿฌ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ๋“ค์ด ๊พธ์ค€ํžˆ ์ถ”๊ฐ€๋˜๊ณ  ์žˆ๋‹ค.

ํ˜„์žฌ ๊ธฐ์—…ํ˜• DBMS๋Š” ์ˆœ์ˆ˜ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์•„๋‹Œ, ๊ฐ์ฒด์ง€์› ๊ธฐ๋Šฅ์ด ํฌํ•จ๋œ ๊ฐ์ฒด๊ด€๊ณ„ํ˜•(Object Relational) ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ๋Œ€๋ถ€๋ถ„์ด๋‹ค.

 

4. ํ…Œ์ด๋ธ” 

์šฉ์–ด ์„ค๋ช…
ํ…Œ์ด๋ธ” ํ–‰๊ณผ ์นผ๋Ÿผ์˜ 2์ฐจ์› ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง„ ๋ฐ์ดํ„ฐ์˜ ์ €์žฅ ์žฅ์†Œ์ด๋ฉฐ, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๊ฐœ๋…
์นผ๋Ÿผโˆ™์—ด 2์ฐจ์› ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง„ ํ…Œ์ด๋ธ”์—์„œ ์„ธ๋กœ ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ํ•˜๋‚˜ ํ•˜๋‚˜์˜ ํŠน์ • ์†์„ฑ(๋”์ด์ƒ ๋‚˜๋ˆŒ ์ˆ˜ ์—†๋Š” ํŠน์„ฑ)
ํ–‰ 2์ฐจ์› ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง„ ํ…Œ์ด๋ธ”์—์„œ ๊ฐ€๋กœ ๋ฐฉํ–ฅ์œผ๋กœ ์—ฐ๊ฒฐ๋œ ๋ฐ์ดํ„ฐ

ํ…Œ์ด๋ธ” ๊ด€๊ณ„ ์šฉ์–ด๋“ค

์šฉ์–ด ์„ค๋ช…
์ •๊ทœํ™” ํ…Œ์ด๋ธ”์„ ๋ถ„ํ• ํ•ด ๋ฐ์ดํ„ฐ์˜ ์ •ํ•ฉ์„ฑ์„ ํ™•๋ณดํ•˜๊ณ , ๋ถˆํ•„์š”ํ•œ ์ค‘๋ณต์„ ์ค„์ด๋Š” ํ”„๋กœ์„ธ์Šค
๊ธฐ๋ณธํ‚ค 2์ฐจ์› ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง„ ํ…Œ์ด๋ธ”์—์„œ ์„ธ๋กœ ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ํ•˜๋‚˜ ํ•˜๋‚˜์˜ ํŠน์ • ์†์„ฑ(๋”์ด์ƒ ๋‚˜๋ˆŒ ์ˆ˜ ์—†๋Š” ํŠน์„ฑ)
์™ธ๋ถ€ํ‚ค 2์ฐจ์› ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง„ ํ…Œ์ด๋ธ”์—์„œ ๊ฐ€๋กœ ๋ฐฉํ–ฅ์œผ๋กœ ์—ฐ๊ฒฐ๋œ ๋ฐ์ดํ„ฐ

 

5. ERD(Entity Relationship Diagram) 

ERD ๋Š” ๊ด€๊ณ„์˜ ์˜๋ฏธ๋ฅผ ์ง๊ด€์ ์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ์ˆ˜๋‹จ์ด๋‹ค.

 

6. ๋ฐ์ดํ„ฐ ์œ ํ˜• 

๋ฐ์ดํ„ฐ ์œ ํ˜• ์„ค๋ช…
CHARACTER(s) - ๊ณ ์ • ๊ธธ์ด ๋ฌธ์ž์—ด ์ •๋ณด(Oracle, SQL Server ๋ชจ๋‘ CHAR๋กœ ํ‘œํ˜„)

- s๋Š” ๊ธฐ๋ณธ ๊ธธ์ด 1๋ฐ”์ดํŠธ, ์ตœ๋Œ€ ๊ธธ์ด Oracle 2000Byte, SQL Server 8000Byte

- s๋งŒํผ ์ตœ๋Œ€ ๊ธธ์ด, ๊ณ ์ • ๊ธธ์ด๋ฅผ ๊ฐ–๊ณ  ์žˆ์œผ๋ฏ€๋กœ ํ• ๋‹น๋œ ๋ณ€์ˆ˜ ๊ฐ’์˜ ๊ธธ์ด๊ฐ€ s๋ณด๋‹ค ์ž‘์„ ๊ฒฝ์šฐ์—๋Š” ๊ทธ ์ฐจ์ด ๊ธธ์ด๋งŒํผ ๊ณต๊ฐ„์œผ๋กœ ์ฑ„์›Œ์ง„๋‹ค.
VARCHAR(s) - Character Varying์˜ ์•ฝ์ž๋กœ ๊ฐ€๋ณ€ ๊ธธ์ด ๋ฌธ์ž์—ด ์ •๋ณด(Oracle์€ Varchar2๋กœ ํ‘œํ˜„, SQL Server๋Š” VARCHAR๋กœ ํ‘œํ˜„)

- s๋Š” ๊ธฐ๋ณธ ๊ธธ์ด 1๋ฐ”์ดํŠธ, ์ตœ๋Œ€ ๊ธธ์ด Oracle 4000Byte, SQL Server 8000Byte
NUMERIC - ์ •์ˆ˜, ์‹ค์ˆ˜ ๋“ฑ ์ˆซ์ž (Oracle์€ NUMBER๋กœ, SQL Server๋Š” 10๊ฐ€์ง€ ์ด์ƒ์˜ ์ˆซ์ž ํƒ€์ž…์„ ๊ฐ–๊ณ  ์žˆ์Œ)

- Oracle์€ ์ฒ˜์Œ์— ์ „์ฒด ์ž๋ฆฌ ์ˆ˜๋ฅผ ์ง€์ •ํ•˜๊ณ , ๊ทธ ๋‹ค์Œ ์†Œ์ˆ˜ ๋ถ€๋ถ„์˜ ์ž๋ฆฌ ์ˆ˜๋ฅผ ์ง€์ •ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ •์ˆ˜ ๋ถ€๋ถ„์ด 6์ž๋ฆฌ์ด๊ณ  ์†Œ์ˆ˜์  ๋ถ€๋ถ„์ด 2์ž๋ฆฌ์ธ ๊ฒฝ์šฐ์—๋Š” 'NUMBER(8,2)'์™€ ๊ฐ™์ด ๋œ๋‹ค.
DATETIME - ๋‚ ์งœ์™€ ์‹œ๊ฐ ์ •๋ณด(Oracle์€ DATE๋กœ ํ‘œํ˜„, SQL Server๋Š” DATETIME์œผ๋กœ ํ‘œํ˜„)

- Oracle์€ 1์ดˆ ๋‹จ์œ„, SQL Server๋Š” 3.33ms(millisecond) ๋‹จ์œ„ ๊ด€๋ฆฌ

 

โœจ์ œ2์ ˆ SELECT ๋ฌธ

์œ„๋กœ๊ฐ€๊ธฐ


1. SELECT 

์• ์Šคํ„ฐ๋ฆฌ์Šคํฌ(*) ํ•ด๋‹น ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ์นผ๋Ÿผ ์ •๋ณด๋ฅผ ๋ณด๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ์— * ๋ฅผ ์‚ฌ์šฉํ•ด ์กฐํšŒํ•œ๋‹ค.
ALL Default ์˜ต์…˜์ด๋ฏ€๋กœ ๋ณ„๋„๋กœ ํ‘œ์‹œํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. ์ค‘๋ณต๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์–ด๋„ ๋ชจ๋‘ ์ถœ๋ ฅํ•œ๋‹ค.
DISTINCT ์ค‘๋ณต๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ 1๊ฑด์œผ๋กœ ์ฒ˜๋ฆฌํ•ด ์ถœ๋ ฅํ•œ๋‹ค.

ALIAS ๋ถ€์—ฌํ•˜๊ธฐ (๋ณ„๋ช…)

  • ์นผ๋Ÿผ๋ช… ๋ฐ”๋กœ ๋’ค์— ์˜จ๋‹ค.
  • ์นผ๋Ÿผ๋ช…๊ณผ ALIAS ์‚ฌ์ด์— AS, as ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค(์˜ต์…˜).
  • ๊ณต๋ฐฑ์ด ๋“ค์–ด๊ฐ€๋Š” ๊ฒฝ์šฐ " " Double quotation ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.( SQL Server ์˜ ๊ฒฝ์šฐ " ", ' ', [ ] ์ด๋ ‡๊ฒŒ 3๊ฐ€์ง€ ๋ฐฉ์‹ )

 

2. ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž์™€ ํ•ฉ์„ฑ ์—ฐ์‚ฐ์ž 

์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž์˜ ์ข…๋ฅ˜
( ) ์—ฐ์‚ฐ์ž์˜ ์šฐ์„  ์ˆœ์œ„๋ฅผ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•œ ๊ด„ํ˜ธ ( ๊ด„ํ˜ธ ์•ˆ์˜ ์—ฐ์‚ฐ์ด ์šฐ์„ ๋œ๋‹ค )
* ๊ณฑํ•˜๊ธฐ
/ ๋‚˜๋ˆ„๊ธฐ
+ ๋”ํ•˜๊ธฐ
- ๋นผ๊ธฐ
ํ•ฉ์„ฑ(Concatenation) ์—ฐ์‚ฐ์ž ( ๐Ÿ’ก๋ฌธ์ž์—ด ํ•ฉ์น˜๊ธฐ )
|| (Oracle) ๋ฌธ์ž์™€ ๋ฌธ์ž๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒฝ์šฐ 2๊ฐœ์˜ ์ˆ˜์ง ๋ฐ”๋ฅผ ์‚ฌ์šฉ
+ (SQL Server) ๋ฌธ์ž์™€ ๋ฌธ์ž๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒฝ์šฐ + ํ‘œ์‹œ๋ฅผ ์‚ฌ์šฉ
CONCAT (Oracle, SQL Server) CONCAT(String1, String2) ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

โœจ์ œ3์ ˆ ํ•จ์ˆ˜

์œ„๋กœ๊ฐ€๊ธฐ


1. ๋‚ด์žฅ ํ•จ์ˆ˜ ๊ฐœ์š” 

SELECT, WHERE, ORDER BY ์ ˆ์— ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

2. ๋ฌธ์žํ˜• ํ•จ์ˆ˜ 

๋ฌธ์žํ˜• ํ•จ์ˆ˜ ๊ฒฐ๊ณผ ๊ฐ’ ํ•จ์ˆ˜ ์„ค๋ช…
LOWER('SQL Expert') 'sql expert' ๋ฌธ์ž์—ด์˜ ์•ŒํŒŒ๋ฒณ ๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พธ์–ด ์ค€๋‹ค.
UPPER('SQL Expert') 'SQL EXPERT' ๋ฌธ์ž์—ด์˜ ์•ŒํŒŒ๋ฒณ ๋ฌธ์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ฐ”๊พธ์–ด ์ค€๋‹ค.
ASCII('A') 65 ๋ฌธ์ž๋‚˜ ์ˆซ์ž๋ฅผ ASCII ์ฝ”๋“œ ๋ฒˆํ˜ธ๋กœ ๋ฐ”๊พธ์–ด ์ค€๋‹ค.
CHR(65) /CHAR(65) *ASCII์ฝ”๋“œ 'A' ASCII ์ฝ”๋“œ ๋ฒˆํ˜ธ๋ฅผ ๋ฌธ์ž๋‚˜ ์ˆซ์ž๋กœ ๋ฐ”๊พธ์–ด ์ค€๋‹ค.
CONCAT('RDBMS','SQL')

'RDBMS' || 'SQL' / 'RDBMS' + 'SQL'
'RDBMS SQL' ๋ฌธ์ž์—ด1๊ณผ ๋ฌธ์ž์—ด2๋ฅผ ์—ฐ๊ฒฐํ•œ๋‹ค. ํ•ฉ์„ฑ ์—ฐ์‚ฐ์ž ( || , + ) ์™€ ๋™์ผ
SUBSTR('SQL Expert', 5, 3)

SUBSTRING('SQL Expert', 5, 3)
'Exp' (๋ฌธ์ž์—ด, m[,n]) m์œ„์น˜์—์„œ n๊ฐœ์˜ ๋ฌธ์ž ๊ธธ์ด ๋ฆฌํ„ด, n์ด ์ƒ๋žต๋˜๋ฉด ๋งˆ์ง€๋ง‰ ๋ฌธ์ž ๊นŒ์ง€ ๋ฆฌํ„ดํ•œ๋‹ค.
LENGTH('SQL Expert')
LEN('SQL Expert')
10 ๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์ˆซ์ž๊ฐ’์œผ๋กœ ๋ฆฌํ„ด
LTRIM ('xxxYYZZxYZ','x') 'YYZZxYZ' ์ฒซ ๋ฌธ์ž๋ถ€ํ„ฐ ํ™•์ธ ์ง€์ • ๋ฌธ์ž๊ฐ€ ๋‚˜ํƒ€๋‚˜๋ฉด ํ•ด๋‹น ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.
SQL Server์—์„œ๋Š” LTRIM ํ•จ์ˆ˜์— ์ง€์ •๋ฌธ์ž ์‚ฌ์šฉ ๋ถˆ๊ฐ€, ๊ณต๋ฐฑ๋งŒ ์ œ๊ฑฐ๊ฐ€๋Šฅํ•˜๋‹ค.
RTRIM('XXYYzzXYzz','z')

RTRIM('XXYYZZXYZ     ')
'XXYYzzXY'

'XXYYZZXYZ'
๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ถ€ํ„ฐ ํ™•์ธ ์ง€์ • ๋ฌธ์ž๊ฐ€ ๋‚˜ํƒ€๋‚˜๋ฉด ํ•ด๋‹น ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.
SQL Server์—์„œ๋Š” LTRIM ํ•จ์ˆ˜์— ์ง€์ •๋ฌธ์ž ์‚ฌ์šฉ ๋ถˆ๊ฐ€, ๊ณต๋ฐฑ๋งŒ ์ œ๊ฑฐ๊ฐ€๋Šฅํ•˜๋‹ค.
TRIM('x' FROM 'xxYYZZxYZxx') 'YYZZxYZ' TRIM ([ leading(์ฒซ๋ฌธ์ž) | trailing(๋งˆ์ง€๋ง‰๋ฌธ์ž) | both(์–‘์ชฝ) ] ์ง€์ •๋ฌธ์ž FROM ๋ฌธ์ž์—ด )
SQL Server์—์„œ๋Š” leading, trailing, both ์‚ฌ์šฉํ• ์ˆ˜์—†๋‹ค. ์–‘์ชฝ์— ์žˆ๋Š” ์ง€์ •๋ฌธ์ž๋งŒ ์ œ๊ฑฐ ๊ฐ€๋Šฅ
default : both

3. ์ˆซ์žํ˜• ํ•จ์ˆ˜ 

์ˆซ์žํ˜• ํ•จ์ˆ˜ ์‚ฌ์šฉ ์ถœ๋ ฅ ํ•จ์ˆ˜์„ค๋ช…
ABS(-15) 15 ์ˆซ์ž์˜ ์ ˆ๋Œ“๊ฐ’์„ ๋Œ๋ ค์ค€๋‹ค.
SIGN(-20)
SIGN(0)
SIGN(+20)
-1
0
1
์ˆซ์ž๊ฐ€ ์–‘์ˆ˜์ธ์ง€, ์Œ์ˆ˜์ธ์ง€ 0์ธ์ง€๋ฅผ ๊ตฌ๋ณ„ํ•œ๋‹ค.
MOD(7,3) / 7%3 1 ์ˆซ์ž 7์„ 3์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋‚˜๋จธ์ง€ ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค. (% ์—ฐ์‚ฐ์ž ๋Œ€์ฒด ๊ฐ€๋Šฅ)
CEIL(38.123) (์˜ฌ๋ฆผ)
CEILING(38.123) 
CEILING(-38.123)
39
39
-38
์ˆซ์ž๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ์ตœ์†Œ ์ •์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.
FLOOR(38.123) (๋‚ด๋ฆผ) 38 ์ˆซ์ž๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ตœ๋Œ€ ์ •์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.
ROUND(38.5235, 3)
ROUND(38.5235, 1)
ROUND(38.5235) (๋ฐ˜์˜ฌ๋ฆผ)
38.524
38.5
39( 0 ์ด Default )
(์ˆซ์ž [,m]) ์ˆซ์ž๋ฅผ ์†Œ์ˆ˜์  m์ž๋ฆฌ์—์„œ ๋ฐ˜์˜ฌ๋ฆผํ•ด ๋ฆฌํ„ดํ•œ๋‹ค.
m์ด ์ƒ๋žต๋˜๋ฉด ๋””ํดํŠธ ๊ฐ’์€ 0
TRUNC(38.5235, 3)
TRUNC(38.5235, 1)
TRUNC(38.5235)
38.523
38.5
38( 0 ์ด Default )
(์ˆซ์ž [,m]) ์ˆซ์ž๋ฅผ ์†Œ์ˆ˜ m์ž๋ฆฌ์—์„œ ์ž˜๋ผ์„œ ๋ฒ„๋ฆฐ๋‹ค.
m์ด ์ƒ๋žต๋˜๋ฉด ๋””ํดํŠธ ๊ฐ’์€ 0

*SQL Server์—์„œ TRUNC ํ•จ์ˆ˜๋Š” ์ œ๊ณตํ•˜์ง€ ์•Š๋Š”๋‹ค.
SIN(0)
COS(0)
TAN(0)
0
1
0
์ˆซ์ž์˜ ์‚ผ๊ฐํ•จ์ˆ˜ ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค.
EXP(2) 7.3890561 ์ˆซ์ž์˜ ์ง€์ˆ˜ ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค.
์ฆ‰ e(e=2.7182813...)์˜ ์ˆซ์ž ์ œ๊ณฑ ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค(=e์ˆซ์ž).
POWER(2, 3) 8 ์ˆซ์ž์˜ ๊ฑฐ๋“ญ์ œ๊ณฑ ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค.
SQRT(4) 2 ์ˆซ์ž์˜ ์ œ๊ณฑ๊ทผ(=์ˆซ์ž) ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค.
LOG(10, 100) / LOG(100, 10) 2 LOG(์ˆซ์ž 1, ์ˆซ์ž2)  ์ˆซ์ž1์„ ๋ฐ‘์ˆ˜๋กœ ํ•˜๋Š” ์ˆซ์ž2์˜ ๋กœ๊ทธ ๊ฐ’(=LOG์ˆซ์ž1์ˆซ์ž2)์„ ๋ฆฌํ„ดํ•œ๋‹ค.
*SQL Server๋Š” ์ˆซ์ž2๋ฅผ ๋ฐ‘์ˆ˜๋กœ ํ•˜๋Š” ์ˆซ์ž1์˜ ๋กœ๊ทธ ๊ฐ’(=LOG์ˆซ์ž2์ˆซ์ž1)
LN(7.3890561) 2 ์ˆซ์ž์˜ ์ž์—ฐ ๋กœ๊ทธ ๊ฐ’(=LOGe์ˆซ์ž)์„ ๋ฆฌํ„ดํ•œ๋‹ค.
*SQL Server์—์„œ LV ํ•จ์ˆ˜๋Š” ์ œ๊ณตํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

4. ๋‚ ์งœํ˜• ํ•จ์ˆ˜ 

๋‚ ์งœํ˜• ํ•จ์ˆ˜ ํ•จ์ˆ˜ ์„ค๋ช…
SYSDATE / GETDATE() ํ˜„์žฌ ๋‚ ์งœ์™€ ์‹œ๊ฐ์„ ์ถœ๋ ฅํ•œ๋‹ค.
EXTRACT('YEAR'|'MONTH'|'DAY'| from d)
/ DATEPART('YEAR'|'MONTH'|'DAY',d)
๋‚ ์งœ ๋ฐ์ดํ„ฐ์—์„œ ์—ฐ์›”์ผ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.
์‹œ๋ถ„์ดˆ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.
TO_NUMBER(TO_CHAR(d, 'YYYY')) / YEAR(d),
TO_NUMBER(TO_CHAR(d, 'MM')) / MONTH(d),
TO_NUMBER(TO_CHAR(d, 'DD')) / DAY(d)
๋‚ ์งœ ๋ฐ์ดํ„ฐ์—์„œ ์—ฐ์›”์ผ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.
Oracle EXTRACT YEAR/MONTH/DAY ์˜ต์…˜์ด๋‚˜ SQL Server DEPART YEAR/MONTH/DAY ์˜ต์…˜๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ์ด๋‹ค.
TO_NUMBER ํ•จ์ˆ˜ ์ œ์™ธ ์‹œ ๋ฌธ์žํ˜•์œผ๋กœ ์ถœ๋ ฅ๋œ๋‹ค.

 

5. ๋ณ€ํ™˜ํ˜• ํ•จ์ˆ˜ 

๋ฐ์ดํ„ฐ ์œ ํ˜• ๋ณ€ํ™˜์˜ ์ข…๋ฅ˜

  • ๋ช…์‹œ์ (Explicit) : ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜ํ˜• ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ ์œ ํ˜•์„ 
  • ์•”์‹œ์ (Implicit) : ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์ž๋™์œผ๋กœ ๋ฐ์ดํ„ฐ ์œ ํ˜•์„ ๋ณ€ํ™˜ํ•ด ๊ณ„์‚ฐํ•˜๋Š” ๊ฒฝ์šฐ (์„ฑ๋Šฅ ์ €ํ•˜๊ฐ€ ๋ฐœ์ƒํ• ์ˆ˜ ์žˆ๋‹ค.)

๋ช…์‹œ์  ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜ํ˜• ํ•จ์ˆ˜์˜ ์ข…๋ฅ˜

๋ณ€ํ™˜ํ˜• ํ•จ์ˆ˜ ํ•จ์ˆ˜ ์„ค๋ช…
TO_NUMBER (๋ฌธ์ž์—ด)
/ CAST (expression AS data_type [(length)])
์ˆซ์ž๋กœ ๋ณ€ํ™˜ ๊ฐ€๋Šฅํ•œ ๋ฌธ์ž์—ด์„ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
expression์„ ๋ชฉํ‘œ ๋ฐ์ดํ„ฐ ์œ ํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
TO_CHAR (์ˆซ์ž|๋‚ ์งœ [, FORMAT])
/ CONVERT (data_type [(length)], expression [,style])
์ˆซ์ž๋‚˜ ๋‚ ์งœ๋ฅผ ์ฃผ์–ด์ง„ FORMAT ํ˜•ํƒœ์ธ ๋ฌธ์ž์—ด ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
expression์„ ์ฃผ์–ด์ง„ style ํ˜•ํƒœ์ธ ๋ชฉํ‘œ ๋ฐ์ดํ„ฐ ์œ ํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
TO_DATE (๋ฌธ์ž์—ด [, FORMAT])
/ CONVERT (data_type [(length)], expression [,style])
๋ฌธ์ž์—ด์„ ์ฃผ์–ด์ง„ FORMAT ํ˜•ํƒœ์ธ ๋‚ ์งœ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.

 

6. CASE ํ‘œํ˜„ 

๋‹จ์ผํ–‰ CASE ํ‘œํ˜„์˜ ์ข…๋ฅ˜

CASE ํ‘œํ˜„ ํ•จ์ˆ˜ ์„ค๋ช…
CASE
SIMPLE_CASE_EXPRESSION ์กฐ๊ฑด
[ELSE ๋””ํดํŠธ๊ฐ’]
END
SIMPLE_CASE_EXPRESSION ์กฐ๊ฑด์ด ๋งž์œผ๋ฉด SIMPLE_CASE_EXPRESSION ์กฐ๊ฑด ๋‚ด์˜ THEN ์ ˆ์„ ์ˆ˜ํ–‰ํ•˜๊ณ , ์กฐ๊ฑด์ด ๋งž์ง€ ์•Š์œผ๋ฉด ELSE ์ ˆ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
CASE
SEARCHED_CASE_EXPRESSION ์กฐ๊ฑด
[ELSE ๋””ํดํŠธ ๊ฐ’]
END
SEARCHED_CASE_EXPRESSION ์กฐ๊ฑด์ด ๋งž์œผ๋ฉด SEARCHED_CASE_EXPRESSION ์กฐ๊ฑด ๋‚ด์˜ THEN ์ ˆ์„ ์ˆ˜ํ–‰ํ•˜๊ณ , ์กฐ๊ฑด์ด ๋งž์ง€ ์•Š์œผ๋ฉด ELSE ์ ˆ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
DECODE(ํ‘œํ˜„์‹, ๊ธฐ์ค€๊ฐ’1, ๊ฐ’1
[, ๊ธฐ์ค€๊ฐ’2, ๊ฐ’2,..., ๋””ํดํŠธ ๊ฐ’])
(if ๋ฌธ) CASE ํ‘œํ˜„์˜ SIMPLE_CASE_EXPRESSION ์กฐ๊ฑด๊ณผ ๋™์ผํ•˜๋‹ค.
*Oracle ์—์„œ ์‚ฌ์šฉ๊ฐ€๋Šฅ
CASE
	WHEN [์กฐ๊ฑด] THEN [ ์ˆ˜ํ–‰๊ฐ’ ]
	ELSE [DEFAULT]
END
DECODE(ํ‘œํ˜„์‹, ๊ฐ’, Default)

 

7. NULL ๊ด€๋ จ ํ•จ์ˆ˜ 

์ผ๋ฐ˜ํ˜• ํ•จ์ˆ˜ ํ•จ์ˆ˜ ์„ค๋ช…
NVL(ํ‘œํ˜„์‹1, ํ‘œํ˜„์‹2) /
ISNULL(ํ‘œํ˜„์‹1, ํ‘œํ˜„์‹2)
ํ‘œํ˜„์‹1์˜ ๊ฒฐ๊ณผ ๊ฐ’์ด NULL์ด๋ฉด ํ‘œํ˜„์‹2์˜ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.
๋‹จ ํ‘œํ˜„์‹1๊ณผ ํ‘œํ˜„์‹2์˜ ๊ฒฐ๊ณผ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ๊ฐ™์•„์•ผ ํ•œ๋‹ค.
NULL ๊ด€๋ จ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ํ•จ์ˆ˜์ด๋ฏ€๋กœ ์ƒ๋‹นํžˆ ์ค‘์š”ํ•˜๋‹ค.
NULLIF(ํ‘œํ˜„์‹1, ํ‘œํ˜„์‹2) ํ‘œํ˜„์‹1์ด ํ‘œํ˜„์‹2์™€ ๊ฐ™์œผ๋ฉด NULL์„, ๊ฐ™์ง€ ์•Š์œผ๋ฉด ํ‘œํ˜„์‹1์„ ๋ฆฌํ„ดํ•œ๋‹ค.
COALESCE(ํ‘œํ˜„์‹1, ํ‘œํ˜„์‹2, ....) ์ž„์˜์˜ ๊ฐœ์ˆ˜ ํ‘œํ˜„์‹์—์„œ NULL์ด ์•„๋‹Œ ์ตœ์ดˆ์˜ ํ‘œํ˜„์‹์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.
๋ชจ๋“  ํ‘œํ˜„์‹์ด NULL์ด๋ผ๋ฉด NULL์„ ๋ฆฌํ„ดํ•œ๋‹ค.

๐Ÿ“Œ Oracle ํ•จ์ˆ˜ / SQL Server ํ•จ์ˆ˜ ํ‘œ์‹œ, '/' ์—†๋Š” ๊ฒƒ์€ ๊ณตํ†ต ํ•จ์ˆ˜

 

โœจ์ œ4์ ˆ WHERE ์ ˆ

์œ„๋กœ๊ฐ€๊ธฐ


1. WHERE ์กฐ๊ฑด์ ˆ ๊ฐœ์š” 

SELECT [DISTINCT/ALL]
	์นผ๋Ÿผ๋ช… [ALIAS๋ช…]
FROM ํ…Œ์ด๋ธ”๋ช…
WHERE ์กฐ๊ฑด์‹;

2. ์—ฐ์‚ฐ์ž์˜ ์ข…๋ฅ˜ 

- ๋น„๊ต ์—ฐ์‚ฐ์ž(๋ถ€์ • ๋น„๊ต ์—ฐ์‚ฐ์ž ํฌํ•จ)

- SQL ์—ฐ์‚ฐ์ž(๋ถ€์ • SQL ์—ฐ์‚ฐ์ž ํฌํ•จ)

- ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž

์—ฐ์‚ฐ์ž ์šฐ์„ ์ˆœ์œ„ ์„ค๋ช…
1 ๊ด„ํ˜ธ ()
2 ๋น„๊ต ์—ฐ์‚ฐ์ž, SQL ์—ฐ์‚ฐ์ž
3 NOT ์—ฐ์‚ฐ์ž
4 AND
5 OR

 

3. ๋น„๊ต ์—ฐ์‚ฐ์ž 

๋น„๊ต ์—ฐ์‚ฐ์ž์˜ ์ข…๋ฅ˜ ์—ฐ์‚ฐ์ž์˜ ์˜๋ฏธ
= ๊ฐ™๋‹ค.
> ๋ณด๋‹ค ํฌ๋‹ค.
>= ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค.
< ๋ณด๋‹ค ์ž‘๋‹ค.
<= ๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค.

โ—‡ ๋ฌธ์ž ์œ ํ˜• ๋น„๊ต ๋ฐฉ๋ฒ• โ—‡

๊ตฌ๋ถ„ ๋น„๊ต๋ฐฉ๋ฒ•
๋น„๊ต ์—ฐ์‚ฐ์ž์˜ ์–‘์ชฝ์ด ๋ชจ๋‘ CHAR ์œ ํ˜• ํƒ€์ž…์ธ ๊ฒฝ์šฐ ๊ธธ์ด๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅธ CHARํ˜• ํƒ€์ž…์ด๋ฉด ์ž‘์€ ์ชฝ์— ์ŠคํŽ˜์ด์Šค๋ฅผ ์ถ”๊ฐ€ํ•ด ๊ธธ์ด๋ฅผ ๊ฐ™๊ฒŒ ํ•œ ํ›„ ๋น„๊ตํ•œ๋‹ค.
์„œ๋กœ ๋‹ค๋ฅธ ๋ฌธ์ž๊ฐ€ ๋‚˜์˜ฌ ๋•Œ๊นŒ์ง€ ๋น„๊ตํ•œ๋‹ค.
๋‹ฌ๋ผ์ง„ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž์˜ ๊ฐ’์— ๋”ฐ๋ผ ํฌ๊ธฐ๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค.
๋ฌธ์ž ๋ ๋ธ”๋žญํฌ ์ˆ˜๋งŒ ๋‹ค๋ฅด๋‹ค๋ฉด ์„œ๋กœ ๊ฐ™์€ ๊ฐ’์œผ๋กœ ๊ฒฐ์ •ํ•œ๋‹ค.
๋น„๊ต ์—ฐ์‚ฐ์ž์˜ ์–ด๋Š ํ•œ ์ชฝ์ด VARCHAR ์œ ํ˜• ํƒ€์ž…์ธ ๊ฒฝ์šฐ ์„œ๋กœ ๋‹ค๋ฅธ ๋ฌธ์ž๊ฐ€ ๋‚˜์˜ฌ ๋•Œ๊นŒ์ง€ ๋น„๊ตํ•œ๋‹ค.
๊ธธ์ด๊ฐ€ ๋‹ค๋ฅด๋‹ค๋ฉด ์งง์€ ๊ฒƒ์ด ๋๋‚  ๋•Œ๊นŒ์ง€๋งŒ ๋น„๊ตํ•œ ํ›„ ๊ธธ์ด๊ฐ€ ๊ธด ๊ฒƒ์ด ํฌ๋‹ค๊ณ  ํŒ๋‹จํ•œ๋‹ค.
๊ธธ์ด๊ฐ€ ๊ฐ™๊ณ  ๋‹ค๋ฅธ ๊ฒƒ์ด ์—†๋‹ค๋ฉด ๊ฐ™๋‹ค๊ณ  ํŒ๋‹จํ•œ๋‹ค.
VARCHAR๋Š” NOT NULL๊นŒ์ง€ ๊ธธ์ด๋ฅผ ๋งํ•œ๋‹ค. ์ฆ‰ ๋ฌธ์ž ๋ ๋ธ”๋žญํฌ๋„ ๋ฌธ์ž๋กœ ์ทจ๊ธ‰ํ•œ๋‹ค.
์ƒ์ˆ˜๊ฐ’๊ณผ ๋น„๊ตํ•  ๊ฒฝ์šฐ ์ƒ์ˆ˜ ์ชฝ์„ ๋ณ€์ˆ˜ ํƒ€์ž…๊ณผ ๋™์ผํ•˜๊ฒŒ ๋ฐ”๊พธ๊ณ  ๋น„๊ตํ•œ๋‹ค.
๋ณ€์ˆ˜ ์ชฝ์ด CHAR ์œ ํ˜• ํƒ€์ž…์ด๋ฉด ์œ„์˜ CHAR ์œ ํ˜• ํƒ€์ž…์˜ ๊ฒฝ์šฐ๋ฅผ ์ ์šฉํ•œ๋‹ค.
๋ณ€์ˆ˜ ์ชฝ์ด VARCHAR ์œ ํ˜• ํƒ€์ž…์ด๋ฉด ์œ„์˜ VARCHAR ์œ ํ˜• ํƒ€์ž…์˜ ๊ฒฝ์šฐ๋ฅผ ์ ์šฉํ•œ๋‹ค.

 

4. SQL ์—ฐ์‚ฐ์ž 

SQL ์—ฐ์‚ฐ์ž์˜ ์ข…๋ฅ˜ ์—ฐ์‚ฐ์ž์˜ ์˜๋ฏธ
BETWEEN a AND b a์™€ b์˜ ๊ฐ’ ์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ–๋Š”๋‹ค(a์™€ b๊ฐ’์ด ํฌํ•จ๋จ)
IN (list) ๋ฆฌ์ŠคํŠธ์— ์žˆ๋Š” ๊ฐ’ ์ค‘์—์„œ ์–ด๋Š ํ•˜๋‚˜๋ผ๋„ ์ผ์น˜ํ•œ๋‹ค.
LIKE '๋น„๊ต๋ฌธ์ž์—ด' ๋น„๊ต๋ฌธ์ž์—ด๊ณผ ํ˜•ํƒœ๊ฐ€ ์ผ์น˜ํ•œ๋‹ค.
% = 0๊ฐœ ์ด์ƒ์˜ ์–ด๋–ค ๋ฌธ์ž๋ฅผ ์˜๋ฏธ (%ํฌํ•จ%,์‹œ์ž‘%,%๋งˆ์ง€๋ง‰)
_ = 1๊ฐœ์˜ ๋‹จ์ผ ๋ฌธ์ž๋ฅผ ์˜๋ฏธ
IS NULL NULL ๊ฐ’์ธ ๊ฒฝ์šฐ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

 

5. ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž 

๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž์˜ ์ข…๋ฅ˜ ์—ฐ์‚ฐ์ž์˜ ์˜๋ฏธ
AND ์•ž์— ์žˆ๋Š” ์กฐ๊ฑด๊ณผ ๋’ค์— ์˜ค๋Š” ์กฐ๊ฑด์ด ์ฐธ(TRUE)์ด ๋˜๋ฉด ๊ฒฐ๊ณผ๋„ ์ฐธ(TRUE)์ด ๋œ๋‹ค.
์ฆ‰ ์•ž์˜ ์กฐ๊ฑด๊ณผ ๋’ค์˜ ์กฐ๊ฑด์„ ๋™์‹œ์— ๋งŒ์กฑํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 
OR ์•ž์˜ ์กฐ๊ฑด์ด ์ฐธ(TRUE)์ด๊ฑฐ๋‚˜ ๋’ค์˜ ์กฐ๊ฑด์ด ์ฐธ(TRUE)์ด ๋˜๋ฉด ๊ฒฐ๊ณผ๋„ ์ฐธ(TRUE)์ด ๋œ๋‹ค.
์ฆ‰ ์•ž๋’ค์˜ ์กฐ๊ฑด ์ค‘ ํ•˜๋‚˜๋งŒ ์ฐธ(TRUE)์ด๋ฉด ๋œ๋‹ค. 
NOT ๋’ค์— ์˜ค๋Š” ์กฐ๊ฑด์— ๋ฐ˜๋Œ€๋˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ๋˜๋Œ๋ ค ์ค€๋‹ค. 

 

6. ๋ถ€์ • ์—ฐ์‚ฐ์ž 

๋ถ€์ • ์—ฐ์‚ฐ์ž์˜ ์ข…๋ฅ˜ ์—ฐ์‚ฐ์ž ์—ฐ์‚ฐ์ž์˜ ์˜๋ฏธ
๋ถ€์ • ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž != ๊ฐ™์ง€ ์•Š๋‹ค.
^= ๊ฐ™์ง€ ์•Š๋‹ค.
<> ๊ฐ™์ง€ ์•Š๋‹ค(ANIS/ISO ํ‘œ์ค€, ๋ชจ๋“  OS์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ)
NOT ์นผ๋Ÿผ๋ช… = ~์™€ ๊ฐ™์ง€ ์•Š๋‹ค.
NOT ์นผ๋Ÿผ๋ช… > ~๋ณด๋‹ค ํฌ์ง€ ์•Š๋‹ค.
๋ถ€์ • SQL ์—ฐ์‚ฐ์ž NOT BETWEEN a AND b a์™€ b์˜ ๊ฐ’ ์‚ฌ์ด์— ์žˆ์ง€ ์•Š๋‹ค(a, b๊ฐ’์„ ํฌํ•จํ•˜์ง€ ์•Š์Œ)
NOT IN (list) list ๊ฐ’๊ณผ ์ผ์น˜ํ•˜์ง€ ์•Š๋‹ค.
IS NOT NULL NULL ์ด ์•„๋‹ˆ๋‹ค.

โœจ์ œ5์ ˆ GROUP BY, HAVING ์ ˆ

์œ„๋กœ๊ฐ€๊ธฐ


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

์ง‘๊ณ„ํ•จ์ˆ˜์˜ ์ข…๋ฅ˜ ์‚ฌ์šฉ ๋ชฉ์ 
COUNT(*) NULL ๊ฐ’์„ ํฌํ•จํ•œ ํ–‰์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅ
COUNT(ํ‘œํ˜„์‹) ํ‘œํ˜„์‹์˜ ๊ฐ’์ด NULL ๊ฐ’์ธ ๊ฒƒ์„ ์ œ์™ธํ•œ ํ–‰ ์ˆ˜๋ฅผ ์ถœ๋ ฅ
SUM([DISTINCT | ALL] ํ‘œํ˜„์‹) ํ‘œํ˜„์‹์˜ NULL ๊ฐ’์„ ์ œ์™ธํ•œ ํ•ฉ๊ณ„๋ฅผ ์ถœ๋ ฅ
AVG([DISTINCT | ALL] ํ‘œํ˜„์‹) ํ‘œํ˜„์‹์˜ NULL ๊ฐ’์„ ์ œ์™ธํ•œ ํ‰๊ท ์„ ์ถœ๋ ฅ
MAX([DISTINCT | ALL] ํ‘œํ˜„์‹) ํ‘œํ˜„์‹์˜ ์ตœ๋Œ“๊ฐ’์„ ์ถœ๋ ฅ (๋ฌธ์ž, ๋‚ ์งœ ๋ฐ์ดํ„ฐ ํƒ€์ž…๋„ ๊ฐ€๋Šฅ)
MIN([DISTINCT | ALL] ํ‘œํ˜„์‹) ํ‘œํ˜„์‹์˜ ์ตœ์†Œ๊ฐ’์„ ์ถœ๋ ฅ (๋ฌธ์ž, ๋‚ ์งœ ๋ฐ์ดํ„ฐ ํƒ€์ž…๋„ ๊ฐ€๋Šฅ)
STDDEV([DISTINCT | ALL] ํ‘œํ˜„์‹) ํ‘œํ˜„์‹์˜ ํ‘œ์ค€ ํŽธ์ฐจ๋ฅผ ์ถœ๋ ฅ
VARIANCE/VAR([DISTINCT | ALL] ํ‘œํ˜„์‹) ํ‘œํ˜„์‹์˜ ๋ถ„์‚ฐ์„ ์ถœ๋ ฅ
๊ธฐํƒ€ ํ†ต๊ณ„ ํ•จ์ˆ˜ ๋ฒค๋”๋ณ„๋กœ ๋‹ค์–‘ํ•œ ํ†ต๊ณ„์‹์„ ์ œ๊ณต

2. GROUP BY ์ ˆ 

- GROUP BY ์ ˆ์€ SQL๋ฌธ์—์„œ FROM์ ˆ๊ณผ WHERE ์ ˆ ๋’ค์— ์˜ค๋ฉฐ,

๋ฐ์ดํ„ฐ๋“ค์„ ์ž‘์€ ๊ทธ๋ฃน์œผ๋กœ ๋ถ„๋ฅ˜ํ•ด ์†Œ๊ทธ๋ฃน์— ๋Œ€ํ•œ ํ•ญ๋ชฉ๋ณ„ ํ†ต๊ณ„ ์ •๋ณด๋ฅผ ์–ป์„ ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค.

( GROUP BY ์ ˆ์—๋Š” ALIAS ๋ช…์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.)

SELECT [DISTINCT] ์นผ๋Ÿผ๋ช… [ALIAS๋ช…]
FROM ํ…Œ์ด๋ธ”๋ช…
[WHERE ์กฐ๊ฑด์‹]
[GROUP BY ์นผ๋Ÿผ(Column) ์ด๋‚˜ ํ‘œํ˜„์‹]
[HAVING ๊ทธ๋ฃน ์กฐ๊ฑด์‹];

 

3. HAVING ์ ˆ 

SELECT [DISTINCT] ์นผ๋Ÿผ๋ช… [ALIAS๋ช…]
FROM ํ…Œ์ด๋ธ”๋ช…
[WHERE ์กฐ๊ฑด์‹]
[GROUP BY ์นผ๋Ÿผ(Column) ์ด๋‚˜ ํ‘œํ˜„์‹]
[์˜ˆ์‹œ]
HAVING AVG (์กฐ๊ฑด) >= ์กฐ๊ฑด;

HAVING ์ ˆ์€ ์ง‘๊ณ„ํ•จ์ˆ˜ ์‚ฌ์šฉ๊ฐ€๋Šฅ, WHERE ์ ˆ์€ ์ง‘๊ณ„ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ• ์ˆ˜ ์—†๋‹ค.

 

4. CASE ํ‘œํ˜„์„ ํ™œ์šฉํ•œ ์›”๋ณ„ ๋ฐ์ดํ„ฐ ์ง‘๊ณ„ 

 

โ—‡ Step 1. ๋จผ์ € ๊ฐœ๋ณ„ ์ž…์‚ฌ์ •๋ณด์—์„œ ์›”๋ณ„ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•œ๋‹ค. (๊ฐœ๋ณ„ ๋ฐ์ดํ„ฐ ํ™•์ธ)

๐Ÿ‘‡Oracle

SELECT ENAME AS ์‚ฌ์›๋ช…, DEPTNO AS ๋ถ€์„œ๋ฒˆํ˜ธ
	, EXTRACT(MONTH FROM HIREDATE) AS ์ž…์‚ฌ์›”, SAL AS ๊ธ‰์—ฌ
FROM EMP;

๐Ÿ‘‡SQL Server

SELECT ENAME AS ์‚ฌ์›๋ช…, DEPTNO AS ๋ถ€์„œ๋ฒˆํ˜ธ
	, DATEPART(MONTH, HIREDATE) AS ์ž…์‚ฌ์›”, SAL AS ๊ธ‰์—ฌ
FROM EMP;

SELECT ENAME AS ์‚ฌ์›๋ช…, DEPTNO AS ๋ถ€์„œ๋ฒˆํ˜ธ
	, MONTH(HIREDATE) AS ์ž…์‚ฌ์›”, SAL AS ๊ธ‰์—ฌ
FROM EMP;

โ—‡ Step 2. ์ถ”์ถœ๋œ ์›”๋ณ„ ๋ฐ์ดํ„ฐ๋ฅผ Simple Case Expression์„ ์ด์šฉํ•ด 12๊ฐœ์˜ ์›”๋ณ„ ์นผ๋Ÿผ์œผ๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค.

๐Ÿ‘‡Oracle

SELECT ENAME AS ์‚ฌ์›๋ช…, DEPTNO AS ๋ถ€์„œ๋ฒˆํ˜ธ
	,CASE MONTH WHEN  1 THEN SAL END AS M01, CASE MONTH WHEN 2 THEN SAL END AS M02
	,CASE MONTH WHEN  3 THEN SAL END AS M03, CASE MONTH WHEN 4 THEN SAL END AS M04
	,CASE MONTH WHEN  5 THEN SAL END AS M05, CASE MONTH WHEN 6 THEN SAL END AS M06
	,CASE MONTH WHEN  7 THEN SAL END AS M07, CASE MONTH WHEN 8 THEN SAL END AS M08
	,CASE MONTH WHEN  9 THEN SAL END AS M09, CASE MONTH WHEN 10 THEN SAL END AS M10
	,CASE MONTH WHEN 11 THEN SAL END AS M11, CASE MONTH WHEN 12 THEN SAL END AS M12
FROM (SELECT ENAME, DEPTNO, EXTRACT (MONTH FROM HIREDATE) AS MONTH, SAL FROM EMP);

โ—‡ Step 2. ๋ถ€์„œ๋ณ„ ํ‰๊ท ๊ฐ’์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด GROUP BY์ ˆ๊ณผ AVG ์ง‘๊ณ„ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

๐Ÿ‘‡Oracle

SELECT ENAME AS ์‚ฌ์›๋ช…, DEPTNO AS ๋ถ€์„œ๋ฒˆํ˜ธ
	,AVG (DECODE (MONTH, 1, SAL)) AS M01, AVG (DECODE (MONTH, 2, SAL)) AS M02
	,AVG (DECODE (MONTH, 3, SAL)) AS M03, AVG (DECODE (MONTH, 4, SAL)) AS M04
	,AVG (DECODE (MONTH, 5, SAL)) AS M05, AVG (DECODE (MONTH, 6, SAL)) AS M06
	,AVG (DECODE (MONTH, 7, SAL)) AS M07, AVG (DECODE (MONTH, 8, SAL)) AS M08
	,AVG (DECODE (MONTH, 9, SAL)) AS M09, AVG (DECODE (MONTH,10, SAL)) AS M10
	,AVG (DECODE (MONTH,11, SAL)) AS M11, AVG (DECODE (MONTH,12, SAL)) AS M12
FROM (SELECT ENAME, DEPTNO, EXTRACT (MONTH FROM HIREDATE) AS MONTH, SAL FROM EMP)
GROUP BY DEPTNO;

5. ์ง‘๊ณ„ํ•จ์ˆ˜์™€ NULL ์ฒ˜๋ฆฌ 

Oracle NVL
SQL Server ISNULL
SELECT ์นผ๋Ÿผ๋ช… 
	,NVL (์นผ๋Ÿผ(Column) ์ด๋‚˜ ํ‘œํ˜„์‹, default๊ฐ’) AS [ALIAS๋ช…]
FROM ํ…Œ์ด๋ธ”๋ช…
[GROUP BY ์นผ๋Ÿผ(Column) ์ด๋‚˜ ํ‘œํ˜„์‹];

 

โœจ์ œ6์ ˆ ORDER BY ์ ˆ

์œ„๋กœ๊ฐ€๊ธฐ


1. ORDER BY ์ •๋ ฌ 

SELECT ์นผ๋Ÿผ๋ช… [ALIAS๋ช…]
FROM ํ…Œ์ด๋ธ”๋ช…
[WHERE ์กฐ๊ฑด์‹]
[GROUP BY ์นผ๋Ÿผ(Column)์ด๋‚˜ ํ‘œํ˜„์‹]
[HAVING ๊ทธ๋ฃน์กฐ๊ฑด์‹]
[ORDER BY ์นผ๋Ÿผ(Column)์ด๋‚˜ ํ‘œํ˜„์‹ [ASC ๋˜๋Š” DESC]];

- ASC (Ascending) : ์กฐํšŒํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค(๊ธฐ๋ณธ ๊ฐ’์ด๋ฏ€๋กœ ์ƒ๋žต ๊ฐ€๋Šฅ)
- DESC (Descending) : ์กฐํšŒํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.

 

2. SELECT ๋ฌธ์žฅ ์‹คํ–‰ ์ˆœ์„œ 

SELECT ์นผ๋Ÿผ๋ช… [ALIAS๋ช…]
	FROM ํ…Œ์ด๋ธ”๋ช…
    WHERE ์กฐ๊ฑด์‹
    GROUP BY ์นผ๋Ÿผ(Column)์ด๋‚˜ ํ‘œํ˜„์‹
    HAVING ๊ทธ๋ฃน์กฐ๊ฑด์‹
    ORDER BY ์นผ๋Ÿผ(Column)์ด๋‚˜ ํ‘œํ˜„์‹;

 

โœจ์ œ7์ ˆ ์กฐ์ธ

์œ„๋กœ๊ฐ€๊ธฐ


1. ์กฐ์ธ๊ฐœ์š” 

๋‘ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”๋“ค์„ ์—ฐ๊ฒฐํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์„ ์กฐ์ธ(JOIN)์ด๋ผ๊ณ  ํ•œ๋‹ค.

 

2. EQUI(๋“ฑ๊ฐ€) JOIN 

- EQUI JOIN

SELECT ํ…Œ์ด๋ธ”1.์นผ๋Ÿผ๋ช…, ํ…Œ์ด๋ธ”2.์นผ๋Ÿผ๋ช…, . . .
FROM ํ…Œ์ด๋ธ”1, ํ…Œ์ด๋ธ”2
WHERE ํ…Œ์ด๋ธ”2.์นผ๋Ÿผ๋ช… = ํ…Œ์ด๋ธ”1.์นผ๋Ÿผ๋ช…; -- WHERE ์ ˆ์— JOIN ์กฐ๊ฑด์„ ๊ธฐ์ˆ ํ•œ๋‹ค.
- ANSI/ISO SQL ํ‘œ์ค€ ๋ฐฉ์‹

SELECT ํ…Œ์ด๋ธ”1.์นผ๋Ÿผ๋ช…, ํ…Œ์ด๋ธ”2.์นผ๋Ÿผ๋ช…, . . .
FROM ํ…Œ์ด๋ธ”1 INNER JOIN ํ…Œ์ด๋ธ”2
ON ํ…Œ์ด๋ธ”2.์นผ๋Ÿผ๋ช… = ํ…Œ์ด๋ธ”1.์นผ๋Ÿผ๋ช…; -- ON ์ ˆ์— JOIN ์กฐ๊ฑด์„ ๊ธฐ์ˆ ํ•œ๋‹ค.

 

3. Non EQUI JOIN 

- Non EQUI JOIN ( Between, >, >=, <, <= ๋“ฑ )

SELECT ํ…Œ์ด๋ธ”1.์นผ๋Ÿผ๋ช…, ํ…Œ์ด๋ธ”2.์นผ๋Ÿผ๋ช…, . . .
FROM ํ…Œ์ด๋ธ”1, ํ…Œ์ด๋ธ”2
WHERE ํ…Œ์ด๋ธ”1.์นผ๋Ÿผ๋ช… BETWEEN ํ…Œ์ด๋ธ”2.์นผ๋Ÿผ๋ช…1 AND ํ…Œ์ด๋ธ”2.์นผ๋Ÿผ๋ช…2;

 

4. 3๊ฐœ ์ด์ƒ TABLE JOIN 

WHERE ์ ˆ์— 2๊ฐœ ์ด์ƒ์˜ ์กฐ์ธ ์กฐ๊ฑด์ด ํ•„์š”ํ•˜๋‹ค.

 

5. OUTER JOIN 

์•ž์„œ ๋‹ค๋ฃจ์—ˆ๋˜ EQUI JOIN, Non EQUI JOIN์€ ๋ชจ๋‘ ์กฐ์ธ ์กฐ๊ฑด์˜ ๊ฒฐ๊ณผ๊ฐ€ ์ฐธ(TRUE)์ธ ํ–‰๋“ค๋งŒ ๋ฐ˜ํ™˜ํ•˜๋Š” INNER(๋‚ด๋ถ€)์กฐ์ธ์ด๋‹ค.

OUTER(์™ธ๋ถ€) JOIN์€ ์กฐ์ธ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ์•Š๋Š” ํ–‰๋“ค๋„ ํ•จ๊ป˜ ๋ฐ˜ํ™˜ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

 

๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ํฐ ์žฅ์ ์ด๋ฉด์„œ SQL ํŠœ๋‹์˜ ์ค‘์š” ๋Œ€์ƒ์ด ๋˜๋Š” ์กฐ์ธ์„ ์ž˜๋ชป ๊ธฐ์ˆ ํ•˜๋ฉด,

์‹œ์Šคํ…œ ์ž์› ๋ถ€์กฑ์ด๋‚˜ ๊ณผ๋‹คํ•œ ์‘๋‹ต์‹œ๊ฐ„ ์ง€์—ฐ์„ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ์ค‘์š” ์›์ธ์ด ๋˜๋ฏ€๋กœ ์กฐ์ธ ์กฐ๊ฑด์€ ์‹ ์ค‘ํ•˜๊ฒŒ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.

 

 

โœจ์ œ8์ ˆ ํ‘œ์ค€ ์กฐ์ธ

์œ„๋กœ๊ฐ€๊ธฐ


1. FROM ์ ˆ ์กฐ์ธ ํ˜•ํƒœ 

- INNER JOIN

- NATURAL JOIN

- USING ์กฐ๊ฑด์ ˆ

- ON ์กฐ๊ฑด์ ˆ

- CROSS JOIN

- OUTER JOIN

 

2. INNER JOIN 

- INNER JOIN์€ OUTER(์™ธ๋ถ€) ์กฐ์ธ๊ณผ ๋Œ€๋น„ํ•ด ๋‚ด๋ถ€ ์กฐ์ธ์ด๋ผ๊ณ  ํ•˜๋ฉฐ ์กฐ์ธ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ํ–‰๋“ค๋งŒ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

3. NATURAL JOIN 

- NATURAL JOIN์€ ๋‘ ํ…Œ์ด๋ธ” ๊ฐ„์— ๋™์ผํ•œ ์ด๋ฆ„์„ ๊ฐ–๋Š” ๋ชจ๋“  ์นผ๋Ÿผ๋“ค์— ๋Œ€ํ•ด EQUI(=) JOIN์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

- NATURAL JOIN์ด ๋ช…์‹œ๋˜๋ฉด ์ถ”๊ฐ€๋กœ USING ์กฐ๊ฑด์ ˆ, ON ์กฐ๊ฑด์ ˆ, WHERE ์ ˆ์—์„œ ์กฐ์ธ ์กฐ๊ฑด์„ ์ •์˜ํ•  ์ˆ˜ ์—†๋‹ค.

- SQL Server ๋Š” ์ง€์›ํ•˜์ง€ ์•Š์Œ

- ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ ์„ฑ๊ฒฉ(๋„๋ฉ”์ธ)๊ณผ ์นผ๋Ÿผ๋ช… ๋“ฑ์ด ๋™์ผํ•ด์•ผ ํ•˜๋Š” ์ œ์•ฝ์กฐ๊ฑด์ด ์žˆ๋‹ค.

 

4. USING ์กฐ๊ฑด์ ˆ 

- SQL Server ๋Š” ์ง€์›ํ•˜์ง€ ์•Š์Œ

- FROM ์ ˆ์˜ USING ์กฐ๊ฑด์ ˆ์„ ์ด์šฉํ•˜๋ฉด ์›ํ•˜๋Š” ์นผ๋Ÿผ์— ๋Œ€ํ•ด์„œ๋งŒ EQUI JOIN์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

SELECT *
	FROM [ํ…Œ์ด๋ธ”1] JOIN [ํ…Œ์ด๋ธ”2]
    USING (์กฐ๊ฑด์ ˆ);

 

5. ON ์กฐ๊ฑด์ ˆ 

- ์นผ๋Ÿผ๋ช…์ด ๋‹ค๋ฅด๋”๋ผ๋„ ์กฐ์ธ ์กฐ๊ฑด์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

- ON ์กฐ๊ฑด์ ˆ์€ WHERE ์ ˆ์˜ ์กฐ์ธ ์กฐ๊ฑด๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ํ•˜๋ฉด์„œ๋„, ๋ช…์‹œ์ ์„ ์กฐ์ธ์˜ ์กฐ๊ฑด์„ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

6. CROSS JOIN 

- CROSS JOIN์€ E.F.CODD ๋ฐ•์‚ฌ๊ฐ€ ์–ธ๊ธ‰ํ•œ ์ผ๋ฐ˜ ์ง‘ํ•ฉ ์—ฐ์‚ฐ์ž์˜ PRODUCT์˜ ๊ฐœ๋…์œผ๋กœ ํ…Œ์ด๋ธ” ๊ฐ„ ์กฐ์ธ ์กฐ๊ฑด์ด ์—†๋Š” ๊ฒฝ์šฐ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ์˜ ์กฐํ•ฉ์„ ๋งํ•œ๋‹ค.

- ๋‘ ๊ฐœ์˜ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•œ CARTESIAN PRODUCT ๋˜๋Š” CROSS PRODUCT์™€ ๊ฐ™์€ ํ‘œํ˜„์œผ๋กœ, ๊ฒฐ๊ณผ๋Š” ์–‘์ชฝ ์ง‘ํ•ฉ์˜ M*N ๊ฑด์˜ ๋ฐ์ดํ„ฐ ์กฐํ•ฉ์ด ๋ฐœ์ƒํ•œ๋‹ค(์•„๋ž˜ 56๊ฑด์˜ ๋ฐ์ดํ„ฐ๋Š” EMP 14๊ฑด * DEPT 4๊ฑด์˜ ๋ฐ์ดํ„ฐ ์กฐํ•ฉ ๊ฑด์ˆ˜์ด๋‹ค).

SELECT A.ENAME, B.DNAME
	FROM EMP A CROSS JOIN DEPT B
ORDER BY A.ENAME;

-- ์‹คํ–‰ ๊ฒฐ๊ณผ ์˜ˆ)

ENAME		DNAME
------------------
ADAMS		ACCOUNTING
ADAMS		RESEARCH
ADAMS		SALES
. . .
. .
.

56 ๊ฐœ์˜ ํ–‰์ด ์„ ํƒ๋์Šต๋‹ˆ๋‹ค.

 

7. OUTER JOIN 

- INNER JOIN๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ FROM ์ ˆ์—์„œ ์ •์˜ํ•˜๊ฒ ๋‹ค๋Š” ํ‘œ์‹œ์ด๋ฏ€๋กœ USING ์กฐ๊ฑด์ ˆ์ด๋‚˜ ON ์กฐ๊ฑด์ ˆ์„ ํ•„์ˆ˜์ ์œผ๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

Outer Join


โœจ์žฅ ์š”์•ฝ

์œ„๋กœ๊ฐ€๊ธฐ

์ œ1์ ˆ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐœ์š”
ํŠน์ • ๋ฐ์ดํ„ฐ๋“ค์˜ ์ง‘ํ•ฉ์—์„œ ํ•„์š”๋กœ ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊บผ๋‚ด์„œ ์กฐํšŒํ•˜๊ณ  ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ,์ˆ˜์ •,์‚ญ์ œํ•˜๋Š” ํ–‰์œ„๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉ์ž๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ๋Œ€ํ™”ํ•˜๊ฒŒ ๋œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  SQL(Structured Query Language)์€ ์ด๋Ÿฌํ•œ ๋Œ€ํ™”๋ฅผ ๊ฐ€๋Šฅํ•˜๋„๋ก ๋งค๊ฐœ ์—ญํ• ์„ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 
SQL์€ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋ฐ์ดํ„ฐ ์ •์˜, ๋ฐ์ดํ„ฐ ์กฐ์ž‘, ๋ฐ์ดํ„ฐ ์ œ์–ด๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ์–ธ์–ด์ด๋‹ค.
์ œ2์ ˆ SELECT ๋ฌธ
SELECT ๋ฌธ์žฅ์„ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฐ์ดํ„ฐ ์กฐํšŒ ์‹œ ์‚ฐ์ˆ ์—ฐ์‚ฐ์ž์™€ ํ•ฉ์„ฑ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ ๊ฐ„์˜ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ์‚ฐ์ˆ ์—ฐ์‚ฐ์ž๋Š” NUMER์™€ DATE ์ž๋ฃŒํ˜•์— ๋Œ€ํ•ด ์ ์šฉ๋˜๋ฉฐ (), *, /, +, - ์˜ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฐ€์ง„๋‹ค.
ํ•ฉ์„ฑ์—ฐ์‚ฐ์ž๋Š” ๋ฌธ์ž์™€ ๋ฌธ์ž๋ฅผ ์—ฐ๊ฒฐํ•˜๋ฉฐ, CONCAT(string1, string2) ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
์ œ3์ ˆ ํ•จ์ˆ˜
์‚ฌ์šฉ์ž๋Š” ๋ฒค๋”์—์„œ ์ œ๊ณตํ•˜๋Š” ๋‚ด์žฅ ํ•จ์ˆ˜(FUNCTION)๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ๊ฐ’์„ ๊ฐ„ํŽธํ•˜๊ฒŒ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹จ์ผํ–‰ ํ•จ์ˆ˜๋Š” ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ์ดํ„ฐ์˜ ํ˜•์‹์— ๋”ฐ๋ผ์„œ ๋ฌธ์žํ˜•, ์ˆซ์žํ˜•, ๋‚ ์งœํ˜•, ๋ณ€ํ™˜ํ˜•, NULL ๊ด€๋ จ ํ•จ์ˆ˜๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค.
์ œ4์ ˆ WHERE ์ ˆ
WHERE ์กฐ๊ฑด์ ˆ์— ์ œํ•œ์„ ๋‘์–ด ์›ํ•˜๋Š” ์ž๋ฃŒ๋งŒ์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค. WHERE ์ ˆ์— ์‚ฌ์šฉ๋˜๋Š” ์—ฐ์‚ฐ์ž๋Š” ๋น„๊ต ์—ฐ์‚ฐ์ž, SQL ์—ฐ์‚ฐ์ž, ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž, ๋ถ€์ • ์—ฐ์‚ฐ์ž๊ฐ€ ์žˆ๋‹ค.
์ œ5์ ˆ GROUP BY, HAVING ์ ˆ
์ง‘๊ณ„ํ•จ์ˆ˜(Aggregate Function)๋Š” ์—ฌ๋Ÿฌ ํ–‰๋“ค์ด ๋ชจ์—ฌ์„œ ๊ทธ๋ฃน๋‹น ๋‹จ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋ฅผ ๋Œ๋ ค์ฃผ๋Š” ๋‹ค์ค‘ํ–‰ ํ•จ์ˆ˜์˜ ์ผ๋ถ€๋กœ์„œ COUNT, SUM, AVG, MAX, MIN ๋“ฑ์ด ์žˆ๋‹ค.
GROUP BY ์ ˆ์€ ์ง‘ํ•ฉ๋ณ„ ํ†ต๊ณ„ ์ •๋ณด์˜ ๊ธฐ์ค€์„ ๋ช…์‹œํ•˜๊ณ , HAVING ์ ˆ์—๋Š” ์ง‘ํ•ฉ์— ๋Œ€ํ•œ ์ œํ•œ ์กฐ๊ฑด์„ ๋‘์–ด ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋‚ด์šฉ๋งŒ ์ถœ๋ ฅํ•œ๋‹ค.
์ œ6์ ˆ ORDER BY ์ ˆ
ORDER BY ์ ˆ์€ ์กฐํšŒ๋œ ์ž๋ฃŒ๋ฅผ ์›ํ•˜๋Š” ์นผ๋Ÿผ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๊ณ , SELECT ๋ฌธ์žฅ์˜ ์ œ์ผ ๋งˆ์ง€๋ง‰์— ์œ„์น˜ํ•œ๋‹ค.
SELECT ๋ฌธ์žฅ์€ FROM ์ ˆ, WHERE ์ ˆ, GROUP BY ์ ˆ, HAVING ์ ˆ, SELECT ์ ˆ, ORDER BY ์ ˆ ์ˆœ์„œ๋กœ ์‹คํ–‰๋œ๋‹ค.
์ œ7์ ˆ ์กฐ์ธ
๋‘ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”๋“ค์„ ์—ฐ๊ฒฐํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์„ ์กฐ์ธ(JOIN)์ด๋ผ๊ณ  ํ•˜๋ฉฐ, ์ „ํ†ต์ ์ธ ๋ฐฉ์‹์˜ ์กฐ์ธ ๋ฌธ๋ฒ•์—์„œ๋Š” WHERE ์ ˆ์— ์กฐ์ธ ์กฐ๊ฑด๊ณผ ๋ฐ์ดํ„ฐ ์ œํ•œ ์กฐ๊ฑด์„ ํ•จ๊ป˜ ๊ธฐ์ˆ ํ•œ๋‹ค.
์ œ8์ ˆ ํ‘œ์ค€ ์กฐ์ธ
ANSI/ISO SQL์—์„œ ๊ทœ์ •ํ•œ ์กฐ์ธ์€ INNER JOIN, NATURAL JOIN, CROSS JOIN, OUTER JOIN์˜ ์กฐ์ธ ์œ ํ˜•์œผ๋กœ ๋ถ„๋ฅ˜ํ•  ์ˆ˜ ์žˆ๋‹ค.
ANSI/ISO ํ‘œ์ค€ ๋ฐฉ์‹์˜ ์กฐ์ธ ๋ฌธ๋ฒ•์—์„œ ๊ฐ€์žฅ ๋‘๋“œ๋Ÿฌ์ง„ ํŠน์ง•์€ ON์กฐ๊ฑด ์ ˆ ๋ฐ USING์กฐ๊ฑด ์ ˆ์„ ์‚ฌ์šฉํ•ด ์กฐ์ธ ์กฐ๊ฑด์„ WHERE ์ ˆ์˜ ๋ฐ์ดํ„ฐ ์ œํ•œ ์กฐ๊ฑด์œผ๋กœ๋ถ€ํ„ฐ ๋ถ„๋ฆฌํ•ด ๊ธฐ์ˆ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

 

300x250

์ฝ”๋“œ