๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป/ORACLE

[ORACLE] 2์ผ์ฐจ : SELECT ๊ธฐ๋ณธ

by ๋”ฐ๊ถˆ 2024. 3. 12.

 

SELECT๋ฌธ
ํ…Œ์ด๋ธ”์ด๋‚˜ ๋ทฐ์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜์—ฌ 0๊ฐœ ์ด์ƒ์˜ ํ–‰์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
SELECT๋Š” ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” DQL(data query language) ๋ช…๋ น์ด๋ฉฐ, DML(data manipulation language)์˜ ๊ตฌ์„ฑ ์š”์†Œ๋กœ ๊ฐ„์ฃผ๋˜๊ธฐ๋„ ํ•œ๋‹ค.

 SELECT๋ฌธ์€ ๋ฐ˜๋“œ์‹œ SELECT์™€ FROM์˜ ๋‘ ๊ฐœ์˜ ํ‚ค์›Œ๋“œ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.
SELECT์ ˆ ๋‹ค์Œ์—๋Š” ์ถœ๋ ฅํ•  ์ปฌ๋Ÿผ๋ช…์ด๋‚˜ ํ‘œํ˜„์‹์„ ๊ธฐ์ˆ ํ•˜๊ฑฐ๋‚˜ ํŠน์ • ์ปฌ๋Ÿผ๋ช… ๋Œ€์‹  '*' ๊ธฐ์ˆ ํ•˜์—ฌ ํ…Œ์ด๋ธ”(๋ทฐ) ๋‚ด์˜ ๋ชจ๋“  ์ปฌ๋Ÿผ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.
FROM์ ˆ ๋‹ค์Œ์—๋Š” ์กฐํšŒํ•  ํ…Œ์ด๋ธ”(๋ทฐ)์˜ ์ด๋ฆ„์„ ๊ธฐ์ˆ ํ•œ๋‹ค.

 

 

SELECT๋ฌธ ํ˜•์‹

  • ํ…Œ์ด๋ธ” ๋ชฉ๋กํ™•์ธ : SELECT * FROM tab; 

  • ํ…Œ์ด๋ธ”์˜ ์ปฌ๋Ÿผ ๋ชฉ๋ก ํ™•์ธ : SELECT * FROM col WHERE tname = 'EMP' / DESC emp;
  • ํŠน์ • ํ…Œ์ด๋ธ”๋งŒ ํ™•์ธ : SELECT empNO,name,sal FROM emp;
  • ์กด์žฌ ํ•˜์ง€ ์•Š๋Š” ์ปฌ๋Ÿผ ํ™•์ธ : SELECT no, name, sal FROM emp;
  • ๋ชจ๋“  ์ปฌ๋Ÿผ ์ถœ๋ ฅ : SELECT * FROM emp;
  • ์ปฌ๋Ÿผ๋ช…์„ ๋ณ€๊ฒฝ ํ•˜์—ฌ ์ถœ๋ ฅ :  SELECT empNO AS ์‚ฌ๋ฒˆ, name AS ์ด๋ฆ„, sal AS ๊ธ‰์—ฌ FROM emp; / SELECT empNO ์‚ฌ๋ฒˆ, name ์ด๋ฆ„, sal ๊ธ‰์—ฌ FROM emp; -- AS ์ƒ๋žต ๊ฐ€๋Šฅ

 

SELECT๋ฌธ ๊ตฌํ˜„ ์ˆœ์„œ ๋ฐ ์‹คํ–‰ ์ˆœ์„œ

 

 

  1. FROM ์ ˆ์—์„œ ํ…Œ์ด๋ธ”์˜ ๋ชฉ๋ก์„ ๊ฐ€์ ธ์˜จ๋‹ค.
  2. WHERE ์ ˆ์—์„œ ๊ฒ€์ƒ‰ ์กฐ๊ฑด์— ๋ถˆ์ผ์น˜ ํ•˜๋Š” ํ–‰์„ ์ œ์™ธํ•œ๋‹ค. 
  3. ROWNUM์„ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ ROWNUM์„ ํ• ๋‹นํ•œ๋‹ค.
  4. GROUP BY ์ ˆ์—์„œ ๋ช…์‹œ๋œ ํ–‰์˜ ๊ฐ’์„ ๊ทธ๋ฃนํ™”ํ•œ๋‹ค.
  5. HAVING ์ ˆ์—์„œ GROUP BY์ ˆ์˜ ๊ฒฐ๊ณผ ํ–‰ ์ค‘ ๊ฒ€์ƒ‰ ์กฐ๊ฑด์— ๋ถˆ์ผ์น˜ ํ•˜๋Š” ํ–‰์„ ์ œ์™ธํ•œ๋‹ค.
  6. SELECT ์ ˆ์—์„œ ๋ช…์‹œ๋œ ์ปฌ๋Ÿผ(ํ‘œํ˜„์‹)์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.
  7. ORDER BY ์ ˆ์—์„œ ์ปฌ๋Ÿผ(ํ‘œํ˜„์‹)์„ ๊ธฐ์ค€์œผ๋กœ ์ถœ๋ ฅํ•  ๋Œ€์ƒ์„ ์ •๋ ฌ ํ›„ ์ถœ๋ ฅํ•œ๋‹ค.

 

 WHERE ์กฐ๊ฑด

์„ ํƒํ•œ ํ–‰์„ ํ•˜๋‚˜ ์ด์ƒ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ํ–‰์œผ๋กœ ์ œํ•œํ•œ๋‹ค. WHERE์ ˆ์„ ์ƒ๋žตํ•˜๋ฉด, FROM์ ˆ์˜ ํ…Œ์ด๋ธ” ๋˜๋Š” ๋ทฐ์—์„œ ๋ชจ๋“  ํ–‰์„ ๋ฆฌํ„ด ํ•œ๋‹ค. 

์กฐ๊ฑด์‹(Condition) : ์กฐ๊ฑด ๋˜๋Š” ์กฐ๊ฑด์‹์€ ํ•œ ๊ฐœ ์ด์ƒ์˜ ํ‘œํ˜„์‹๊ณผ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž๊ฐ€ ๊ฒฐํ•ฉ๋œ ์‹์œผ๋กœ TRUE, FALSE, UNKNOWN์„ธ ๊ฐ€์ง€ ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

SELECT ์ปฌ๋Ÿผ๋ช…, ์ปฌ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ”๋ช… WHERE ์กฐ๊ฑด;

 

๋น„๊ต ๋ฐ ๋…ผ๋ฆฌ์—ฐ์‚ฐ์ž

 

 

ANY(SOME), ALL

  • ANY(SOME)
    • ๊ฐ’์˜ ๋ชฉ๋ก์˜ ๊ฐ ๊ฐ’๊ณผ ๋น„๊ตํ•˜๊ฑฐ๋‚˜ ์ฟผ๋ฆฌ์—์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.(OR ๊ฐœ๋…๊ณผ ์œ ์‚ฌ)
    • =,!=,>,<,<=,>= ์•ž์— ๊ธฐ์ˆ ํ•˜๋ฉฐ, ํ•˜๋‚˜ ์ด์ƒ์˜ ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋Š” ํ‘œํ˜„์‹ ๋˜๋Š” ์„œ๋ธŒ ์ฟผ๋ฆฌ๊ฐ€ ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.
  • ALL 
    • ๊ฐ’์„ ๋ชฉ๋ก์˜ ๋ชจ๋“  ๊ฐ’๊ณผ ๋น„๊ตํ•˜๊ฑฐ๋‚˜ ์ฟผ๋ฆฌ์—์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.(AND ๊ฐœ๋…๊ณผ ์œ ์‚ฌ)
    • =,!=,>,<,<=,>= ์•ž์— ๊ธฐ์ˆ ํ•˜๋ฉฐ, ํ•˜๋‚˜ ์ด์ƒ์˜ ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋Š” ํ‘œํ˜„์‹ ๋˜๋Š” ์„œ๋ธŒ ์ฟผ๋ฆฌ๊ฐ€ ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.
emp ํ…Œ์ด๋ธ” : city๊ฐ€ '์„œ์šธ', '๊ฒฝ๊ธฐ', '์ธ์ฒœ' ์ธ ์ž๋ฃŒ ์ค‘ empNo, name, city ์ปฌ๋Ÿผ ์ถœ๋ ฅ
SELECT empNO,name,city
FROM emp
WHERE city= ANY('์„œ์šธ','๊ฒฝ๊ธฐ','์ธ์ฒœ');

 

emp ํ…Œ์ด๋ธ” : sal๊ฐ€ 2000000์› ์ด์ƒ์ธ ์ž๋ฃŒ ์ค‘ empNo, name, sal ์ปฌ๋Ÿผ ์ถœ๋ ฅ
SELECT empNO,name,city
FROM emp
WHERE sal >= 2000000;

 

 

SQL  ์—ฐ์‚ฐ์ž 

 

  • BETWEEN ์กฐ๊ฑด์‹
    • BETWEEN ์กฐ๊ฑด์€ ํ•œ ์‹์˜ ๊ฐ’์ด ๋‹ค๋ฅธ ๋‘ ์‹์œผ๋กœ ์ •์˜ ๋œ ๊ฐ„๊ฒฉ์— ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค.
    • ์„ธ๊ฐ€์ง€ ํ‘œํ˜„์‹์€ ๋ชจ๋‘ ์ˆซ์ž, ๋ฌธ์ž ๋˜๋Š” ๋‚ ์งœ / ์‹œ๊ฐ„ ํ‘œํ˜„์‹์ด์–ด์•ผ ํ•œ๋‹ค.
    • ํ‘œํ˜„์‹์ด ๋ชจ๋‘ ๋™์ผํ•œ ๋ฐ์ดํ„ฐ ์œ ํ˜•์ด ์•„๋‹Œ ๊ฒฝ์šฐ ์•”์‹œ์ ์œผ๋กœ ํ‘œํ˜„์‹์„ ๊ณตํ†ต ๋ฐ์ดํ„ฐ ์œ ํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•  ์ˆ˜ ์—†์œผ๋ฉด ์˜ค๋ฅ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
emp ํ…Œ์ด๋ธ” : sal๊ฐ€ 2000000~3000000 ์‚ฌ์ด ์ธ ์ž๋ฃŒ ์ค‘ name, sal ์ปฌ๋Ÿผ ์ถœ๋ ฅ
SELECT name,sal
FROM emp
WHERE sal BETWEEN 2000000 AND 3000000;
emp ํ…Œ์ด๋ธ” : hireDate๊ฐ€ 2022๋…„๋„์ธ ์ž๋ฃŒ์ค‘ name, hireDate ์ปฌ๋Ÿผ ์ถœ๋ ฅ
SElECT name,hireDate
FROM emp
WHERE hireDate BETWEEN '2022-01-01' AND '2022-12-31'
  • IN ์กฐ๊ฑด์‹
    • list์— ์žˆ๋Š” ๊ฐ’ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ์ผ์น˜ํ•˜๋ฉด TRUE๋‹ค.
emp ํ…Œ์ด๋ธ” : city๊ฐ€ '์„œ์šธ', '์ธ์ฒœ', '๊ฒฝ๊ธฐ' ์ธ ์ž๋ฃŒ ์ค‘ name, city ์ปฌ๋Ÿผ ์ถœ๋ ฅ
SELECT empNO,city
FROM emp
WHERE city in('์„œ์šธ', '๊ฒฝ๊ธฐ', '์ธ์ฒœ');
emp ํ…Œ์ด๋ธ” : city์™€ pos๊ฐ€ '์„œ์šธ ์ด๋ฉด์„œ '๋ถ€์žฅ'์ด๊ฑฐ๋‚˜ '๊ฒฝ๊ธฐ' ์ด๋ฉด์„œ '๊ณผ์žฅ' ์ธ ์ž๋ฃŒ ์ค‘ name, city, pos ์ปฌ๋Ÿผ ์ถœ๋ ฅ
SELECT empNO,city,pos
FROM emp
WHERE (city,pos)IN(('์„œ์šธ','๋ถ€์žฅ'),('๊ฒฝ๊ธฐ','๊ณผ์žฅ'));
  • LIKE ์กฐ๊ฑด์‹
    • LIKE ์กฐ๊ฑด์€ ํŒจํ„ด ์ผ์น˜์™€ ๊ด€๋ จ๋œ ์กฐ๊ฑด์„ ์ง€์ •ํ•œ๋‹ค.
    • ํŒจํ„ด ๋ฌธ์ž 
      • _ :  ๋ฐ‘์ค„(_)์€ ๊ฐ’์—์„œ ์ •ํ™•ํžˆ ํ•˜๋‚˜์˜ ๋ฌธ์ž
      • % : ํผ์„ผํŠธ ๋ถ€ํ˜ธ(%)๋Š” ๊ฐ’์—์„œ 0 ๊ฐœ ์ด์ƒ์˜ ๋ฌธ์ž. null๊ณผ๋Š” ์ผ์น˜ ํ•  ์ˆ˜ ์—†๋‹ค.
    • ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ์‹œ ์˜ค๋ผํด ๋‚ด๋ถ€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ƒ LIKE์กฐ๊ฑด์‹ ๋ณด๋‹ค๋Š” INSTRํ•จ์ˆ˜๊ฐ€ ๋” ๋น ๋ฅด๊ฒŒ ์ฒ˜๋ฆฌ๋œ๋‹ค
emp ํ…Œ์ด๋ธ” : LIKE ์˜ˆ
SELECT empNO, name
FROM emp
WHERE tel Like '%3%'; -- 3์ด ์กด์žฌํ•˜๋Š” ์ „ํ™”๋ฒˆํ˜ธ

SELECT empNO, name
FROM emp
WHERE name LIKE '%3'; -- 3์œผ๋กœ ๋๋‚˜๋Š” ์ „ํ™”๋ฒˆํ˜ธ

SELECT empNO, name, rrn
FROM emp
WHERE rrn LIKE '_0%'; -- ์ƒ๋…„์›”์˜์˜ ์—ฐ๋„๊ฐ€ 10์˜ ๋ฐฐ์ˆ˜

 

 

NULL

ํ–‰์˜ ์—ด์— ๊ฐ’์ด ์—†์œผ๋ฉด ์—ด์€  null์ด๊ฑฐ๋‚˜  null์„ ํฌํ•จํ•œ๋‹ค๊ณ  ํ•œ๋‹ค

NOT NULL ๋˜๋Š” PRIMARY KEY  ๋ฌด๊ฒฐ์„ฑ ์ œ์•ฝ ์กฐ๊ฑด์— ์˜ํ•ด ์ œํ•œ๋˜์ง€ ์•Š๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์œ ํ˜•์˜ ์—ด์—๋Š” ๋„์ด ๋‚˜ํƒ€๋‚  ์ˆ˜ ์žˆ๋‹ค.

์‹ค์ œ ๊ฐ’์„ ์•Œ ์ˆ˜ ์—†๊ฑฐ๋‚˜ ๊ฐ’์ด ์˜๋ฏธ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ๋„์„ ์‚ฌ์šฉํ•œ๋‹ค. ๊ธธ์ด๊ฐ€ 0์ธ ๋ฌธ์ž ๊ฐ’์€ ๋„๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค. 0์ด ์•„๋‹Œ ์ˆซ์ž ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋ž˜๋ฉด null์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค 

 

SELECT 10+NULL FROM dual; -- NULL

 

emp ํ…Œ์ด๋ธ” : tel์ด NULL ์ธ ์ž๋ฃŒ ์ค‘ name, tel ์ปฌ๋Ÿผ ์ถœ๋ ฅ
SELECT name,tel
FROM emp
WHERE tel is NULL;

 

CASEํ‘œํ˜„์‹

 

IF ~ THEN ~ELSE ๋…ผ๋ฆฌ์™€ ์œ ์‚ฌํ•œ ๋ฐฉ์‹์œผ๋กœ ํ‘œํ˜„์‹์„ ์ž‘์„ฑํ•ด์„œ  SQL์˜ ๋น„๊ต ์—ฐ์‚ฐ ๊ธฐ๋Šฅ์„ ๋ณด์™„ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. 

 

  • Simple Case Expression (๊ฐ„๋‹จํ•œ CASE ํ‘œํ˜„์‹)
ํ˜•์‹1 : ๊ฐ„๋‹จํ•œ CASE ํ‘œํ˜„์‹
SELECT name,rrn, CASE SUBSTR(rrn,8,1)
WHEN '1' THEN '๋‚จ์ž'
WHEN '2' THEN '์—ฌ์ž'
WHEN '3' THEN '๋‚จ์ž'
WHEN '4' THEN '์—ฌ์ž'
END "์„ฑ๋ณ„"
FROM emp;

 

SELECT name,rrn, CASE MOD(SUBSTR(rrn,8,1),2)
WHEN 0 THEN '์—ฌ์ž'
WHEN 1 THEN '๋‚จ์ž'
END "์„ฑ๋ณ„"
FROM emp;

 

  • Searched Case Expression
emp : name,sal+bonus, ์„ธ๊ธˆ(sal+bonus >= 300๋งŒ์ด๋ฉด 3%, sal+bonus>=200๋งŒ 2%, ๋‚˜๋จธ์ง€ 0)
SELECT name,sal+bonus ์ด๊ธ‰์—ฌ,
CASE
WHEN sal+bonus >= 3000000 THEN (sal+bonus) * 0.03
WHEN sal+bonus >= 2000000 THEN (sal+bonus) * 0.02
ELSE 0
END AS ์„ธ๊ธˆ
FROM emp;

 

SELECT name,sal+bonus ์ด๊ธ‰์—ฌ,
CASE
WHEN sal+bonus >= 3000000 THEN 0.03
WHEN sal+bonus >= 2000000 THEN 0.02
ELSE 0 END *(sal+bonus) AS ์„ธ๊ธˆ
FROM emp;

 

 

DECODE ํ•จ์ˆ˜

๊ฐ’์„ ๊ฐ ๊ฒ€์ƒ‰๊ณผ ๋น„๊ตํ•˜์—ฌ ๊ฐ™์œผ๋ฉด ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜
DECODE (a,'b',1,2) -- a๊ฐ€ 'b'์ด๋ฉด 1์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด 2๋ฅผ ๋ฐ˜ํ™˜
DECODE (a,'b',1) -- a๊ฐ€ 'b'์ด๋ฉด 1์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด NULL์„ ๋ฐ˜ํ™˜ 
DECODE (a,'b',1,'c',2,3) -- a๊ฐ€ 'b'์ด๋ฉด 1์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , a๊ฐ€ 'c'์ด๋ฉด 2๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด 3์„ ๋ฐ˜ํ™˜

 

SELECT name,rrn,
DECODE(MOD(SUBSTR(rrn,8,1),2),1,'๋‚จ์ž','์—ฌ์ž')์„ฑ๋ณ„
FROM emp; -- ์ฃผ๋ฏผ๋ฒˆํ˜ธ 8๋ฒˆ์งธ๊ฐ€ ํ™€์ˆ˜๋ฉด ๋‚จ์ž, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์—ฌ์ž ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•

 

 

ALL ๊ณผ DISTNCT 

ALL ์˜ต์…˜

์„ ํƒ๋œ ๋ชจ๋“  ํ–‰์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์˜ต์…˜์œผ๋กœ ๊ธฐ๋ณธ ๊ฐ’์€ ALL.

SELECT All dept FROM emp;
SELECT dept FROM emp;

 

DISTINCT , UNIQUE ์˜ต์…˜

์„ ํƒ๋œ ํ–‰ ์ค‘์—์„œ ์ค‘๋ณต์ ์ธ ํ–‰์€ ํ•˜๋‚˜๋งŒ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด DISTINCT๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ค‘๋ณต ํ–‰์€ SELECT ๋ชฉ๋ก์˜ ๊ฐ ํ‘œํ˜„์‹์— ๋Œ€ํ•ด ์ผ์น˜ํ•˜๋Š” ๊ฐ’์„ ๊ฐ–๋Š” ํ–‰์ด๋‹ค. DISTINCT๋Š” UNIQUE์™€ ๋™์ผํ•˜๋ฏ€๋กœ ์–ด๋А ๊ฒƒ์„ ์‚ฌ์šฉํ•ด๋„ ์ƒ๊ด€ ์—†๋‹ค.

 

SELECT DISTINCT dept FROM emp; -- ํ‘œ์ค€
SELECT UNIQUE dept FROM emp;

 

 

ORDER BY์ ˆ

SQL ๋ฌธ์žฅ์œผ๋กœ ์กฐํšŒ๋œ ๋ฐ์ดํ„ฐ๋“ค์„ ๋‹ค์–‘ํ•œ ๋ชฉ์ ์— ๋งž๊ฒŒ ํŠน์ • ์ปฌ๋Ÿผ์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•œ๋‹ค. ORDER BY์ ˆ์— ์ปฌ๋Ÿผ(column)๋ช… ๋Œ€์‹ ์— SELECT ์ ˆ์—์„œ ์‚ฌ์šฉํ•œ ALIAS๋ช…์ด๋‚˜ ์ปฌ๋Ÿผ ์ˆœ์„œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

SELECT name,dept,sal
FROM emp
ORDER BY sal asc; -- sal ์˜ค๋ฆ„์ฐจ์ˆœ ์ƒ๋žต๊ฐ€๋Šฅ

SELECT name,dept,sal
FROM emp
ORDER BY sal DESC; --sal ๋‚ด๋ฆผ์ฐจ์ˆœ

 

 

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

๋‘ ๊ฐœ์˜ ๊ตฌ์„ฑ ์š”์†Œ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋‹จ์ผ ๊ฒฐ๊ณผ๋กœ ๊ฒฐํ•ฉํ•œ๋‹ค. ์ง‘ํ•ฉ ์—ฐ์‚ฐ์ž๋ฅผ ํฌํ•จํ•˜๋Š” ์ฟผ๋ฆฌ๋ฅผ ๋ณตํ•ฉ ์ฟผ๋ฆฌ๋ผ๊ณ  ํ•œ๋‹ค.

 

UNIONS(ํ•ฉ์ง‘ํ•ฉ)

์ฒซ ๋ฒˆ์งธ SQL๋ฌธ์˜ ๊ฒฐ๊ณผ์™€ ๋‘ ๋ฒˆ์งธ SQL๋ฌธ์˜ ๊ฒฐ๊ณผ ์ค‘ ์ค‘๋ณต๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.

SELECT name, dept, pos, city FROM emp WHERE city = '์„œ์šธ'
UNION
SELECT name, dept, pos, city FROM emp WHERE city = '๊ฐœ๋ฐœ๋ถ€';

 

UNION ALL

์ฒซ ๋ฒˆ์จฐ SQL๋ฌธ์˜ ๊ฒฐ๊ณผ์™€ ๋‘ ๋ฒˆ์งธ SQL๋ฌธ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ๋‘ ์ถœ๋ ฅํ•œ๋‹ค.

SELECT name, dept, pos, city FROM emp WHERE city = '์„œ์šธ'
UNION ALL
SELECT name, dept, pos, city FROM emp WHERE dept = '๊ฐœ๋ฐœ๋ถ€';

 

 

MINUS (์ฐจ์ง‘ํ•ฉ)

์ฒซ ๋ฒˆ์งธ SQL๋ฌธ ๊ฒฐ๊ณผ์—๋Š” ์žˆ๊ณ , ๋‘ ๋ฒˆ์งธ SQL๋ฌธ์˜ ๊ฒฐ๊ณผ์—๋Š” ์—†๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

SELECT name, dept, pos, city FROM emp WHERE city = '์„œ์šธ'
MINUS
SELECT name, dept, pos, city FROM emp WHERE dept = '๊ฐœ๋ฐœ๋ถ€';

 

INTERSECT(๊ต์ง‘ํ•ฉ)

์ธํ„ฐ์„นํŠธ๋Š” ๋‘๋ฒˆ์งธ SQL๋ฌธ์˜ ๊ฒฐ๊ณผ์™€ ์ฒซ๋ฒˆ์งธ SQL๋ฌธ์˜ ๊ฒฐ๊ณผ์— ์ค‘๋ณต๋œ ํ–‰๋งŒ ์ถœ๋ ฅํ•œ๋‹ค.

SELECT name, dept, pos, city FROM emp WHERE city = '์„œ์šธ'
INTERSECT
SELECT name, dept, pos, city FROM emp WHERE dept = '๊ฐœ๋ฐœ๋ถ€';

 

 

pseudo ์ปฌ๋Ÿผ

์˜ค๋ผํด์—์„œ ๋‚ด๋ถ€์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์ปฌ๋Ÿผ์„ ์ˆ˜๋„์šฐ ์ปฌ๋Ÿผ์ด๋ผ๊ณ  ํ•˜๋ฉฐ, ROWID, UROWID,ROWNUM๋“ฑ์ด ์žˆ๋‹ค. ํ…Œ์ด๋ธ” ์—ด์ฒ˜๋Ÿผ ๋™์ž‘ํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ ํ…Œ์ด๋ธ”์—๋Š” ์ €์žฅ๋˜์ง€ ์•Š๋Š”๋‹ค. pseudo ์ปฌ๋Ÿผ์ค‘์—์„œ ์„ ํƒํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์‚ฝ์ž…,์ˆ˜์ •,์‚ญ์ œ ํ•  ์ˆ˜ ์—†๋‹ค. pseudo์ปฌ๋Ÿผ์€ ์ธ์ˆ˜๊ฐ€ ์—†๋Š” ํ•จ์ˆ˜์™€ ๋น„์Šทํ•˜๋‹ค. ์ธ์ˆ˜๊ฐ€ ์—†๋Š” ํ•จ์ˆ˜๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์˜ ๋ชจ๋“  ํ–‰์— ๋™์ผํ•œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ pseudo ์ปฌ๋Ÿผ์€ ๊ฐ ํ–‰์— ๋Œ€ํ•ด ๋‹ค๋ฅธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 

 

ROWID

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๊ฐ ํ–‰์— ๋Œ€ํ•ด ROWID๋Š” ํ–‰์˜ ์ฃผ์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ROWID ๊ฐ’์—๋Š” ํ–‰์„ ์ฐพ๋Š”๋ฐ ํ•„์š”ํ•œ ์ •๋ณด๊ฐ€ ํฌํ•จ๋œ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ROWID ๊ฐ’์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ํ–‰์„ ๊ณ ์œ ํ•˜๊ฒŒ ์‹๋ณ„ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋™์ผํ•œ ํด๋Ÿฌ์Šคํ„ฐ์— ํ•จ๊ป˜ ์ €์žฅ๋œ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์˜ ํ–‰์€ ๋™์ผํ•œ ROWID๋ฅผ ๊ฐ€์งˆ์ˆ˜ ์žˆ๋‹ค. ROWID์„ ์ด์šฉํ•˜์—ฌ ํ–‰์ˆ˜๋ฅผ ์ œํ•œ ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

SELECT ROWID, name,dept FROM emp;

 

 

 

ROWNUM

์ฟผ๋ฆฌ์—์„œ ๋ฐ˜ํ™˜ ๋œ ๊ฐ ํ–‰์— ๋Œ€ํ•ด ROWNUM์€ ํ…Œ์ด๋ธ” ๋˜๋Š” ์กฐ์ธ๋œ ํ–‰ ์ง‘ํ•ฉ์—์„œ ํ–‰์„ ์„ ํƒํ•˜๋Š” ์ˆœ์„œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ฆ‰, ์„ ํƒ๋œ ์ฒซ ๋ฒˆ์งธ ํ–‰์˜ ROWNUM ์€ 1์ด๊ณ  ๋‘๋ฒˆ์งธ ํ–‰์€ 2์ด๋‹ค. 

SELECT * FROM emp WHERE ROWNUM <11; -- ์–‘์ˆ˜๋ณด๋‹ค ํฐ ROWNUM ๊ฐ’์— ๋Œ€ํ•œ ์กฐ๊ฑด ํ…Œ์ŠคํŠธ๋Š” ํ•ญ์ƒ ๊ฑฐ์ง“ SELECT * FROM emp WHERE ROWNUM >1; -- ๊ฑฐ์ง“, ์•„๋ฌด๊ฒƒ๋„ ์ถœ๋ ฅ์•ˆ๋จ 1๋ณด๋‹ค ํฐ์ˆ˜์™€ ROWNUM์— ๋Œ€ํ•œ ๋™๋“ฑ ๋น„๊ต๋Š” ํ•ญ์ƒ ๊ฑฐ์ง“
SELECT * FROM emp WHERE ROWNUM =10; -- ๊ฑฐ์ง“, ์•„๋ฌด๊ฒƒ๋„ ์ถœ๋ ฅ์•ˆ๋จ 1๊ณผ ROWNUM์— ๋Œ€ํ•œ ๋™๋“ฑ ๋น„๊ต๋Š” ๊ฐ€๋Šฅ
SELECT * FROM emp WHERE ROWNUM =1; -- ๊ฑฐ์ง“, ์•„๋ฌด๊ฒƒ๋„ ์ถœ๋ ฅ์•ˆ๋จ  ORDER BY์ด ์žˆ๋Š” ๊ฒฝ์šฐ ROWNUM๊ณผ ๋น„๊ตํ•ด์„œ๋Š” ์•ˆ๋œ๋‹ค

 

 

SUB QUERY ์‚ฌ์šฉ

SELECT * FROM( -- sal ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜์—ฌ ์•ž์—์„œ 10๊ฐœ๋งŒ ์ถœ๋ ฅ
SELECT * FROM emp
ORDER By sal
) WHERE ROWNUM <11;

 

 ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ์—์„œ ์ž์ฃผ ์‚ฌ์šฉ

SELECT * FROM(
SELECT ROWNUM rnum,tb.*FROM(
SELECT empNO, name, dept, sal
FROM emp
ORDER BY sal DESC
) tb WHERE ROWNUM <= 30
)WHERE rnum >=21;