Skip to content
/ SQL Public

๐Ÿ“š SQL ๋ช…๋ น์–ด์˜ ๋ชจ๋“ ๊ฒƒ

Notifications You must be signed in to change notification settings

gkqkehs7/SQL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

8 Commits
ย 
ย 

Repository files navigation

sqld ๊ธฐ๋ณธ ๋ฐ ํ™œ์šฉ

SQL ๋ฌธ์žฅ์˜ ์ข…๋ฅ˜

FROM - WHERE - GROUP BY - HAVING - SELECT - ORDER BY

  • (๋น„์ ˆ์ฐจ์ ) ๋ฐ์ดํ„ฐ ์กฐ์ž‘์–ด (DML) โ†’ SELECT, INSERT, UPDATE, DELETE
  • ๋ฐ์ดํ„ฐ ์ •์˜์–ด (DDL) โ†’ CREATE, ALTER, DROP, RENAME
  • ๋ฐ์ดํ„ฐ ์ œ์–ด์–ด (DCL) โ†’ GRANT, REVOKE
  • ํŠธ๋žœ์žญ์…˜ ์ œ์–ด์–ด (TCL) โ†’ COMMIT, ROLLBACK

ํ…Œ์ด๋ธ”๋ช… ์„ค์ •

  • ์˜๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•ด์•ผํ•จ
  • ์ค‘๋ณต๋˜๋ฉด ์•ˆ๋จ
  • ์˜ˆ์•ฝ์„œ ์‚ฌ์šฉ๋ถˆ๊ฐ€
  • A-Z, a-z, 0-9, _ ,#, $ ์˜ ๋ฌธ์ž๋งŒ ํ—ˆ์šฉํ•œ๋‹ค.
  • ๋์€ ํ•ญ์ƒ ;๋กœ ๋๋‚จ

abc_123 (0) / _abc (x) / 123abc (x)


CREATE

CREATE TABLE member(
		id VARCHAR2(20),
    password VARCHAR2(20),
    name VARCHAR2(20)
)

NOT NULL

CREATE TABLE member(
		id VARCHAR2(20) NOT NULL,
    password VARCHAR2(20) NOT NULL,
    name VARCHAR2(20)
)

DEFAULT

CREATE TABLE member(
		id VARCHAR2(20) NOT NULL,
    password VARCHAR2(20) DEFAULT '1234',
    name VARCHAR2(20) DEFAULT 'NICKNAME' NOT NULL
)

DEFAULT ์˜ต์…˜์€ ์ƒˆ๋กœ์šด ๋ ˆ์ฝ”๋“œ๊ฐ€ ํ…Œ์ด๋ธ”์— ์‚ฝ์ž…๋  ๋•Œ ํ•ด๋‹น ์ปฌ๋Ÿผ์— ๊ฐ’์„ ์ œ๊ณตํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ์‚ฌ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ๊ฐ’์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.


UNIQUE

CREATE TABLE member(
		id VARCHAR2(20) UNIQUE,
    password VARCHAR2(20) NOT NULL,
    name VARCHAR2(20)
)

CREATE TABLE member(
		id VARCHAR2(20),
    password VARCHAR2(20) NOT NULL,
    name VARCHAR2(20),
		UNIQUE(id)
)

UNIQUE ์˜ต์…˜์€ ํ…Œ์ด๋ธ”์˜ ์ปฌ๋Ÿผ์— ์ค‘๋ณต๋œ ๊ฐ’์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋„๋ก ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ํ•ด๋‹น ์ปฌ๋Ÿผ์˜ ๋ชจ๋“  ๊ฐ’์€ ์œ ์ผํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.


CHECK

CREATE TABLE member(
		id VARCHAR2(20) UNIQUE,
    password VARCHAR2(20) NOT NULL,
    name VARCHAR2(20)
		age NUMBER CHECK(AGE โ‰ฅ 18)  
		gender VARCHAR2(10) CHECK(gender IN ('๋‚จ', '์—ฌ'))
)

CHECK ์˜ต์…˜์€ ํŠน์ • ์กฐ๊ฑด์ด ๋งŒ์กฑ๋˜์–ด์•ผ๋งŒ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ฝ์ž…๋˜๊ฑฐ๋‚˜ ์—…๋ฐ์ดํŠธ๋  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์ง€์ •ํ•œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ๋Š” ํ…Œ์ด๋ธ”์— ์ถ”๊ฐ€๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.


PRIMARY

// ๋ฐ์ดํ„ฐ ํƒ€์ž…๊ณผ ๊ฐ™์ด ์„ค์ •ํ•˜๊ธฐ
CREATE TABLE member(
		id VARCHAR2(20) PRIMARY KEY,
    password VARCHAR2(20),
    name VARCHAR2(20)
)

// ๋ฐ์ดํ„ฐ ํƒ€์ž…๊ณผ ๋”ฐ๋กœ ์„ค์ •ํ•˜๊ธฐ
CREATE TABLE member(
		id VARCHAR2(20),
    password VARCHAR2(20),
    name VARCHAR2(20)
		PRIMARY KEY(id)
)

PRIMARY KEY ์„ค์ •

CREATE TABLE member(
		id VARCHAR2(20) CONSTRAINT pk_id PRIMARY KEY,
    password VARCHAR2(20),
    name VARCHAR2(20)
)

PRIMARY KEY๋ฅผ ์ง€์ •ํ•˜๋ฉฐ ์ œ์•ฝ ์กฐ๊ฑด์— ์ด๋ฆ„ ๋‹ฌ๊ธฐ

์ œ์•ฝ ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ• ๋•Œ๋Š” CONSTRAINT ๋กœ ์ด๋ฆ„์„ ์ง€์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์œผ๋กœ ์ข‹์€ ๊ด€ํ–‰์ด๋‹ค.


CREATE TABLE orders (
	order_id INT,
	product_id INT,
	quantity INT,
	PRIMARY KEY (order_id, product_id)
);

์—ฌ๋Ÿฌ ์ปฌ๋Ÿผ์„ ์กฐํ•ฉํ•˜์—ฌ ๋ณตํ•ฉ ๊ธฐ๋ณธ ํ‚ค(Composite Primary Key) ์„ค์ •ํ•˜๊ธฐ


FOREIGN KEY

CREATE TABLE member(
		id VARCHAR2(20) PRIMARY KEY,
    password VARCHAR2(20),
    name VARCHAR2(20)
)

// ๋ฐ์ดํ„ฐ ํƒ€์ž…๊ณผ ๊ฐ™์ด ์„ค์ •ํ•˜๊ธฐ
CREATE TABLE member_score(
	  id VARCHAR2(20) REFERENCES member(id),
		score NUMBER NOT NULL,
)

// ๋ฐ์ดํ„ฐ ํƒ€์ž…๊ณผ ๋”ฐ๋กœ ์„ค์ •ํ•˜๊ธฐ
CREATE TABLE member_score(
	  id VARCHAR2(20) REFERENCES member(id),
		score NUMBER NOT NULL,
		FOREGIN KEY (id) REFERENCES member(id)
)

member_score์—์„œ member์— ์—†๋Š” id๋ฅผ ์‚ฝ์ž…ํ•˜๋ ค ํ•œ๋‹ค๋ฉด DEPENDENT ์˜ค๋ฅ˜ ๋ฐœ์ƒํ•œ๋‹ค.

์™ธ๋ž˜ ํ‚ค ๊ฐ’์€ ์ฐธ์กฐ ๋ฌด๊ฒฐ์„ฑ ์ œ์•ฝ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.


์™ธ๋ž˜ํ‚ค ์‚ญ์ œ ์˜ต์…˜

CREATE TABLE member(
		id VARCHAR2(20) PRIMARY KEY,
    password VARCHAR2(20),
    name VARCHAR2(20)
)

CREATE TABLE member_score(
	  id VARCHAR2(20) PRIMARY KEY,
		score NUMBER NOT NULL,
		// ๋ถ€๋ชจ ๊ฐ’ ์‚ญ์ œ์‹œ ์ฐธ์กฐํ•˜๋Š” ์ž์‹์ด ์žˆ๋‹ค๋ฉด ์‚ญ์ œ ๋ถˆ๊ฐ€ (๊ธฐ๋ณธ ์˜ต์…˜)
		FOREIGN KEY(id) REFERENCES member(id) ON DELETE RESTRICT
		// ๋ถ€๋ชจ ํ‚ค๋ฅผ ์‚ญ์ œ ์‹œ ์ž์‹ ํ‚ค๋ฅผ ๊ฐ€์ง„ ํ–‰๋„ ํ•จ๊ป˜ ์‚ญ์ œ
		FOREIGN KEY(id) REFERENCES member(id) ON DELETE CASCADE
		// ๋ถ€๋ชจ ํ‚ค๋ฅผ ์‚ญ์ œ ์‹œ ์ž์‹ ํ‚ค๋ฅผ NULL๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค (๋‹ค๋ฅธ ํ–‰์€ ๊ทธ๋Œ€๋กœ).
		FOREIGN KEY(id) REFERENCES member(id) ON DELETE SET NULL
)

์นผ๋Ÿผ์— ์ฃผ์„ ๋‹ฌ๊ธฐ

CREATE TABLE member(
		id VARCHAR2(20),
    password VARCHAR2(20),
    name VARCHAR2(20)
)

COMMENT ON COLUMN member.id IS '๋ฉค๋ฒ„ ์•„์ด๋””'
COMMENT ON COLUMN member.password IS '๋ฉค๋ฒ„ ๋น„๋ฐ€๋ฒˆํ˜ธ'
COMMENT ON COLUMN member.name IS '๋ฉค๋ฒ„ ๋‹‰๋„ค์ž„'

Indexing

CREATE TABLE tableName (
	columnName dataType(SIZE) NOT NULL,
	columnName dataType(SIZE) DEFAULT,
)

CREATE INDEX indexName ON tableName (columnName)

ALTER

// ์นผ๋Ÿผ ์ด๋ฆ„ ๋ณ€๊ฒฝ
ALTER TABLE tableName RENAME COLUMN oldColumnName TO newColumnName;

// ์นผ๋Ÿผ ์ถ”๊ฐ€
ALTER TABLE tableName ADD columnName dataType(SIZE) 

// ์‚ฌ์ด์ฆˆ ๋ณ€๊ฒฝ, ๋ฐ์ดํ„ฐ ํƒ€์ž… ๋ณ€๊ฒฝ
ALTER TABLE tableName MODIFY columnName dataType(SIZE)

// NOT NULL ์˜ต์…˜ ์ถ”๊ฐ€
ALTER TABLE tableName MODIFY columnName VARCHAR(100) NOT NULL;

// ์ปฌ๋Ÿผ ์‚ญ์ œ
ALTER TABLE tableName DROP COLUMN columnName

ALTER ๋ช…๋ น์–ด๋Š” ์—ฌ๋Ÿฌ๊ฐœ์˜ column์„ ๋™์‹œ์— ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋‹ค. ๋‹จ, ํ•˜๋‚˜์— ํ•œ๊ฐœ์˜ ์ž‘์—…๋งŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.


์ œ์•ฝ์กฐ๊ฑด ์ถ”๊ฐ€ํ•˜๊ธฐ

// unique ์˜ต์…˜ ์ถ”๊ฐ€
ALTER TABLE tableName ADD CONSTRAINT ์ œ์•ฝ์กฐ๊ฑด์ด๋ฆ„ UNIQUE (columnName);

// PKํ‚ค ์„ค์ • ์ถ”๊ฐ€
ALTER TABLE tableName ADD PRIMARY KEY (columnName);
ALTER TABLE tableName ADD CONSTRAINT ์ œ์•ฝ์กฐ๊ฑด์ด๋ฆ„ PRIMARY KEY (columnName);

// ์™ธ๋ž˜ํ‚ค ์„ค์ • ์ถ”๊ฐ€
ALTER TABLE child_tableName ADD CONSTRAINT ์ œ์•ฝ์กฐ๊ฑด์ด๋ฆ„
FOREIGN KEY (child_columnName) REFERENCES parent(parent_columnName);

// ์™ธ๋ž˜ํ‚ค ์‚ญ์ œ
ALTER TABLE ํ…Œ์ด๋ธ”์ด๋ฆ„ DROP PRIMARY KEY

// check ์˜ต์…˜ ์ถ”๊ฐ€
ALTER TABLE products ADD CONSTRAINT ์ œ์•ฝ์กฐ๊ฑด์ด๋ฆ„ CHECK (price > 0);

์œ„์—์„œ๋„ ๋งํ–ˆ๋“ฏ์ด ์ œ์•ฝ ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ• ๋•Œ๋Š” CONSTRAINT ๋กœ ์ด๋ฆ„์„ ์ง€์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์œผ๋กœ ์ข‹์€ ๊ด€ํ–‰์ด๋‹ค.


DROP / TRUNCATE / DELETE

// ํ…Œ์ด๋ธ” ์‚ญ์ œ
DROP TABLE tableName 

// ์นผ๋Ÿผ๊ฐ’๋งŒ ๋‚จ๊ธฐ๊ณ  ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์‚ญ์ œ
TRUNCATE TABLE tableName 

// ๋ฐ์ดํ„ฐ ๋ชจ๋‘ ์‚ญ์ œ
DELETE FROM tableName
DROP TRUNCATE DELETE
DDL DDL DML
RollBack ๋ถˆ๊ฐ€๋Šฅ RollBack ๋ถˆ๊ฐ€๋Šฅ Commit ์ด์ „ Rollback ๊ฐ€๋Šฅ
Auto Commit Auto Commit ์‚ฌ์šฉ์ž Commit
ํ…Œ์ด๋ธ”์ด ์‚ฌ์šฉํ–ˆ๋˜ ๋ชจ๋“  Storage ์‚ญ์ œ ํ…Œ์ด๋ธ”์ด ์‚ฌ์šฉํ–ˆ๋˜ Storage ์ค‘ ์ตœ์ดˆ ํ…Œ์ด๋ธ” ์ƒ์„ฑ์‹œ ํ• ๋‹น๋œ Storage๋งŒ ๋‚จ๊ธฐ๊ณ  ์‚ญ์ œ ์‚ฌ์šฉํ–ˆ๋˜ Storage๋Š” ์‚ญ์ œ๋˜์ง€ ์•Š์Œ
ํ…Œ์ด๋ธ”์˜ ์ •์˜ ์ž์ฒด๋ฅผ ์™„์ „ํžˆ ์‚ญ์ œ ํ…Œ์ด๋ธ”์„ ์ตœ์ดˆ ์ƒ์„ฑ๋œ ์ดˆ๊ธฐ์ƒํƒœ๋กœ ๋งŒ๋“ฌ ๋ฐ์ดํ„ฐ๋งŒ ์‚ญ์ œ

RENAME

RENAME origin_table_name TO change_table_name

ํ…Œ์ด๋ธ” ์ด๋ฆ„ ๋ณ€๊ฒฝํ•˜๊ธฐ


INSERT

// ์‚ฝ์ž… ์ปฌ๋Ÿผ์„ ๋ช…์‹œํ•˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ ๋ชจ๋“  ์ปฌ๋Ÿผ์„ ์‚ฝ์ž…ํ•ด์•ผํ•œ๋‹ค.
INSERT INTO tableName VALUES (data, data, data...); 

// ํŠน์ • ๋ฐ์ดํ„ฐ๋งŒ ์‚ฝ์ž…ํ•˜๋Š” ๊ฒฝ์šฐ ์‚ฝ์ž… ์ปฌ๋Ÿผ์„ ๋ช…์‹œํ•ด์•ผํ•œ๋‹ค.
INSERT INTO tableName (columnName, columnName) VALUES (data, data); 

UPDATE

// ํŠน์ • column ๊ฐ’ ๋ณ€๊ฒฝ
UPDATE tableName SET columnName = data WHERE ์กฐ๊ฑด 

// ๋‘๊ฐœ ํ•œ๋ฒˆ์— ๋ณ€๊ฒฝ
UPDATE tableName SET columnName1 = data1, columnName2 = data2 WHERE ์กฐ๊ฑด 

WHERE ์กฐ๊ฑด์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋ชจ๋“  ํ–‰์ด ์—…๋ฐ์ดํŠธ ๋Œ€์ƒ์ด ๋œ๋‹ค.


DELETE

 // table์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์‚ญ์ œ
DELETE FROM tableName

// ์กฐ๊ฑด์„ ๋‹ฌ์•„์„œ ์‚ญ์ œํ•˜๊ธฐ
DELETE FROM tableName WHERE ์กฐ๊ฑด

// ์กฐ๊ฑด ์—ฌ๋Ÿฌ๊ฐœ ๋‹ฌ์•„์„œ ์‚ญ์ œํ•˜๊ธฐ
DELETE FROM tableName WHERE ์กฐ๊ฑด AND ์กฐ๊ฑด 
DELETE FROM neflix WHERE video_name IN ('์‹œ๊ทธ๋„', '๋‚˜์˜ ์•„์ €์”จ')

// X ์ž˜๋ชป๋œ ๋ฌธ๋ฒ•
DELETE * FROM tableName

TRUNCATE
DDL, ROLLBACK ๋ถˆ๊ฐ€๋Šฅ, AUTO COMMIT, ํ…Œ์ด๋ธ” ์ตœ์ดˆ ์ƒ์„ฑ ์‹œ ์šฉ๋Ÿ‰๋งŒ ๋‚จ๊ธฐ๊ณ  ๋ชจ๋‘๋ฐ˜ํ™˜. ํ…Œ์ด๋ธ”์„ ์ตœ์ดˆ ์ƒ์„ฑ๋œ ์ดˆ๊ธฐ์ƒํƒœ๋กœ ๋งŒ๋“ฌ

DELETE
DML, ROLLBACK ๊ฐ€๋Šฅ(๋กœ๊ทธ ๋‚จ์Œ), ์‚ฌ์šฉ์ž COMMIT, ์šฉ๋Ÿ‰๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์Œ,๋ฐ์ดํ„ฐ๋งŒ ์‚ญ์ œ


SELECT

// ํ…Œ์ด๋ธ”์˜ ์ „์ฒด ๋ฐ์ดํ„ฐ ์กฐํšŒ
SELECT * FROM tableName 

// ํŠน์ • ์ปฌ๋Ÿผ๋งŒ ๊ฐ€์ ธ์˜ค๊ธฐ
SELECT columnName1, columnName2 FROM tableName 

// ํŠน์ • ์ปฌ๋Ÿผ๋งŒ ๊ฐ€์ ธ์˜ค๊ธฐ
SELECT * FROM tableName WHERE ์กฐ๊ฑด์ ˆ 

// ์ค‘๋ณต ๋ฐ์ดํ„ฐ ํ•˜๋‚˜๋กœ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
SELECT DISTINCT columnName FROM tableName WHERE ์กฐ๊ฑด์ ˆ 

// ASํ‚ค์›Œ๋“œ๋กœ ๋ณ„์นญ ์„ค์นญํ•˜๊ธฐ
SELECT columnName1 AS c1, columnName2 AS c2 FROM tableName 

FROM - WHERE - GROUP BY - HAVING - SELECT - ORDER BY


alias ์‚ฌ์šฉ

SELECT tableA.name, tableA.age, tableB.job
FROM tableA, tableB

SELECT A.name, A.age, B.job
FROM tableA A, tableB B

์ด๋ ‡๊ฒŒ ๋‹ค์ค‘ ํ…Œ์ด๋ธ”์—์„œ SELECTํ•  ๊ฒฝ์šฐ ํ…Œ์ด๋ธ” ์ด๋ฆ„์„ ๊ณ„์† ์“ฐ๋Š”๊ฑด ๋ถˆํŽธํ•˜๋ฏ€๋กœ alias๋กœ ์ค„์—ฌ์„œ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค.


WHERE

// column
SELECT * FROM tableName WHERE columnName = "columnName"

// category๊ฐ€ movie๋‚˜ animtion์ธ ๊ฒƒ๋“ค ์กฐํšŒ
SELECT * FROM neflix WHERE category IN ("movie", "animation") 

// category๊ฐ€ movie๋‚˜ animtion์ด ์•„๋‹Œ๊ฒƒ๋“ค ์กฐํšŒ
SELECT * FROM neflix WHERE category NOT IN ("movie", "animation") 

// AND๋‚˜ OR์œผ๋กœ ์—ฌ๋Ÿฌ๊ฐœ ์กฐ๊ฑด ๋น„๊ต
SELECT * FROM neflix WHERE view < 70;
SELECT * FROM neflix WHERE view < 70 AND view > 50;
SELECT * FROM neflix WHERE category = "animation" AND view < 70 
SELECT * FROM neflix WHERE category = "animation" OR view < 70 

// BEWEEN์œผ๋กœ ์‚ฌ์ด๊ฐ’ ๋น„๊ต
SELECT * FROM neflix WHERE view BETWEEN 50 AND 70

// LIKE๋ฌธ์œผ๋กœ ๋ฌธ์ž์—ด ๋น„๊ต
SELECT * FROM neflix WHERE name LIKE "๋ฏธ%" // "๋ฏธ"๋กœ ์‹œ์ž‘ํ•˜๋Š” ์˜ํ™” ๊ฐ€์ ธ์˜ค๊ธฐ
SELECT * FROM neflix WHERE name LIKE "%๋ฏธ" // "๋ฏธ"๋กœ ๋๋‚˜๋Š” ์˜ํ™” ๊ฐ€์ ธ์˜ค๊ธฐ
SELECT * FROM neflix WHERE name LIKE "%๋ฏธ%" // "๋ฏธ"๋ฅผ ํฌํ•จํ•˜๋Š” ์˜ํ™” ๊ฐ€์ ธ์˜ค๊ธฐ

// NULL์„ WHERE์— ์‚ฌ์šฉํ• ๋•Œ๋Š” IS๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
SELECT * FROM tableName WHERE columnName IS NULL
SELECT * FROM tableName WHERE columnName IS NOT NULL

JOIN

Customers table

customer_id customer_name
1 a
2 b
3 c
4 d

Orders table

custome_id order_id
1 101
2 102
1 103
5 104

LEFT-OUTER-JOIN

SELECT A.customer_id, A.customer_name, B.order_id 

FROM Customers_table A 

LEFT OUTER JOIN Orders_table B 

ON A.customer_id = B.customer_id
customer_id customer_name order_id
1 a 101
1 b 103
2 b 102
3 c NULL
4 d NULL

**LEFT OUTER JOIN**์€ ์™ผ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ์™€ ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์˜ ์ผ์น˜ํ•˜๋Š” ๋ ˆ์ฝ”๋“œ๋ฅผ ๊ฐ€์ ธ์˜ค๋ฉฐ, ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ ˆ์ฝ”๋“œ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋Š” NULL ๊ฐ’์„ ๊ฐ€์ง„ ๋ ˆ์ฝ”๋“œ๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.

์™ผ์ชฝ์— ์žˆ๋Š” table์— ์ •๋ณด๋Š” ๋ชจ๋‘ ๊ฐ€์ ธ์™€์ง€๋ฉฐ, ์˜ค๋ฅธ์ชฝ์— ์žˆ๋Š” table์€ ON์กฐ๊ฑด์— ์ผ์น˜ํ•˜๋Š” ๊ฒƒ๋งŒ ๊ฐ€์ ธ์™€์ง„๋‹ค.


RIGHT-OUTER-JOIN

SELECT A.customer_id, A.customer_name, B.order_id 

FROM Customers_table A 

RIGHT OUTER JOIN Orders_table B 

ON A.customer_id = B.customer_id
order_id custome_id customer_name
101 1 a
102 2 b
103 1 a
104 5 NULL

**RIGHT OUTER JOIN**์€ ์™ผ์ชฝ ํ…Œ์ด๋ธ”์˜ ์ผ์น˜ํ•˜๋Š” ๋ ˆ์ฝ”๋“œ์™€ ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ๋ฅผ ๊ฐ€์ ธ์˜ค๋ฉฐ, ์™ผ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ ˆ์ฝ”๋“œ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋Š” NULL ๊ฐ’์„ ๊ฐ€์ง„ ๋ ˆ์ฝ”๋“œ๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.


FULL-OUTER-JOIN

SELECT A.customer_id, A.customer_name, B.order_id 

FROM Customers_table A 

FULL OUTER JOIN Orders_table B 

ON A.customer_id = B.customer_id
customer_id customer_name order_id
1 a 101
1 b 103
2 b 102
3 c NULL
5 NULL 104

**FULL OUTER JOIN**์€ ์–‘์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ๋ฅผ ๊ฐ€์ ธ์˜ค๋ฉฐ, ์–‘์ชฝ ํ…Œ์ด๋ธ” ์ค‘ ์–ด๋Š ํ•œ์ชฝ์ด๋ผ๋„ ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋Š” NULL ๊ฐ’์„ ๊ฐ€์ง„ ๋ ˆ์ฝ”๋“œ๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.


INNER JOIN ( Equal Join )

SELECT A.name, A.age, B.name

FROM tableA A, tableB B

WHERE A.name = B.name
customer_id customer_name order_id
1 a 101
1 b 103
2 b 102

INNER JOIN ( Non Equal Join )

SELECT A.name, A.age, B.name

FROM tableA A, tableB B

WHERE A.name Like B.name

์ผ๋ฐ˜์ ์œผ๋กœ Join์€ PK์™€ FK๊ฐ’์˜ ์—ฐ๊ด€์„ฑ์— ์˜ํ•ด ์„ฑ๋ฆฝ๋œ๋‹ค.

Equal Join์€ Join์— ๊ด€์—ฌํ•˜๋Š” ํ…Œ์ด๋ธ” ๊ฐ„์˜ ์ปฌ๋Ÿผ ๊ฐ’๋“ค์ด ์ •ํ™•ํ•˜๊ฒŒ ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉ๋œ๋‹ค.

Equal Join์€ โ€˜=โ€™ ์—ฐ์‚ฐ์ž์— ์˜ํ•ด ์ˆ˜ํ–‰๋˜๋ฉฐ, ๊ทธ ์ด์™ธ์˜ ๋น„๊ต ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๋ชจ๋‘ Non Equal Join์ด๋‹ค. ๋Œ€๋ถ€๋ถ„ Non Equal Join์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์„ค๊ณ„์ƒ์˜ ์ด์œ ๋กœ ์ˆ˜ํ–‰์ด ๋ถˆ๊ฐ€๋Šฅ ํ•œ ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค.


SELF JOIN

// ์ง์†์ƒ๊ด€์„ ์ถœ๋ ฅํ•˜๋Š” ์ฟผ๋ฆฌ
SELECT A.id, A.name AS '์ง์›๋ช…', B.name AS '์ง์†์ƒ๊ด€'
FROM employee A, employee B
WHERE A.supervisor_id = B.id

CROSS JOIN

SELECT *
FROM Customers
CROSS JOIN Products;

์œ„์˜ SQL ๋ฌธ์žฅ์€ "Customers" ํ…Œ์ด๋ธ”๊ณผ "Products" ํ…Œ์ด๋ธ”์„ CROSS JOINํ•˜์—ฌ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ณ ๊ฐ๊ณผ ์ œํ’ˆ ์กฐํ•ฉ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค

ํ•œ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ–‰๊ณผ ๋‹ค๋ฅธ ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ–‰์„ ์กฐ์ธ์‹œํ‚ค๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. ์ƒํ˜ธ ์กฐ์ธ ๊ฒฐ๊ณผ์˜ ์ „์ฒด ํ–‰ ๊ฐœ์ˆ˜๋Š” ๋‘ ํ…Œ์ด๋ธ”์˜ ๊ฐ ํ–‰์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ณฑํ•œ ์ˆ˜๋งŒํผ ๋ฉ๋‹ˆ๋‹ค. ์นดํ‹ฐ์…˜ ๊ณฑ(CARTESIAN PRODUCT)๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค.


ORDER BY

// ์กฐํšŒ์ˆ˜ ๊ธฐ์ค€ ์˜ค๋ฆ„์ฐจ์ˆœ (ASC๊ฐ€ default)
SELETE * FROM neflix ORDER BY view ASC 

// ์กฐํšŒ์ˆ˜ ๊ธฐ์ค€ ๋‚ด๋ฆผ์ฐจ์ˆœ
SELETE * FROM neflix ORDER BY view DESC 

// category๋ณ„๋กœ ์ •๋ ฌํ•œ ๋‹ค์Œ, ์กฐํšŒ์ˆ˜ ๊ธฐ์ค€ ์ •๋ ฌ
SELETE * FROM neflix ORDER BY category, view DESC 

// category๋Š” ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•œ ๋‹ค์Œ, ์กฐํšŒ์ˆ˜๋Š” ์˜ค๋ฆ„์ฐจ์ˆœ ๊ธฐ์ค€ ์ •๋ ฌ
SELETE * FROM neflix ORDER BY category DESC, view ASC 

// GROUP BY๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ GROUP BY ํ‘œํ˜„์‹์ด ์•„๋‹Œ ๊ฐ’์€ ORDER BY์— ๊ธฐ์ˆ ๋  ์ˆ˜ ์—†๋‹ค.
SELECT ์ง€์—ญ, SUM(๋งค์ถœ๊ธˆ์•ก) AS ๋งค์ถœ๊ธˆ์•ก
FROM ์ง€์—ญ๋ณ„ ๋งค์ถœ
GROUP BY ์ง€์—ญ
ORDER BY ๋…„ (x)

// GROUP BY๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ๋Š” ์ƒ๊ด€์—†๋‹ค.
SELECT ์ง€์—ญ, ๋งค์ถœ๊ธˆ์•ก
FROM ์ง€์—ญ๋ณ„ ๋งค์ถœ
ORDER BY ๋…„ (o)

// ์ฒซ๋ฒˆ์งธ ์นผ๋Ÿผ ๊ธฐ์ค€ ์ •๋ ฌ ํ›„, ๋‘๋ฒˆ์งธ ์นผ๋Ÿผ ๊ธฐ์ค€ ์ •๋ ฌ
SELECT ์ง€์—ญ, ๋งค์ถœ๊ธˆ์•ก
FROM ์ง€์—ญ๋ณ„ ๋งค์ถœ
ORDER BY 1, 2

GROUP BY

// category ๋ณ„๋กœ ์ˆซ์ž์„ธ๊ธฐ
// null์€ count ์•ˆ๋œ๋‹ค
SELECT category, COUNT(*) FROM neflix GROUP BY category

// category ๋ณ„๋กœ ์กฐํšŒ์ˆ˜ ํ•ฉ๊ณ„
SELECT category, SUM(view) FROM neflix GROUP BY category

// category ๋ณ„๋กœ ์กฐํšŒ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋งŽ์€ ๊ฐ’
SELECT category, MAX(view) FROM neflix GROUP BY category

// MAX๊ฐ’์ด ๊ฐ€์žฅ ํฐ category๋ถ€ํ„ฐ ์ •๋ ฌ
SELECT category, MAX(view) FROM neflix GROUP BY category ORDER BY MAX(view) DESC

// category ๋ณ„๋กœ ์กฐํšŒ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’
SELECT category, MIN(view) FROM neflix GROUP BY category

// category ๋ณ„๋กœ ์กฐํšŒ์ˆ˜ ์ตœ๋Œ€ ์ตœ์†Œ๊ฐ’ ํ•จ๊ป˜ ์ถœ๋ ฅ
SELECT category, MAX(view), MIN(view) FROM neflix GROUP BY category

// category ๋ณ„๋กœ ์กฐํšŒ ์ˆ˜ ํ‰๊ท 
SELECT category, AVG(view) FROM neflix GROUP BY category

HAVING

SELECT customer_id, COUNT(order_id) AS order_count
FROM orders
GROUP BY customer_id
HAVING order_count >= 10;

**Having**์ ˆ์€ **WHERE**์ ˆ๊ณผ ๋น„์Šทํ•˜๊ฒŒ ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ทธ๋ฃน๋งŒ์„ ์„ ํƒํ•˜์ง€๋งŒ,

**GROUP BY**๋กœ ๊ทธ๋ฃนํ™”๋œ ๊ฒฐ๊ณผ ์ค‘์—์„œ ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ทธ๋ฃน๋งŒ์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค.


CASE WHEN

SELECT column1, column2, column3
CASE WHEN column1 = 1 THEN 'ONE'  // 1์ธ ๊ฐ’๋“ค์— ๋Œ€ํ•ด์„  'ONE'
		 WHEN column2 = 2 THEN 'TWO' ELSE 'THREE' // 2์ธ ๊ฐ’๋“ค์— ๋Œ€ํ•ด์„  'TWO' ๋‚˜๋จธ์ง€๋Š” 'THREE'

**CASE WHEN**์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ธฐ์กด์— ์žˆ๋Š” ์—ด์˜ ์กฐ๊ฑด์— ๋”ฐ๋ฅธ ์ƒˆ๋กœ์šด ๊ฐ’์„ ๊ฐ–๋Š” ์ƒˆ๋กœ์šด ์—ด์ด ๋งŒ๋“ค์–ด์ง€๊ฒŒ ๋œ๋‹ค.

SELECT column1, column2, column3
CASE WHEN column1 = 1 THEN 'ONE'  // 1์ธ ๊ฐ’๋“ค์— ๋Œ€ํ•ด์„  'ONE' ๋‚˜๋จธ์ง€๋Š” NULL

**ELSE**๋ฅผ ๋นผ๊ณ  ์“ฐ๋ฉด ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๊ฐ’๋“ค์— ๋Œ€ํ•ด์„  NULL์ด ๋œ๋‹ค.

SELECT column1, column2, column3
CASE column1 WHEN 1 THEN 'ONE' 
			WHEN 2 THEN 'TWO' ELSE 'THREE' AS new_col

AS๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ–‰์˜ ์ด๋ฆ„์„ ์ง€์ •ํ•ด ์ค„ ์ˆ˜ ์žˆ๋‹ค.


๋‚ด์žฅ ํ•จ์ˆ˜

๋‚ ์งœ ํ•จ์ˆ˜

// ํ˜„์žฌ ๋‚ ์งœ ๋ฐ ์‹œ๊ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
SELECT SYSDATE FROM DUAL;

// ๋…„, ์›”๋งŒ ์ถœ๋ ฅํ•˜๊ณ  ์‹œ๊ฐ„์„ ๋ฒ„๋ฆฌ๊ธฐ
SELECT TRUNC(SYSDATE) FROM DUAL;

// ์˜ค๋Š˜ ๋‚ ์งœ๋กœ ๋ถ€ํ„ฐ 3๋‹ฌ ํ›„ ๋‚ ์งœ ์ถœ๋ ฅ
SELECT ADD_MONTHS(SYSDATE, 3) FROM DUAL;

// ์˜ค๋Š˜ ๋‚ ์งœ๋กœ ๋ถ€ํ„ฐ 3๋‹ฌ ์ „ ๋‚ ์งœ ์ถœ๋ ฅ
SELECT ADD_MONTHS(SYSDATE, -3) FROM DUAL;

// ์‹œ๊ฐ„ ๋ฌธ์ž์—ด ํฌ๋งคํŒ…
SELECT TO_CHAR(SYSDATE, 'YYYYMMDD HH24MISS') FROM DUAL;
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') FROM DUAL;
SELECT TO_CHAR(SYSDATE, 'YYYY/MM/DD HH24:MI:SS') FROM DUAL;

REPLACE

// ์ฝ”๋“œ๋ผ์ด์–ธ์—์„œ ์ฝ”๋“œ๋ฅผ CODE๋กœ ๋ณ€๊ฒฝ
SELECT REPLACE('์ฝ”๋“œ๋ผ์ด์–ธ', '์ฝ”๋“œ', 'CODE') FROM DUAL

// ์„ธ๋ฒˆ์งธ ์ธ์ž์— ์•„๋ฌด๊ฒƒ๋„ ์ ์–ด์ฃผ์ง€ ์•Š์œผ๋ฉด ์ฝ”๋“œ๋ผ์ด์–ธ์—์„œ ์ฝ”๋“œ๋ฅผ ์—†์• ๊ธฐ
SELECT REPLACE('์ฝ”๋“œ๋ผ์ด์–ธ', '์ฝ”๋“œ') FROM DUAL

// ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ์—์„œ - ์‚ญ์ œ
SELECT REPLACE('010-1234-1234', '-') FROM DUAL

// ์‹ค์ œ ํ…Œ์ด๋ธ” ์‚ฌ์šฉ ์˜ˆ 
SELECT REPLACE(columnName, "A", "B") FROM tableName

SUBSTR

// 3๋ฒˆ์งธ ๋ถ€ํ„ฐ 2๊ธ€์ž ์ž˜๋ผ์„œ ๊ฐ€์ ธ์˜ค๊ธฐ
SELECT SUBSTR("์ฝ”๋“œ๋ผ์ด์–ธ", 3, 2) FROM DUAL // ๋ผ์ด

// 3๋ฒˆ ์ธ์ž์— ์•„๋ฌด๊ฒƒ๋„ ์•ˆ์“ฐ๋ฉด ๋๊นŒ์ง€ ๊ฐ€์ ธ์˜ด
SELECT SUBSTR("์ฝ”๋“œ๋ผ์ด์–ธ", 3, 2) FROM DUAL // ๋ผ์ด์–ธ

// ์Œ์ˆ˜๋ฉด ๋’ค์—์„œ๋ถ€ํ„ฐ ์ž๋ฆฌ์ˆ˜ (4๋‹ˆ๊นŒ ๋’ค์—์„œ๋ถ€ํ„ฐ 4๋ฒˆ์งธ ๊ธ€์ž)
SELECT SUBSTR("์ฝ”๋“œ๋ผ์ด์–ธ", -4, 2) FROM DUAL // ๋“œ๋ผ์ด์–ธ

// ์ด๋ฆ„ ๋ณ€๊ฒฝ ๊น€๋ฏผ์šฐ -> ๊น€*์šฐ  || ๋Š” ์ด์–ด ๋ถ™ํžˆ๊ธฐ
SELECT SUBSTR(name, 1, 1) || "*" || SUBSTR(name, 3) FROM tableName

// ์ด ํŽธ์ง€๋Š”...
SELECT SUBSTR("์ด ํŽธ์ง€๋Š” ์˜๊ตญ์—์„œ๋ถ€ํ„ฐ ์‹œ์ž‘๋˜์–ด ํ•œ ์•„์ด์œผ ๊ฐ€์ •~~~", 1, 5) || "...." FROM DUAL

UPPER / LOWER

// ๋Œ€๋ฌธ์ž ๋ณ€๊ฒฝ
SELECT UPPER("abcd") FROM DUAL // ABCD

// ์†Œ๋ฌธ์ž ๋ณ€๊ฒฝ
SELECT LOWER("ABCD") FROM DUAL // ABCD

SELECT * FROM member WHERE ID = UPPER("collection");

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

// ๋ฐ˜์˜ฌ๋ฆผ
SELECT ROUND(3.16) FROM DUAL

// ๋ฐ˜์˜ฌ๋ฆผํ•ด์„œ ์†Œ์ˆ˜์  ์•„๋ž˜ ์ฒซ์žฌ์ž๋ฆฌ ๊นŒ์ง€ ๋ฐ˜ํ™˜
SELECT ROUND(3.16, 1) FROM DUAL

// ๋’ค์— ์†Œ์ˆ˜์  ๋ฒ„๋ฆฌ๊ธฐ
SELECT TRUNC(3.16) FROM DUAL

// ์˜ฌ๋ฆผ
SELECT CEIL(3.16) FROM DUAL

TOP

SELECT TOP 3 columnName
FROM tableName
ORDER BY columnName DESC

columnName์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ์ƒ์œ„ 3๊ฐœ ์ถœ๋ ฅ

SELECT TOP 3 WITH TIES columnName
FROM tableName
ORDER BY columnName DESC

๋งŒ์•ฝ ๊ฐ™์€ ๊ฐ’์ด ์žˆ๋‹ค๋ฉด ๊ฐ™์ด ์ถœ๋ ฅ

SELECT TOP 50 PERCENT columnName
FROM tableName
ORDER BY columnName DESC

์ƒ์œ„ 50ํผ์„ผํŠธ ์ถœ๋ ฅ


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

// ๋ชจ๋“  ํ–‰์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋ฉฐ NULL ๊ฐ’์„ ๊ฐ€์ง„ ํ–‰๋„ ํฌํ•จ
SELECT COUNT(*) AS count FROM tableName

// ํ‘œํ˜„์‹์˜ ๊ฐ’์ด NULL์ธ ๊ฒƒ์„ ์ œ์™ธํ•œ ํ–‰์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅ
SELECT COUNT(columnName) AS c1 FROM tableName;

// ํ‘œํ˜„์‹์˜ NULL์„ ์ œ์™ธํ•œ ํ•ฉ๊ณ„ ์ถœ๋ ฅ
SELECT SUM(columnName) AS c1 FROM tableName;

// ํ‘œํ˜„์‹์˜ NULL์„ ์ œ์™ธํ•œ ํ‰๊ท  ์ถœ๋ ฅ
SELECT AVG(columnName) AS c1 FROM tableName;

// ์ตœ๋Œ€๊ฐ’ ์ถœ๋ ฅ
SELECT MAX(columnName) AS c1 FROM tableName;

// ์ตœ์†Œ๊ฐ’ ์ถœ๋ ฅ
SELECT MIN(columnName) AS c1 FROM tableName;

// ํ‘œ์ค€ ํŽธ์ฐจ ์ถœ๋ ฅ
SELECT STDDEV(columnName) AS c1 FROM tableName;

// ๋ถ„์‚ฐ ์ถœ๋ ฅ
SELECT VARIAN(columnName) AS c1 FROM tableName;

๋‹จ์ผํ–‰ NULL ๊ด€๋ จ ํ•จ์ˆ˜

// ISNULL(A, B) โ†’ A column์˜ ๊ฐ’์ด NULL์ด๋ผ๋ฉด B๋กœ ๋Œ€์ฒด (SQL)
SELECT column1, column2, ISNULL(column3, '๊ฐ’ ์—†์Œ') FROM tableName

// NVL(A, B) โ†’ A column์˜ ๊ฐ’์ด NULL์ด๋ผ๋ฉด B๋กœ ๋Œ€์ฒด (ORACLE)
SELECT column1, column2, NVL(column3, '๊ฐ’ ์—†์Œ') FROM tableName

// NULLIF(A, B) โ†’ A column์˜ ๊ฐ’์ด B์™€ ๊ฐ™์œผ๋ฉด NULL์„, ๊ฐ™์ง€ ์•Š์œผ๋ฉด A column์˜ ๊ฐ’์„ return
SELECT column1, column2, NULLIF(column3, 1) FROM tableName

// NULL์ด ์•„๋‹Œ ์ตœ์ดˆ์˜ ํ‘œํ˜„์‹ ๋‚˜ํƒ€๋ƒ„, ๋ชจ๋“ ๊ฒŒ NULL์ด๋ฉด NULL return
SELECT column1, column2, COALESCE(column3, column4, column5) IS FROM tableName
SELECT A, B, C COALESCE(A, B, C) FROM tableName;
A B C COALESCE(A, B)
1 2 NULL 1
NULL NULL 3 3
NULL 1 NULL 1
NULL NULL NULL NULL

Transaction

Transaction์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋…ผ๋ฆฌ์  ์—ฐ์‚ฐ๋‹จ์œ„๋กœ์„œ ๋ฐ€์ ‘ํžˆ ๊ด€๋ จ๋˜์–ด ๋ถ„๋ฆฌ๋  ์ˆ˜ ์—†๋Š” ํ•œ ๊ฐœ ์ด์ƒ์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์กฐ์ž‘์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

  • **์›์ž์„ฑ** - ํŠธ๋ž™์žญ์…˜์—์„œ ์ •์˜๋œ ์—ฐ์‚ฐ๋“ค์€ ๋ชจ๋‘ ์„ฑ๊ณต์ ์œผ๋กœ ์‹คํ–‰๋˜๋˜์ง€ ์ „ํ˜€ ์‹คํ–‰๋˜์ง€ ์•Š์€ ์ƒํƒœ๋กœ ๋‚จ์•„์žˆ์–ด์•ผ ํ•œ๋‹ค.
  • **์ผ๊ด€์„ฑ** - ํŠธ๋žœ์žญ์…˜์ด ์‹คํ–‰๋˜๊ธฐ ์ „์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋‚ด์šฉ์ด ์ž˜๋ชป๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค๋ฉด ํŠธ๋ž™์žญ์…˜์ด ์‹คํ–‰๋œ ์ดใ…œใ…Ž์—๋„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋‚ด์šฉ์— ์ž˜๋ชป์ด ์žˆ์œผ๋ฉด ์•ˆ๋œ๋‹ค.
  • **๊ณ ๋ฆฝ์„ฑ** - ํŠธ๋žœ์žญ์…˜์ด ์‹คํ–‰๋˜๋Š” ๋„์ค‘์— ๋‹ค๋ฅธ ํŠธ๋žœ์žญ์…˜์˜ ์˜ํ–ฅ์„ ๋ฐ›์•„ ์ž˜๋ชป๋œ ๊ฒฐ๊ณผ๋ฅผ ๋งŒ๋“ค์–ด์„œ๋Š” ์•ˆ๋œ๋‹ค
  • **์ง€์†์„ฑ** - ํŠธ๋žœ์žญ์…˜์ด ์„ฑ๊ณต์ ์œผ๋กœ ์ˆ˜ํ–‰๋˜๋ฉด ๊ทธ ํŠธ๋žœ์žญ์…˜์ด ๊ฐฑ์‹ ํ•œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋‚ด์šฉ์€ ์˜๊ตฌ์ ์œผ๋กœ ์ €์žฅ๋œ๋‹ค.
  • **Dirty Read** - ์ˆ˜์ •๋˜์—ˆ์ง€๋งŒ commit์ด ๋˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๋Š” ๊ฒƒ
  • **Non-Repeatbale Read** - ์กฐํšŒ / ์ˆ˜์ • / ์กฐํšŒ ์ˆœ์œผ๋กœ ์ผ์–ด๋‚˜๋ฉด ๋‘ ์กฐํšŒ์˜ ๊ฒฐ๊ณผ๋ฌผ์ด ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค. ( ์กฐํšŒ๋œ ๋ ˆ์ฝ”๋“œ์˜ ๊ฐ’์ด ๋ณ€๊ฒฝ ๋จ )
  • **Phantom Read** - ์กฐํšŒ / ์‚ฝ์ž… or ์‚ญ์ œ / ์กฐํšŒ ์ˆœ์œผ๋กœ ์ผ์–ด๋‚˜์„œ ๋‘ ์กฐํšŒ์˜ ๊ฒฐ๊ณผ๋ฌผ์ด ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค. ( ์ƒˆ๋กœ์šด ๊ฐ’์ด ์ƒ๊ฒจ๋‚˜๊ฑฐ๋‚˜ ์—†์–ด์ง )
ORACLE SQL Server
DDL ๋ฌธ์žฅ ์ˆ˜ํ–‰ ํ›„ ์ž๋™ COMMIT DDL ๋ฌธ์žฅ ์ˆ˜ํ–‰ ํ›„ ์ž๋™ COMMIT ํ•˜์ง€ ์•Š์Œ
DDL ๋ฌธ์žฅ์˜ ์ˆ˜ํ–‰์€ ๋‚ด๋ถ€์ ์œผ๋กœ Transaction ์ข…๋ฃŒ CREATE๋„ Transaction์— ํฌํ•จ๋จ์œผ๋กœ Rollback์‹œ ์ทจ์†Œ
๋ฐ์ดํ„ฐ ๊ฐ’์„ ๊ณต๋ฐฑ์œผ๋กœ ์ž…๋ ฅํ•˜๋ฉด NULL ์ž…๋ ฅ๋จ ๋ฐ์ดํ„ฐ ๊ฐ’์„ ๊ณต๋ฐฑ์œผ๋กœ ์ž…๋ ฅํ•˜๋ฉด โ€œโ€ ์ž…๋ ฅ๋จ

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

WHERE์ ˆ์— ๋“ค์–ด๊ฐ€๋Š” ์ฟผ๋ฆฌ

๋‹จ์ผํ–‰ ์—ฐ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ

// Jones๋ณด๋‹ค ๋†’์€ ๊ธ‰์—ฌ๋ฅผ ๋ฐ›๋Š” ์‚ฌ์›๋“ค ์ถœ๋ ฅ
SELECT *
FROM employees
WHERE salary > (SELECT salary FROM employees WHERE name = 'JONES');

์ด๋•Œ JONES๋ผ๋Š” ์ด๋ฆ„์ด ์—ฌ๋Ÿฌ๊ฐœ ์กด์žฌํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ๊ด„ํ˜ธ์•ˆ์˜ ๊ฒฐ๊ณผ๋Š” ํ•˜๋‚˜์—ฌ์•ผ ํ•œ๋‹ค.


๋‹ค์ค‘ํ–‰ ์—ฐ๊ด€์„œ๋ธŒ์ฟผ๋ฆฌ

SELECT * FROM employees A
WHERE A.department_id IN
     (SELECT B.department_id FROM department B WHERE b.location_id = 1700)

์—ฌ๋Ÿฌ๊ฐœ์˜ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด IN์„ ์‚ฌ์šฉํ•ด์ฃผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.


WHERE EXIST

// ๋Ÿฐ๋‹๋งจ๊ณผ ๋ฌดํ•œ๋„์ „ ๋ชจ๋‘ ์ฐธ์—ฌํ•˜๋Š” ์ฐธ์—ฌ์ž 
SELECT * FROM ๋Ÿฐ๋‹๋งจ A 
WHERE EXISTS (SELECT 1 FROM ๋ฌดํ•œ๋„์ „ B WHERE A.NAME = B.NAME)

WHERE EXIST ๋‹ค์Œ์˜ SELECT ์ ˆ ์•ˆ์˜ ์ปฌ๋Ÿผ์€ SELECT 1 ์ฒ˜๋Ÿผ ์•„๋ฌด ์ฟผ๋ฆฌ๋‚˜ ์ ์–ด๋„ ๋œ๋‹ค. ์ค‘์š”ํ•œ ๊ฑด ์กฐ๊ฑด์— ๋งž์ถฐ ๋ฐ˜ํ™˜๊ฐ’์ด ์žˆ๋ƒ ์—†๋ƒ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. WHERE ์ ˆ์˜ ์กฐ๊ฑด๋งŒ ์œ ์‹ฌํžˆ ํ™•์ธํ•˜๋ฉด ๋œ๋‹ค.


WHERE NOT EXISTS

SELECT * FROM ๋Ÿฐ๋‹๋งจ A 
WHERE NOT EXISTS (SELECT 1 FROM ๋ฌดํ•œ๋„์ „ B WHERE A.NAME = B.NAME)

๋Ÿฐ๋‹๋งจ์—๋Š” ์ฐธ์—ฌํ•˜์ง€๋งŒ ๋ฌดํ•œ๋„์ „์—๋Š” ์ฐธ์—ฌํ•˜์ง€ ์•Š๋Š” ์ฐธ์—ฌ์ž ( ์ฐจ์ง‘ํ•ฉ )


FROM์ ˆ ์„œ๋ธŒ์ฟผ๋ฆฌ (Inline view)

SELECT E.EMPNO, E.ENAME, E.DEPTNO, D.DNAME, D.LOC
FROM (SELECT * FROM EMP WHERE DEPTNO = 10) E, DEPT D
WHERE E.DEPTNO = D.DEPTNO;

FROM์ ˆ์— ์ง์ ‘ ํ…Œ์ด๋ธ”์„ ๋ช…์‹œํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ธฐ์— ๋ฐ์ดํ„ฐ ๊ทœ๋ชจ๊ฐ€ ํฌ๊ฑฐ๋‚˜ ๋ถˆํ•„์š”ํ•œ ์—ด์ด ๋งŽ์„ ๋•Œ, ์ผ๋ถ€ ํ–‰๊ณผ ์—ด๋งŒ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.


SELECT์ ˆ ์„œ๋ธŒ์ฟผ๋ฆฌ (์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ)

// customers ํ…Œ์ด๋ธ”์˜ ๊ฐ ๊ณ ๊ฐ์— ๋Œ€ํ•ด ํ•ด๋‹น ๊ณ ๊ฐ์˜ ์ฃผ๋ฌธ ์ˆ˜
SELECT customers.customer_name,
    (
      SELECT COUNT(*)
      FROM orders
      WHERE orders.customer_id = customers.customer_id
    ) AS order_count
FROM customers;

select์ ˆ ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” join์œผ๋กœ๋„ ๋™์ผํ•œ ๊ฒฐ๊ณผ ๋„์ถœ ๊ฐ€๋Šฅํ•˜๋‹ค.


์ง‘ํ•ฉ์—ฐ์‚ฐ์ž

UNION

๋‘๊ฐœ์˜ ํ…Œ์ด๋ธ”์„ ํ•˜๋‚˜๋กœ ๋งŒ๋“œ๋Š” ์—ฐ์‚ฐ, ์ค‘๋ณต๋˜๋Š” ๋ฐ์ดํ„ฐ๋Š” ํ•˜๋‚˜๋กœ ์ฒ˜๋ฆฌ

UNION ALL

๋‘๊ฐœ์˜ ํ…Œ์ด๋ธ”์„ ํ•˜๋‚˜๋กœ ๋งŒ๋“œ๋Š” ์—ฐ์‚ฐ, ์ค‘๋ณต๋˜๋Š” ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ์ค‘๋ณต ์ œ๊ฑฐ๋ฅผ ํ•˜์ง€ ์•Š์Œ

INTERSECT

๋‘๊ฐœ์˜ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•ด ๊ฒน์น˜๋Š” ๋ถ€๋ถ„์„ ์ถ”์ถœํ•˜๋Š” ์—ฐ์‚ฐ. ์ค‘๋ณต๋˜๋Š” ๋ฐ์ดํ„ฐ๋Š” ํ•˜๋‚˜๋กœ ์ฒ˜๋ฆฌ

EXCEPT

์•ž์˜ ์ˆ˜ํ–‰ ๊ฒฐ๊ณผ์—์„œ ๋’ค์˜ ๊ฒฐ๊ณผ๋ฅผ ์ฐจ์ง‘ํ•ฉ. ์ค‘๋ณต๋˜๋Š” ๋ฐ์ดํ„ฐ๋Š” ํ•˜๋‚˜๋กœ ์ฒ˜๋ฆฌ


SELECT column1 A, column2 B
FROM table1
UNION ALL
SELECT column1 C, column2 D
FROM table2

๊ฒฐ๊ณผ์—์„œ column1์„ ํ‘œํ˜„ํ• ๋•Œ๋Š” ์ฒซ๋ฒˆ์งธ SELECT ๋ฌธ์žฅ์˜ alias ์ธ A๊ฐ€ ์ ์šฉ์ด ๋œ๋‹ค.


START WITH

  • START WITH๋Š” Oracle์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • SQL Server์—์„œ์˜ ๊ณ„์ธตํ˜• ์งˆ์˜๋ฌธ์€ CTE๋ฅผ ์žฌ๊ท€ํ˜ธ์ถœํ•จ์œผ๋กœ์จ ๊ณ„์ธต ๊ตฌ์กฐ๋ฅผ ์ „๊ฐœํ•œ๋‹ค.
  • SQL Server์—์„œ์˜ ๊ณ„์ธตํ˜• ์งˆ์˜๋ฌธ์€ ์•ต์ปค ๋ฉค๋ฒ„๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๊ธฐ๋ณธ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์„ ๋งŒ๋“ค๊ณ  ์ดํ›„ ์žฌ๊ท€ ๋ฉค๋ฒ„๋ฅผ ์ง€์†์ ์œผ๋กœ ์‹คํ–‰ํ•œ๋‹ค.
  • Oracle์˜ ๊ณ„์ธตํ˜• ์งˆ์˜๋ฌธ์—์„œ WHERE์ ˆ์€ ๋ชจ๋“  ์ „๊ฐœ๋ฅผ ์ง„ํ–‰ํ•œ ์ดํ›„ ํ•„ํ„ฐ ์กฐ๊ฑด์œผ๋กœ์„œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋ฐ์ดํ„ฐ๋งŒ์„ ์ถ”์ถœํ•˜๋Š”๋ฐ ํ™œ์šฉํ•œ๋‹ค.
  • PRIORํ‚ค์›Œ๋“œ๋Š” SELECT์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

C1 C2 C3
1 NULL A
2 1 B
3 1 C
4 2 D
SELECT * FROM TABLE
START WITH C2 IS NULL // 1ํ–‰์ด level 1์ด ๋œ๋‹ค.
CONNECT BY PRIOR C1 = C2 // c2๊ฐ€ 1์ธ ๊ฒƒ๋“ค์ด level2๊ฐ€ ๋œ๋‹ค.
ORDER SIBLINGS BY C3 DESC // ์ž์‹๋“ค๋ผ๋ฆฌ๋Š” c3๊ธฐ์ค€ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ์„ ํ•œ๋‹ค.
C1 C2 C3 level
1 NULL A 1
3 1 C 2
2 1 B 2
4 2 D 3

์ˆœ๋ฐฉํ–ฅ ์ „๊ฐœ & ์—ญ๋ฐฉํ–ฅ ์ „๊ฐœ

๋ถ€์„œ์ฝ”๋“œ ๋ถ€์„œ๋ช… ์ƒ์œ„ ๋ถ€์„œ์ฝ”๋“œ
100 ์•„์‹œ์•„๋ถ€ NULL
110 ํ•œ๊ตญ์ง€์‚ฌ 100
111 ์„œ์šธ์ง€์  110
120 ์ผ๋ณธ์ง€์‚ฌ 100
121 ๋„์ฟ„์ง€์  120
122 ์˜ค์‚ฌ์นด์ง€์  120

์ˆœ๋ฐฉํ–ฅ์ „๊ฐœ

SELECT * FROM TABLE
START WITH ๋ถ€์„œ์ฝ”๋“œ = 120
CONNECT BY PRIOR ๋ถ€์„œ์ฝ”๋“œ = ์ƒ์œ„ ๋ถ€์„œ์ฝ”๋“œ 

๋ถ€๋ชจ ๋…ธ๋“œ๋กœ๋ถ€ํ„ฐ ์ž์‹ ๋…ธ๋“œ ๋ฐฉํ–ฅ์œผ๋กœ ์ „๊ฐœํ•˜๋Š” ๊ฒƒ

๋ถ€์„œ์ฝ”๋“œ ๋ถ€์„œ๋ช… ์ƒ์œ„ ๋ถ€์„œ์ฝ”๋“œ level
120 ์ผ๋ณธ์ง€์‚ฌ 100 1
121 ๋„์ฟ„์ง€์  120 2
122 ์˜ค์‚ฌ์นด์ง€์  120 2

์—ญ๋ฐฉํ–ฅ์ „๊ฐœ

SELECT * FROM TABLE
START WITH ๋ถ€์„œ์ฝ”๋“œ = 120
CONNECT BY PRIOR ์ƒ์œ„ ๋ถ€์„œ์ฝ”๋“œ = ๋ถ€์„œ์ฝ”๋“œ 

์ž์‹ ๋…ธ๋“œ๋กœ๋ถ€ํ„ฐ ๋ถ€๋ชจ ๋…ธ๋“œ ๋ฐฉํ–ฅ์œผ๋กœ ์ „๊ฐœํ•˜๋Š” ๊ฒƒ

๋ถ€์„œ์ฝ”๋“œ ๋ถ€์„œ๋ช… ์ƒ์œ„ ๋ถ€์„œ์ฝ”๋“œ level
120 ์ผ๋ณธ์ง€์‚ฌ 100 1
100 ์•„์‹œ์•„๋ถ€ NULL 2

๊ณ„์ธตํ˜• ์ฟผ๋ฆฌ์—์„œ์˜ ์กฐ๊ฑด์ ˆ

SELECT * FROM TABLE
START WITH C2 IS NULL // 1ํ–‰์ด level 1์ด ๋œ๋‹ค.
CONNECT BY PRIOR C1 = C2 // c2๊ฐ€ 1์ธ ๊ฒƒ๋“ค์ด level2๊ฐ€ ๋œ๋‹ค.
AND ์กฐ๊ฑด~~~
ORDER SIBLINGS BY C3 DESC // ์ž์‹๋“ค๋ผ๋ฆฌ๋Š” c3๊ธฐ์ค€ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ์„ ํ•œ๋‹ค.

START WITH์— ํ•ด๋‹นํ•˜๋Š” ํ–‰๋“ค์€(์—ฌ๊ธฐ์„  C2) ์กฐ๊ฑด๊ณผ ์ƒ๊ด€์—†์ด select๊ฐ€ ๋œ๋‹ค!! ์ฃผ์˜


VIEW

: ํ•˜๋‚˜ ์ด์ƒ์˜ ๊ธฐ๋ณธ ํ…Œ์ด๋ธ”์ด๋‚˜ ๋‹ค๋ฅธ ๋ทฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒ์„ฑ๋˜๋Š” ๊ฐ€์ƒ ํ…Œ์ด๋ธ”

    • ๊ธฐ๋ณธ ํ…Œ์ด๋ธ”์€ ๋””์Šคํฌ์— ๊ณต๊ฐ„์ด ํ• ๋‹น๋˜์–ด ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•จ
    • ๋ทฐ๋Š” ๋ฐ์ดํ„ฐ ๋”•์…”๋„ˆ๋ฆฌ (Data Dictionary) ํ…Œ์ด๋ธ”์— ๋ทฐ์— ๋Œ€ํ•œ ์ •์˜ (SQL ๋ฌธ) ๋งŒ ์ €์žฅ๋˜์–ด ๋””์Šคํฌ ์ €์žฅ ๊ณต๊ฐ„ ํ• ๋‹น์ด ์ด๋ฃจ์–ด์ง€์ง€ ์•Š์Œ
    • ์ „์ฒด ๋ฐ์ดํ„ฐ ์ค‘์—์„œ ์ผ๋ถ€๋งŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ
    • ๋ทฐ์— ๋Œ€ํ•œ ์ˆ˜์ • ๊ฒฐ๊ณผ๋Š” ๋ทฐ๋ฅผ ์ •์˜ํ•œ ๊ธฐ๋ณธ ํ…Œ์ด๋ธ”์— ์ ์šฉ๋จ
    • ๋ทฐ๋ฅผ ์ •์˜ํ•œ ๊ธฐ๋ณธ ํ…Œ์ด๋ธ”์—์„œ ์ •์˜๋œ ๋ฌด๊ฒฐ์„ฑ ์ œ์•ฝ์กฐ๊ฑด์€ ๊ทธ๋Œ€๋กœ ์œ ์ง€๋จ

๋ทฐ์˜ ํ•„์š”์„ฑ

    • ์‚ฌ์šฉ์ž ๋งˆ๋‹ค ํŠน์ • ๊ฐ์ฒด๋งŒ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•  ํ•„์š”๊ฐ€ ์žˆ์Œ

(๋ชจ๋“  ์ง์›์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋ชจ๋“  ์‚ฌ์›์ด ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋ฉด ์•ˆ ๋จ)

    • ๋ณต์žกํ•œ ์งˆ์˜๋ฌธ์„ ๋‹จ์ˆœํ™” ํ•  ์ˆ˜ ์žˆ์Œ
    • ๋ฐ์ดํ„ฐ์˜ ์ค‘๋ณต์„ฑ์„ ์ตœ์†Œํ™”ํ•  ์ˆ˜ ์žˆ์Œ

์˜ˆ) ํŒ๋งค๋ถ€์— ์†ํ•œ ์‚ฌ์›๋“ค๋งŒ์„ ์‚ฌ์›ํ…Œ์ด๋ธ”์—์„œ ์ฐพ์•„์„œ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”๋กœ ๋งŒ๋“ค๋ฉด ์ค‘๋ณต์„ฑ์ด ๋ฐœ์ƒํ•จ

  • ์ด๋Ÿด ๋•Œ ๋ทฐ๊ฐ€ ํ•„์š”ํ•จ

create view ๋ทฐ์ด๋ฆ„ย asย sql๋ฌธย (select๋ฌธ)ย ;

์˜ˆ)ย ์‚ฌ์›ย ํ…Œ์ด๋ธ”์—ย ๋ถ€์„œ๋ฒˆํ˜ธย 30์ธย ์‚ฌ์›๋“ค์˜ย ๋ทฐย ์ƒ์„ฑํ•˜๊ธฐ

create view emp30 as select * from employee where dno=30;

ROLLUP

ROLL UPํ•จ์ˆ˜๋Š” ์†Œ๊ทธ๋ฃน๊ฐ„์˜ ํ•ฉ๊ณ„๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. ROLLUP์„ ์‚ฌ์šฉํ•˜๋ฉด GROUP BY๋กœ ๋ฌถ์€ ๊ฐ๊ฐ์˜ ์†Œ๊ทธ๋ฃน ํ•ฉ๊ณ„์™€ ์ „์ฒด ํ•ฉ๊ณ„๋ฅผ ๋ชจ๋‘ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.


SELECT ์ƒํ’ˆID, ์›”, SUM(๋งค์ถœ์•ก) AS ๋งค์ถœ์•ก
FROM ์›”๋ณ„๋งค์ถœ
GROUP BY ์ƒํ’ˆID, ์›”;
์ƒํ’ˆID ์›” ๋งค์ถœ์•ก
P1 1 1000
P1 2 2000
P2 1 1000
P2 1 3000
P3 2 4000

์ด๋ ‡๊ฒŒ GROUP BY ๋งŒ ์ž‘์„ฑํ•œ๋‹ค๋ฉด ์ƒํ’ˆID์˜ ์›”๋ณ„ ํ•ฉ๊ณ„๋งŒ ๋ณด์—ฌ์ฃผ์ง€๋งŒ


SELECT ์ƒํ’ˆID, ์›”, SUM(๋งค์ถœ์•ก) AS ๋งค์ถœ์•ก
FROM ์›”๋ณ„๋งค์ถœ
GROUP BY ROLLUP(์ƒํ’ˆID, ์›”);
์ƒํ’ˆID ์›” ๋งค์ถœ์•ก
P1 1 1000
P1 2 2000
p1 null 3000
P2 1 1000
P2 1 3000
P2 null 4000
P3 2 4000
P3 null 4000
null null 11000

ROLL UP ์œผ๋กœ ๋ฌถ์–ด์ค€๋‹ค๋ฉด ์ƒํ’ˆID์˜ ์›”๋ณ„ ํ•ฉ๊ณ„, ์ƒํ’ˆID๋ณ„ ํ•ฉ๊ณ„, ์ด ํ•ฉ๊ณ„ ์ด๋ ‡๊ฒŒ 3๊ฐœ๋ฅผ ๋ณด์—ฌ์ฃผ๊ฒŒ ๋œ๋‹ค.


CUBE

CUBEํ•จ์ˆ˜๋Š” ํ•ญ๋ชฉ๋“ค ๊ฐ„์˜ ๋‹ค์ฐจ์›์ ์ธ ์†Œ๊ณ„๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค. ROLL UP๊ณผ ๋‹ฌ๋ฆฌ GROUP BY ์ ˆ์— ๋ช…์‹œํ•œ ๋ชจ๋“  ์ปฌ๋Ÿผ์— ๋Œ€ํ•ด ์†Œ๊ทธ๋ฃน ํ•ฉ๊ณ„๋ฅผ ๊ณ„์‚ฐํ•ด์ค€๋‹ค.


SELECT ์ƒํ’ˆID, ์›”, SUM(๋งค์ถœ์•ก) AS ๋งค์ถœ์•ก
FROM ์›”๋ณ„๋งค์ถœ
GROUP BY CUBE(์ƒํ’ˆID, ์›”);
์ƒํ’ˆID ์›” ๋งค์ถœ์•ก
P1 1 1000
P1 2 2000
p1 null 3000
P2 1 1000
P2 1 3000
P2 null 4000
P3 2 4000
P3 null 4000
null 1 5000
null 2 6000
null null 11000

ROLL UP ์€ ์ƒํ’ˆID์— ๋Œ€ํ•ด ํ•ฉ๊ณ„๋งŒ ๋ณด์—ฌ์ฃผ์ง€๋งŒ, CUBE ๋Š” ์›”๋ณ„ ๋งค์ถœ์•ก๋„ ๋ณด์—ฌ์ฃผ๊ฒŒ๋œ๋‹ค.


GROUPING SETS

SELECT ์ƒํ’ˆID, ์›”, SUM(๋งค์ถœ์•ก) AS ๋งค์ถœ์•ก
FROM ์›”๋ณ„๋งค์ถœ
GROUP BY GROUPING SETS(์ƒํ’ˆID, ์›”);
์ƒํ’ˆID ์›” ๋งค์ถœ์•ก
p1 null 3000
P2 null 4000
P3 null 4000
null 1 5000
null 2 6000

GROUPING SETS๋Š” ํŠน์ • ํ•ญ๋ชฉ์— ๋Œ€ํ•œ ์†Œ๊ณ„๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

ROLL UP๊ณผ CUBE๋Š” GROUP BY ๊ฒฐ๊ณผ์— ์†Œ๊ทธ๋ฃน ํ•ฉ๊ณ„์™€ ํ† ํƒˆ ํ•ฉ๊ณ„๋ฅผ ๋ณด์—ฌ์ฃผ์ง€๋งŒ GROUPING SETS๋Š” ๊ฐ ์†Œ๊ทธ๋ฃน๋ณ„ ํ•ฉ๊ณ„๋งŒ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ณด์—ฌ์ค€๋‹ค.


SELECT ์ƒํ’ˆID, ์›”, SUM(๋งค์ถœ์•ก) AS ๋งค์ถœ์•ก
FROM ์›”๋ณ„๋งค์ถœ
GROUP BY GROUPING SETS((์ƒํ’ˆID), (์›”), (์ƒํ’ˆID, ์›”), ());
// == GROUP BY CUBE(์ƒํ’ˆID, ์›”);

GROUPING SETS ์•ˆ์— ํŠน์ • ์ปฌ๋Ÿผ๋“ค์„ ์ž‘์„ฑํ•˜๋ฉด ํ•ด๋‹น ์นผ๋Ÿผ๋“ค์— ๋Œ€ํ•ด ํ•ฉ๊ณ„๋“ค์„ ์ˆœ์„œ๋กœ ๋ณด์—ฌ์ค€๋‹ค.

GROUPING

SELECT
    CASE GROUPING(์ƒํ’ˆID) WHEN 1 THEN '๋ชจ๋“  ์ƒํ’ˆID' ELSE ์ƒํ’ˆID END AS ์ƒํ’ˆID,
    CASE GROUPING(์›”) WHEN 1 THEN '๋ชจ๋“  ์›”' ELSE ์›” END AS ์›”,
    SUM(๋งค์ถœ์•ก) AS ๋งค์ถœ์•ก
FROM ์›”๋ณ„๋งค์ถœ
GROUP BY ROLLUP(์ƒํ’ˆID, ์›”);
์ƒํ’ˆID ์›” ๋งค์ถœ์•ก GROUPING(์ƒํ’ˆID) GROUPING(์›”)
P1 1 1000 0 0
P1 2 2000 0 0
P1 ๋ชจ๋“  ์›” 3000 0 1
P2 1 1000 0 0
P2 1 3000 0 0
P2 ๋ชจ๋“  ์›” 4000 0 1
P3 2 4000 0 0
P3 ๋ชจ๋“  ์›” 4000 0 1
๋ชจ๋“  ์ƒํ’ˆ ID ๋ชจ๋“  ์›” 11000 1 1

GROUPING์€ ์ง์ ‘์ ์œผ๋กœ ๊ทธ๋ฃน๋ณ„ ์ง‘๊ณ„๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜๋Š” ์•„๋‹ˆ์ง€๋งŒ ์œ„์˜ ์ง‘๊ณ„ํ•จ์ˆ˜๋“ค์„ ์ง€์›ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

์ง‘๊ณ„๊ฐ€ ๊ณ„์‚ฐ๋œ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด์„œ๋Š” 1์˜ ๊ฐ’์„ ๊ฐ–๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด์„œ๋Š” 0์˜ ๊ฐ’์„ ๊ฐ–๋Š”๋‹ค.

CASE WHEN๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ๋งจ ์ฒ˜์Œ์— ๋‹จ์ˆœ ROLL UPํ•จ์ˆ˜๋งŒ ์ผ์„ ๋•Œ NULL๊ฐ’์œผ๋กœ ํ‘œ์‹œ๋˜์—ˆ๋˜ ๊ณณ์— ๊ฐ’์„ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค. ์ง‘๊ณ„๊ฐ€ ๊ณ„์‚ฐ๋œ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด์„œ๋งŒ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— GROUPING(์ปฌ๋Ÿผ๋ช…)=1์ธ ๊ฒฝ์šฐ์—๋งŒ '๋ชจ๋“ ์ƒํ’ˆID' ๋˜๋Š” '๋ชจ๋“ ์›”' ๊ฐ’์„ ๋ถ€์—ฌํ–ˆ๊ณ  0์ธ ๊ฒฝ์šฐ์—๋Š” ์›๋ž˜๋Œ€๋กœ ์ƒํ’ˆID์™€ ์›”์„ ์จ์ฃผ์—ˆ๋‹ค.


์œˆ๋„์šฐ ํ•จ์ˆ˜

GROUP BY ๋Š” ์ง‘์•ฝ ๊ธฐ๋Šฅ์œผ๋กœ ์ธํ•ด ํ–‰ ์ˆ˜๊ฐ€ ์ค„์–ด๋“  ๋ฐ˜๋ฉด, ์œˆ๋„์šฐ ํ•จ์ˆ˜๋Š” ํ–‰ ์ˆ˜๊ฐ€ ๊ทธ๋Œ€๋กœ ๋‚จ์•„์žˆ๋‹ค.

์œˆ๋„์šฐ ํ•จ์ˆ˜์—๋Š” ์ง‘์•ฝ์˜ ๊ธฐ๋Šฅ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์ด๋Ÿฌํ•œ ํŠน์ง•์„ ์ด์šฉํ•ด์„œ ์šฐ๋ฆฌ๋Š” ํ–‰๊ณผ ํ–‰๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ํŽธํ•˜๊ฒŒ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

PARTITION BY๋ฅผ ๋ถ™ํžˆ๋ฉด ํ•ด๋‹น ๋ฐ์ดํ„ฐ ๋ณ„๋กœ ์ˆœ์œ„๊ฐ€ ์ง€์ •๋œ๋‹ค. ๋ถ™ํžˆ์ง€ ์•Š์œผ๋ฉด ์ „์ฒด ํ–‰์— ๋Œ€ํ•ด ์ ์šฉ๋œ๋‹ค.


์ˆœ์œ„ํ•จ์ˆ˜

RANK

SELECT JOB, ENAME, SAL, 
       RANK() OVER (ORDER BY SAL DESC) JOB_RANK 
FROM EMP;

JOB       ENAME             SAL   JOB_RANK
--------- ---------- ---------- ----------
ANALYST   FORD             3000          1
ANALYST   SCOTT            3000          1
CLERK     MILLER           1300          3
CLERK     ADAMS            1300          3
CLERK     JAMES             950          5
CLERK     SMITH             800          

SELECT JOB, ENAME, SAL, 
       RANK() OVER (PARTITION BY JOB ORDER BY SAL DESC) JOB_RANK 
FROM EMP;

JOB       ENAME             SAL   JOB_RANK
--------- ---------- ---------- ----------
ANALYST   FORD             3000          1
ANALYST   SCOTT            3000          1
CLERK     MILLER           1300          1
CLERK     ADAMS            1300          1
CLERK     JAMES             950          3
CLERK     SMITH             800          4

ORDER BY๋ฅผ ํฌํ•จํ•œ ์ฟผ๋ฆฌ๋ฌธ์—์„œ ํŠน์ • ์ปฌ๋Ÿผ์˜ ์ˆœ์œ„๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. PARTITION ๋‚ด์—์„œ ์ˆœ์œ„๋ฅผ ๊ตฌํ•  ์ˆ˜๋„ ์žˆ๊ณ  ์ „์ฒด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ˆœ์œ„๋ฅผ ๊ตฌํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๋™์ผํ•œ ๊ฐ’์— ๋Œ€ํ•ด์„œ๋Š” ๊ฐ™์€ ์ˆœ์œ„๋ฅผ ๋ถ€์—ฌํ•˜๋ฉฐ ์ค‘๊ฐ„ ์ˆœ์œ„๋ฅผ ๋น„์šด๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ๋™์ผํ•œ ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ ์ˆœ์œ„๋Š” 1,1,3,4,4,6 ์ด๋Ÿฐ์‹์ธ ์…ˆ


DENSE_RANK

SELECT JOB, ENAME, SAL, 
       DENSE_RANK() OVER (PARTITION BY JOB ORDER BY SAL DESC) JOB_RANK 
FROM EMP;

JOB       ENAME             SAL   JOB_RANK
--------- ---------- ---------- ----------
ANALYST   FORD             3000          1
ANALYST   SCOTT            3000          1
CLERK     MILLER           1300          1
CLERK     ADAMS            1300          1
CLERK     JAMES             950          2
CLERK     SMITH             800          3

RANK์™€ ์ž‘๋™๋ฒ•์€ ๋™์ผํ•˜์ง€๋งŒ, ๋™์ผํ•œ ๊ฐ’์— ๋Œ€ํ•ด์„œ๋Š” ๊ฐ™์€ ์ˆœ์œ„๋ฅผ ๋ถ€์—ฌํ•˜๊ณ  ์ค‘๊ฐ„ ์ˆœ์œ„๋ฅผ ๋น„์šฐ์ง€ ์•Š๋Š”๋‹ค.

์˜ˆ๋ฅผ๋“ค์–ด ๋™์ผํ•œ ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ ์ˆœ์œ„๋Š” 1,1,2,3,3,4 ์ด๋Ÿฐ ์‹


ROW_NUMBER

SELECT JOB, ENAME, SAL, 
       ROW_NUMBER() OVER (PARTITION BY JOB ORDER BY SAL DESC) JOB_RANK 
FROM EMP;

JOB       ENAME             SAL   JOB_RANK
--------- ---------- ---------- ----------
ANALYST   FORD             3000          1
ANALYST   SCOTT            3000          2
CLERK     MILLER           1300          1
CLERK     ADAMS            1300          2
CLERK     JAMES             950          3
CLERK     SMITH             800          4

RANK, DENSE_RANK๋Š” ๋™์ผํ•œ ๊ฐ’์— ๋Œ€ํ•ด ๋™์ผ ์ˆœ์œ„๋ฅผ ๋ถ€์—ฌํ•˜์ง€๋งŒ ROW_NUMBER์€ ๋™์ผํ•œ ๊ฐ’์ด์–ด๋„ ๊ณ ์œ ํ•œ ์ˆœ์œ„๋ฅผ ๋ถ€์—ฌํ•œ๋‹ค.


๊ทธ๋ฃน ๋‚ด ํ–‰ ์ˆœ์„œ ํ•จ์ˆ˜

FIRST_VALUE

SELECT  DEPTNO, ENAME, SAL,
        FIRST_VALUE(ENAME) OVER (PARTITION BY DEPTNO ORDER BY SAL DESC
        ROWS UNBOUNDED PRECEDING) DEPT_RICH
FROM    EMP ;

ํŒŒํ‹ฐ์…˜๋ณ„ ์œˆ๋„์šฐ์—์„œ ๊ฐ€์žฅ ๋จผ์ € ๋‚˜์˜จ ๊ฐ’์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.


LAST_VALUE

SELECT  DEPTNO, ENAME, SAL,
        LAST_VALUE(ENAME) OVER (PARTITION BY DEPTNO ORDER BY SAL DESC
        ROW BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) DEPT_POOR
FROM    EMP ;

ํŒŒํ‹ฐ์…˜๋ณ„ ์œˆ๋„์šฐ์—์„œ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ๋‚˜์˜จ ๊ฐ’์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.


LAG

SELECT ENAME, HIREDATE, SAL
     , LAG(SAL) OVER (ORDER BY HIREDATE) as PREV_SAL 
FROM EMP 
WHERE JOB = 'SALESMAN';

ENAME      HIREDATE         SAL   PREV_SAL
---------- --------- ---------- ----------
ALLEN      20-FEB-81       1600
WARD       22-FEB-81       1250       1600
TURNER     08-SEP-81       1500       1250
MARTIN     28-SEP-81       1250       1500

--HIREDATE๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ๋ณธ์ธ๋ณด๋‹ค ์ž…์‚ฌ์ผ์ž๊ฐ€ ํ•˜๋‚˜ ๋” ์•ž์„  ์‚ฌ์›์˜ ๊ธ‰์—ฌ๋ฅผ ์ถœ๋ ฅ

SELECT ENAME, HIREDATE, SAL
     , LAG(SAL, 2, 0) OVER (ORDER BY HIREDATE) as PREV_SAL 
FROM EMP 
WHERE JOB = 'SALESMAN';

ENAME      HIREDATE         SAL   PREV_SAL
---------- --------- ---------- ----------
ALLEN      20-FEB-81       1600          0
WARD       22-FEB-81       1250          0
TURNER     08-SEP-81       1500       1600
MARTIN     28-SEP-81       1250       1250

--HIREDATE๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ๋ณธ์ธ๋ณด๋‹ค ์ž…์‚ฌ์ผ์ž๊ฐ€ ๋‘ ๊ฐœ ๋” ์•ž์„  ์‚ฌ์›์˜ ๊ธ‰์—ฌ๋ฅผ ์ถœ๋ ฅ
--๋‘ ๊ฐœ ๋” ์•ž์„  ์‚ฌ์›์ด ์—†์„ ๊ฒฝ์šฐ 0์„ ์ถœ๋ ฅ

์ด์ „ ๋ช‡ ๋ฒˆ์งธ ํ–‰์˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š” ํ•จ์ˆ˜์ด๋‹ค. ์ธ์ž๋ฅผ ์ตœ๋Œ€ 3๊ฐœ๊นŒ์ง€ ๊ฐ€์ง„๋‹ค.

๋‘๋ฒˆ์งธ ์ธ์ž๋Š” ๋ช‡ ๋ฒˆ์งธ ์•ž์˜ ํ–‰์„ ๊ฐ€์ ธ์˜ฌ์ง€ ๊ฒฐ์ •ํ•˜๋Š” ๊ฒƒ์ด๋ฉฐ DEFAULT๊ฐ’์€ 1์ด๋‹ค. ์„ธ๋ฒˆ์งธ ์ธ์ž๋Š” ๊ฐ€์ ธ์˜ฌ ํ–‰์ด ์—†์„ ๊ฒฝ์šฐ DEFAULT๊ฐ’์„ ์ง€์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์œผ๋กœ NVL์ด๋‚˜ ISNULLํ•จ์ˆ˜์˜ ๊ธฐ๋Šฅ๊ณผ ๋™์ผํ•˜๋‹ค.


LEAD

SELECT ENAME, HIREDATE
     , LEAD(HIREDATE, 1) OVER (ORDER BY HIREDATE) as "NEXTHIRED" 
  FROM EMP;

ENAME      HIREDATE  NEXTHIRED
---------- --------- ---------
SMITH      17-DEC-80 20-FEB-81
ALLEN      20-FEB-81 22-FEB-81
WARD       22-FEB-81 02-APR-81
JONES      02-APR-81 01-MAY-81
BLAKE      01-MAY-81 09-JUN-81
CLARK      09-JUN-81

--HIREDATE๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ๋ณธ์ธ๋ณด๋‹ค HIREDATE๊ฐ€ ํ•˜๋‚˜ ๋” ๋’ค์ธ ๋‚ ์งœ๋ฅผ ์ถœ๋ ฅ
--์—†๋Š” ๊ฒฝ์šฐ NULL

์ดํ›„ ๋ช‡ ๋ฒˆ์งธ ํ–‰์˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š” ํ•จ์ˆ˜๋กœ LAG์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ธ์ž๋ฅผ ์ตœ๋Œ€ 3๊ฐœ๊นŒ์ง€ ๊ฐ–๋Š”๋‹ค.


๊ทธ๋ฃน ๋‚ด ์ง‘๊ณ„ ํ•จ์ˆ˜

์œˆ๋„์šฐ ํ•จ์ˆ˜() OVER (partition by ์ปฌ๋Ÿผ order by ์ปฌ๋Ÿผ RANGE BETWEEN A AND B)

์œˆ๋„์šฐ ํ•จ์ˆ˜() OVER (partition by ์ปฌ๋Ÿผ order by ์ปฌ๋Ÿผ ROWS BETWEEN A AND B)

UNBOUNDED PRECEDING // ์ตœ์ข… ์ถœ๋ ฅ๋  ๊ฐ’์˜ ๋งจ ์ฒ˜์Œ row ๊ฐ’

CURRENT ROW // ํ˜„์žฌ row ๊ฐ’

UNBOUNDED FOLLOWING // ์ตœ์ข… ์ถœ๋ ฅ๋  ๊ฐ’์˜ ๋งจ ๋งˆ์ง€๋ง‰ row ๊ฐ’
// ์ง์—…๋ณ„ ์›”๊ธ‰ ๋ˆ„์  ํ•ฉ๊ณ„ ๊ตฌํ•˜๊ธฐ
SELECT ENAME, JOB, SAL, 
			 SUM(SAL) OVER (PARTITION BY JOB) AS ์›”๊ธ‰ ๋ˆ„์  ํ•ฉ๊ณ„
FROM EMP

// ์ง์—…๋ณ„ ์›”๊ธ‰ ๋ˆ„์  ํ•ฉ๊ณ„ ๊ตฌํ•˜๊ณ , ์›”๊ธ‰ ์ˆœ์„œ๋Œ€๋กœ ๋ณด์—ฌ์ฃผ๊ธฐ
SELECT ENAME, JOB, SAL, 
			 SUM(SAL) OVER (PARTITION BY JOB ORDER BY SAL) AS ์›”๊ธ‰ ๋ˆ„์  ํ•ฉ๊ณ„
FROM EMP
MAX(์—ด) OVER (PARTITION BY ๊ทธ๋ฃน์—ด)  -- ์˜ˆ์‹œ) ์ตœ๋Œ€๊ฐ’
 
SUM(์—ด) OVER (ORDER BY ์ˆœ์„œ์—ด) -- ๋ˆ„์ ํ•ฉ
SUM(์—ด) OVER (ORDER BY ์ˆœ์„œ์—ด PARTITION BY ๊ทธ๋ฃน์—ด) -- ๊ทธ๋ฃน๋ผ๋ฆฌ ๋ˆ„์ ํ•ฉ

SUM

SELECT MGR, ENAME, SAL, 
       SUM(SAL) OVER (PARTITION BY MGR) MGR_SUM
FROM   EMP ;

๊ทธ๋ฃน ๋‚ด ๋น„์œจ ํ•จ์ˆ˜

RATIO_TO_REPORT

SELECT ENAME, SAL
     , ROUND(RATIO_TO_REPORT(SAL) OVER (), 2) as R_R
  FROM EMP
 WHERE JOB = 'SALESMAN';

ENAME             SAL        R_R
---------- ---------- ----------
ALLEN            1600        .29
WARD             1250        .22
MARTIN           1250        .22
TURNER           1500        .27

--์ „์ฒด ๊ธ‰์—ฌ์—์„œ ๊ฐ๊ฐ์ด ์ฐจ์ง€ํ•˜๋Š” ๋น„์œจ ์ถœ๋ ฅ

ํŒŒํ‹ฐ์…˜ ๋‚ด ์ „์ฒด SUM๊ฐ’์— ๋Œ€ํ•œ ํ–‰๋ณ„ ์ปฌ๋Ÿผ ๊ฐ’์˜ ๋ฐฑ๋ถ„์œจ์„ ์†Œ์ˆ˜์ ์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค. ๊ฒฐ๊ณผ๊ฐ’์€ 0~1 ์‚ฌ์ด์ด๋ฉฐ ๊ฐœ๋ณ„ ๋น„์œจ์˜ ํ•ฉ์„ ๊ตฌํ•˜๋ฉด 1์ด๋‹ค.


PERCENT_RANK

SELECT DEPTNO, ENAME, SAL
     , PERCENT_RANK() OVER (PARTITION BY DEPTNO ORDER BY SAL DESC) as P_R
  FROM EMP;

    DEPTNO ENAME             SAL        P_R
---------- ---------- ---------- ----------10 KING             5000          0
        10 CLARK            2450         .5
        10 MILLER           1300          1
        20 SCOTT            3000          0
        20 FORD             3000          0
        20 JONES            2975         .5
        20 ADAMS            1100        .75
        20 SMITH             800          1
        30 BLAKE            2850          0
        30 ALLEN            1600         .2
        30 TURNER           1500         .4
        30 MARTIN           1250         .6
        30 WARD             1250         .6
        30 JAMES             950          1

--DEPTNO๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํŒŒํ‹ฐ์…”๋‹ํ•ด์„œ ์ˆœ์„œ๋ณ„ ๋ฐฑ๋ถ„์œจ ์ถœ๋ ฅ

ํŒŒํ‹ฐ์…˜๋ณ„๋กœ ๊ฐ€์žฅ ๋จผ์ €ย ๋‚˜์˜ค๋Š”ย ๊ฐ’์„ย 0,ย ๊ฐ€์žฅย ๋งˆ์ง€๋ง‰์—ย ๋‚˜์˜ค๋Š”ย ๊ฐ’์„ย 1๋กœย ํ•ด์„œย ํ–‰ย ์ˆœ์„œ๋ณ„ย ๋ฐฑ๋ถ„์œจย ์ถœ๋ ฅํ•œ๋‹ค. ๊ตฌ๊ฐ„์„ ๋‚˜๋ˆ„์–ด ๋ฐฑ๋ถ„์œจ๋กœ ํ‘œ์‹œํ•œ๋‹ค.


CUME_DIST

SELECT DEPTNO, ENAME, SAL
     , CUME_DIST() OVER (PARTITION BY DEPTNO ORDER BY SAL DESC) as CUME_DIST
  FROM EMP;

    DEPTNO ENAME             SAL  CUME_DIST
---------- ---------- ---------- ----------10 KING             5000 .333333333
        10 CLARK            2450 .666666667
        10 MILLER           1300          1
        20 SCOTT            3000         .4
        20 FORD             3000         .4
        20 JONES            2975         .6
        20 ADAMS            1100         .8
        20 SMITH             800          1
        30 BLAKE            2850 .166666667
        30 ALLEN            1600 .333333333
        30 TURNER           1500         .5
        30 MARTIN           1250 .833333333
        30 WARD             1250 .833333333
        30 JAMES             950          1

--DEPTNO๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํŒŒํ‹ฐ์…”๋‹ํ•ด์„œ ๋ˆ„์  ๋ฐฑ๋ถ„์œจ ์ถœ๋ ฅ

ํŒŒํ‹ฐ์…˜๋ณ„ย ์ „์ฒด๊ฑด์ˆ˜์—์„œย ํ˜„์žฌย ํ–‰๋ณด๋‹คย ์ž‘๊ฑฐ๋‚˜ย ๊ฐ™์€ย ๊ฑด์ˆ˜์—ย ๋Œ€ํ•œย ๋ˆ„์ ๋ฐฑ๋ถ„์œจ ์„๊ตฌํ•œ๋‹ค.


NTILE

SELECT ENAME, SAL
     , NTILE(4) OVER (ORDER BY SAL DESC) as QUAR_TILE
  FROM EMP ;

ENAME             SAL  QUAR_TILE
---------- ---------- ----------
KING             5000          1
FORD             3000          1
SCOTT            3000          1
JONES            2975          1
BLAKE            2850          2
CLARK            2450          2
ALLEN            1600          2
TURNER           1500          2
MILLER           1300          3
WARD             1250          3
MARTIN           1250          3
ADAMS            1100          4
JAMES             950          4
SMITH             800          4

--์ „์ฒด๊ฑด์ˆ˜๋ฅผ ARGUMENT๊ฐ’(์—ฌ๊ธฐ์„œ๋Š” 4)์œผ๋กœ N๋“ฑ๋ถ„ํ•œ ๊ฒฐ๊ณผ ์ถœ๋ ฅ--14๋ฅผ 4๋กœ ๋‚˜๋ˆ„๋ฉด ๋ชซ์ด 3 ๋‚˜๋จธ์ง€๊ฐ€ 2, ๋‚˜๋จธ์ง€2๋Š” ์•ž์˜ ์กฐ๋ถ€ํ„ฐ ํ• ๋‹น๋˜๊ธฐ ๋•Œ๋ฌธ์— 4,4,3,3์œผ๋กœ ๋‚˜๋ˆ”

ํŒŒํ‹ฐ์…˜๋ณ„ย ์ „์ฒดย ๊ฑด์ˆ˜๋ฅผย ARGUMENT๊ฐ’์œผ๋กœย N๋“ฑ๋ถ„ํ•œย ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


๊ถŒํ•œ๋ถ€์—ฌ ํ•˜๊ธฐ

DBMS ์‚ฌ์šฉ์ž๋ฅผ ์ƒ์„ฑํ•˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ๋งŽ์€ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•ด์•ผํ•œ๋‹ค.

๋งŽ์€ DBMS ์—์„œ๋Š” DBMS ๊ด€๋ฆฌ์ž๊ฐ€ ์‚ฌ์šฉ์ž๋ณ„๋กœ ๊ถŒํ•œ์„ ๊ด€๋ฆฌํ•ด์•ผ ํ•˜๋Š” ๋ถ€๋‹ด๊ณผ ๋ณต์žกํ•จ์„ ์ค„์ด๊ธฐ ์œ„ํ•˜์—ฌ ๋‹ค์–‘ํ•œ ๊ถŒํ•œ์„ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์–ด ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์‚ฌ์šฉ์ž์™€ ๊ถŒํ•œ ์‚ฌ์ด์—์„œ ์ค‘๊ฐœ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ROLE ์„ ์ œ๊ณตํ•œ๋‹ค.


GRANT(๊ถŒํ•œ ๋ถ€์—ฌ)

// ์‚ฌ์šฉ์ž U1, U2, U3์—๊ฒŒ professor ํ…Œ์ด๋ธ”์— ๋Œ€ํ•œ select ๋ฌธ์žฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์„ ๋ถ€์—ฌ
Grant select on professor to U1, U2, U3;

// ์‚ฌ์šฉ์ž U4์—๊ฒŒ professor ํ…Œ์ด๋ธ”์— ๋Œ€ํ•œ select ๋ฌธ์žฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•˜๊ณ  
// ๋˜ํ•œ U4์—๊ฒŒ ๋ฐ›์€ ๊ถŒํ•œ์„ ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์„ ํ•จ๊ป˜ ๋ถ€์—ฌ
Grant select on professor to U4 with grant option;

// Lee ์‚ฌ์šฉ์ž์—๊ฒŒ department ํ…Œ์ด๋ธ”์˜ deptName ์†์„ฑ์„ ์ฐธ์กฐํ•˜๋Š” ์™ธ๋ž˜ํ‚ค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ถŒํ•œ์„ ๋ถ€์—ฌ
Grant references (deptName) on department to Lee;

// ๋ชจ๋“ ์‚ฌ์šฉ์ž์—๊ฒŒ ๊ถŒํ•œ๋ถ€์—ฌ
Grant select on professor to public; 

REVOKE(๊ถŒํ•œ ์ฐจ๋‹จ)

// U1์—๊ฒŒ๋งŒ professor ํ…Œ์ด๋ธ”์— ๋Œ€ํ•œ select ๊ถŒํ•œ์„ ํšŒ์ˆ˜ํ•˜๋ผ
Revoke select on professor from U1

// U1, U2, U3๋Š” ๋ฌผ๋ก ์ด๊ณ  U1,U2,U3๊ฐ€ ์‹ธ์งˆ๋Ÿฌ๋†“์€ ๊ถŒํ•œ๊นŒ์ง€๋„ ๋ชจ๋‘ ์ฒ ํšŒํ•˜์‹œ์˜ค
Revoke select on professor from U1, U2, U3 cascade;

// U1, U2, U3๊ฐ€ ์•„๋ฌด๊ฒƒ๋„ ์‹ธ์งˆ๋Ÿฌ๋†“์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋งŒ ์–˜๋“ค ๊ถŒํ•œ์„ ๋ชจ๋‘ ์ฒ ํšŒํ•˜์‹œ์˜ค.
Revoke select on professor from U1, U2, U3 restrict;

// ๋ชจ๋“  ์‚ฌ์šฉ์ž ๊ถŒํ•œ ์ฒ ํšŒ
revoke select on professor from public

cascade

๋งŒ์•ฝ์— ์‚ฌ์šฉ์ž U1์ด U2์—๊ฒŒ ํŠน์ • ๊ถŒํ•œ์„ ์ฃผ์—ˆ๊ณ , ๋˜ U2๊ฐ€ ๋™์ผ ๊ถŒํ•œ์„ ์‚ฌ์šฉ์ž U3์—๊ฒŒ ๋ถ€์—ฌํ–ˆ์„ ๋•Œ, ์ด ๊ฒฝ์šฐ ์‚ฌ์šฉ์ž U1์ด U2์—๊ฒŒ ๊ถŒํ•œ ์ทจ์†Œ๋ฅผ ํ•œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ?

cascadeย ์˜ต์…˜์€ ๊ถŒํ•œ ์ทจ์†Œ ์‹œ์— ์ทจ์†Œ๋˜๋Š” ๊ถŒํ•œ์œผ๋กœ ์ธํ•˜์—ฌ ํ•จ๊ป˜ ์ทจ์†Œ๊ฐ€ ๋˜์–ด์•ผ ํ•˜๋Š” ๊ถŒํ•œ์ด ์žˆ์œผ๋ฉด ๊ทธ ๊ถŒํ•œ๋„ ํ•จ๊ป˜ ์ทจ์†Œํ•˜๋Š” ๊ฒƒ์ด๋ฉฐ, ์ฆ‰ U1์ด U2์—๊ฒŒ ๊ถŒํ•œ์ทจ์†Œ ๋ช…๋ น์„ ๋‚ด๋ ธ์„ ๋•Œ, ๊ฑฐ๊ธฐ์— ๋”ธ๋ฆฐ ๋†ˆ U3์˜ ๊ถŒํ•œ๋„ ์ฒ ํšŒ๋œ๋‹ค.

restrict

๋งŒ์•ฝ์— ์‚ฌ์šฉ์ž U1์ด U2์—๊ฒŒ ํŠน์ •๊ถŒํ•œ์„ ์ฃผ์—ˆ๋Š”๋ฐ, U3๊ฐ€ U2์—๊ฒŒ ๋™์ผ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•˜์˜€๋‹ค๋ฉด ์‚ฌ์šฉ์ž U1์ดย Revoke select on professor from U2;ย ๋ผ๋Š” ๊ตฌ๋ฌธ์„ ์ž…๋ ฅํ–ˆ์„ ๋•Œ U2์˜ select๊ถŒํ•œ์€ ์—†์–ด์งˆ๊นŒ?

์ด ๊ฒฝ์šฐ ์‚ฌ์šฉ์ž U3๋„ย Revoke select on professor from U2;ย ๋ฅผ ์ž…๋ ฅํ•ด์•ผ U2๋Š” professor์—๋Œ€ํ•œ select๊ถŒํ•œ์ด ์™„์ „ํžˆ ์—†์–ด์ง€๊ฒŒ ๋œ๋‹ค.


๊ธฐํƒ€

0 ๋‚˜๋ˆ„๊ธฐ

  • A / 0 โ†’ 0
  • 0 / A โ†’ ERROR
  • 0 / NULL, NULL / 0, A / NULL, NULL / A โ†’ NULL

null

  • ๋ชจ๋ฅด๋Š” ๊ฐ’์„ ์˜๋ฏธํ•œ๋‹ค.
  • ๊ฐ’์˜ ๋ถ€์žฌ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
  • NULL๊ณผ์˜ ๋ชจ๋“  ๋น„๊ต๋Š” Unknown์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • NULL๊ณผ์˜ ๋ชจ๋“  ์—ฐ์‚ฐ์€ NULL์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

ORACLE๊ณผ SQL

  • orcle์€ โ€˜โ€™๋กœ ์ž…๋ ฅํ•˜๋ฉด null๋กœ ์ธ์‹ / sql์€ โ€˜โ€™๋กœ ์ธ์‹
  • Oracle์—์„œ๋Š” NULL์„ ๊ฐ€์žฅ ํฐ ๊ฐ’์œผ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค.
  • SQL์—์„œ๋Š” NULL์„ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์œผ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค.

About

๐Ÿ“š SQL ๋ช…๋ น์–ด์˜ ๋ชจ๋“ ๊ฒƒ

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published