Top N 쿼리
κ²°κ³Ό μ§ν©μμ μμ Nκ°μ νμ λ°ννλ 쿼리
μ’ λ₯
β’
ROWNUM
β’
ROWID
ROWNUM
SELECT λ¬Έμ λ
Όλ¦¬μ μΈ μλ²μ λνλ΄λ κ°μμ 컬λΌ
ORACLE DB
λ°μ΄ν°λ₯Ό μΆλ ₯ν λ λΆμ¬λλ λ
Όλ¦¬μ μΈ μμ λ²νΈμ΄κΈ° λλ¬Έμ,
ROWNUM μ μ¬μ©νμ¬ νμ΄μ§ μ²λ¦¬λ₯Ό νκΈ° μν΄μλ μΈλΌμΈ λ·°λ₯Ό μ¨μΌνλ€.
ROWNUM λ₯Ό νμ©ν νμ΄μ§ μ²λ¦¬
SELECT *
FROM (
SELECT ROWNUM AS row_num, no, title, content
FROM board
WHERE ROWNUM <= 10 -- μνλ νμ΄μ§ ν¬κΈ°λ₯Ό μ¬κΈ°μ μ§μ ν©λλ€.
)
WHERE row_num >= 1; -- μνλ νμ΄μ§ λ²νΈμ λ§κ² μμνλ ν λ²νΈλ₯Ό μ¬κΈ°μ μ§μ ν©λλ€.
SQL
볡μ¬
board ν μ΄λΈ μμ± λ° μν λ°μ΄ν°
-- DDL
CREATE TABLE board (
no NUMBER NOT NULL,
title varchar2(100) NOT NULL,
writer varchar2(100) NOT NULL,
content varchar2(1000),
reg_date DATE DEFAULT sysdate NOT NULL,
upd_date DATE DEFAULT sysdate NOT NULL,
views NUMBER DEFAULT 0 NOT NULL,
PRIMARY KEY (no)
);
TRUNCATE board;
-- μν λ°μ΄ν°
INSERT INTO `board` (title, writer, content) VALUES
('μ λͺ© 1', 'μμ±μ 1', 'λ΄μ© 1'),
('μ λͺ© 2', 'μμ±μ 2', 'λ΄μ© 2'),
('μ λͺ© 3', 'μμ±μ 3', 'λ΄μ© 3'),
('μ λͺ© 4', 'μμ±μ 4', 'λ΄μ© 4'),
('μ λͺ© 5', 'μμ±μ 5', 'λ΄μ© 5'),
('μ λͺ© 6', 'μμ±μ 6', 'λ΄μ© 6'),
('μ λͺ© 7', 'μμ±μ 7', 'λ΄μ© 7'),
('μ λͺ© 8', 'μμ±μ 8', 'λ΄μ© 8'),
('μ λͺ© 9', 'μμ±μ 9', 'λ΄μ© 9'),
('μ λͺ© 10', 'μμ±μ 10', 'λ΄μ© 10'),
('μ λͺ© 11', 'μμ±μ 11', 'λ΄μ© 11'),
('μ λͺ© 12', 'μμ±μ 12', 'λ΄μ© 12'),
('μ λͺ© 13', 'μμ±μ 13', 'λ΄μ© 13'),
('μ λͺ© 14', 'μμ±μ 14', 'λ΄μ© 14'),
('μ λͺ© 15', 'μμ±μ 15', 'λ΄μ© 15'),
('μ λͺ© 16', 'μμ±μ 16', 'λ΄μ© 16'),
('μ λͺ© 17', 'μμ±μ 17', 'λ΄μ© 17'),
('μ λͺ© 18', 'μμ±μ 18', 'λ΄μ© 18'),
('μ λͺ© 19', 'μμ±μ 19', 'λ΄μ© 19'),
('μ λͺ© 20', 'μμ±μ 20', 'λ΄μ© 20'),
('μ λͺ© 21', 'μμ±μ 21', 'λ΄μ© 21'),
('μ λͺ© 22', 'μμ±μ 22', 'λ΄μ© 22'),
('μ λͺ© 23', 'μμ±μ 23', 'λ΄μ© 23'),
('μ λͺ© 24', 'μμ±μ 24', 'λ΄μ© 24'),
('μ λͺ© 25', 'μμ±μ 25', 'λ΄μ© 25'),
('μ λͺ© 26', 'μμ±μ 26', 'λ΄μ© 26'),
('μ λͺ© 27', 'μμ±μ 27', 'λ΄μ© 27'),
('μ λͺ© 28', 'μμ±μ 28', 'λ΄μ© 28'),
('μ λͺ© 29', 'μμ±μ 29', 'λ΄μ© 29'),
('μ λͺ© 30', 'μμ±μ 30', 'λ΄μ© 30'),
('μ λͺ© 31', 'μμ±μ 31', 'λ΄μ© 31'),
('μ λͺ© 32', 'μμ±μ 32', 'λ΄μ© 32'),
('μ λͺ© 33', 'μμ±μ 33', 'λ΄μ© 33'),
('μ λͺ© 34', 'μμ±μ 34', 'λ΄μ© 34'),
('μ λͺ© 35', 'μμ±μ 35', 'λ΄μ© 35'),
('μ λͺ© 36', 'μμ±μ 36', 'λ΄μ© 36'),
('μ λͺ© 37', 'μμ±μ 37', 'λ΄μ© 37'),
('μ λͺ© 38', 'μμ±μ 38', 'λ΄μ© 38'),
('μ λͺ© 39', 'μμ±μ 39', 'λ΄μ© 39'),
('μ λͺ© 40', 'μμ±μ 40', 'λ΄μ© 40'),
('μ λͺ© 41', 'μμ±μ 41', 'λ΄μ© 41'),
('μ λͺ© 42', 'μμ±μ 42', 'λ΄μ© 42'),
('μ λͺ© 43', 'μμ±μ 43', 'λ΄μ© 43'),
('μ λͺ© 44', 'μμ±μ 44', 'λ΄μ© 44'),
('μ λͺ© 45', 'μμ±μ 45', 'λ΄μ© 45'),
('μ λͺ© 46', 'μμ±μ 46', 'λ΄μ© 46'),
('μ λͺ© 47', 'μμ±μ 47', 'λ΄μ© 47'),
('μ λͺ© 48', 'μμ±μ 48', 'λ΄μ© 48'),
('μ λͺ© 49', 'μμ±μ 49', 'λ΄μ© 49'),
('μ λͺ© 50', 'μμ±μ 50', 'λ΄μ© 50');
SQL
볡μ¬
ROWID
κ° νμ κ³ μ νκ² μλ³νλλ° μ¬μ©λλ μλ³μ
κ΅¬μ± μμ | μ€λͺ
|
μ€λΈμ νΈ λ²νΈ | ROWIDκ° μν κ°μ²΄(ν
μ΄λΈ λλ ν΄λ¬μ€ν°)μ κ³ μ ν λ²νΈλ₯Ό λνλ
λλ€. |
μλ νμΌ λ²νΈ | νμ΄ μ μ₯λ μλμ μΈ λ°μ΄ν° νμΌ λ²νΈλ₯Ό λνλ
λλ€. |
λΈλ‘ λ²νΈ | νμ΄ μ μ₯λ λ°μ΄ν° λΈλ‘μ λ²νΈλ₯Ό λνλ
λλ€. |
λ°μ΄ν° λ²νΈ | λΈλ‘ λ΄μμμ νμ μλμ μΈ μμΉλ₯Ό λνλ
λλ€. |
β’
ROWID μμμ½λ
SELECT ROWID
, employee_id
, first_name
FROM employees
WHERE department_id = 30;
SQL
볡μ¬