* MYSQL에서는 LIMIT이지만, 오라클에서는 ROWNUM
SELECT ROWNUM, E.*
FROM EMP E
ORDER BY E.SAL DESC; -- CTRL + ENTER : 쿼리실행
SELECT ROWNUM, T.*
FROM (SELECT *
FROM EMP
ORDER BY SAL DESC) T;
SELECT *
FROM (SELECT ROWNUM AS RNUM, T.*
FROM (SELECT *
FROM EMP
ORDER BY SAL DESC) T)
WHERE RNUM >=5 AND RNUM <=7;
SELECT *
FROM (SELECT ROWNUM AS RNUM, E.*
FROM (SELECT *
FROM EMP
WHERE JOB NOT IN ('SALESMAN', 'PRESIDENT')
ORDER BY SAL DESC) E)
WHERE RNUM BETWEEN 1 AND 5;
----
*SELECT 의 FROM 절에서 내부적으로 ROWNUM을 가지게 됨.
SELECT ROWNUM, E.*
FROM (SELECT * FROM EMP ORDER BY ENAME ASC) E;
*FROM절에 ROWNUM 속성이 없으므로 WHERE절에서 ROWNUM으로 조건검색을 할 수 없다.
SELECT ROWNUM, E.*
FROM (SELECT * FROM EMP ORDER BY ENAME ASC) E
WHERE ROWNUM BETWEEN 3 AND 6;
FROM (SELECT * FROM EMP ORDER BY ENAME ASC) E
WHERE ROWNUM BETWEEN 3 AND 6;
*이제는 FROM 절에 RNUM이 생겼으므로 WHERE절에서 조건으로 RNUM 사용 가능. ROWNUM은 예약어이므로 AS 로 리네임.
SELECT *
FROM (
SELECT ROWNUM AS RNUM, E.*
FROM (SELECT * FROM EMP ORDER BY ENAME ASC) E
)
WHERE RNUM BETWEEN 3 AND 6;
FROM (
SELECT ROWNUM AS RNUM, E.*
FROM (SELECT * FROM EMP ORDER BY ENAME ASC) E
)
WHERE RNUM BETWEEN 3 AND 6;
'IT > Oracle' 카테고리의 다른 글
오라클 제공 테스트 테이블 DUAL (0) | 2018.07.23 |
---|---|
오라클 계층형 쿼리와 ROWNUM (0) | 2018.07.23 |
오라클 셋팅방법 및 명령어 (0) | 2018.07.23 |
오라클 PL/SQL insert, update, delete, merge 예제 (0) | 2018.07.19 |
오라클 PL/SQL (0) | 2018.07.19 |