Select Row X to Y in Oracle


the fastest query,

SELECT *
FROM (SELECT A.*, ROWNUM RN
FROM (SELECT *
FROM HR.EMPLOYEES
ORDER BY EMPLOYEE_ID) A
WHERE ROWNUM <= :Y)
WHERE RN >= :X;

another query,
1.SELECT *
FROM (SELECT EMP.*, ROWNUM RN
FROM HR.EMPLOYEES EMP
WHERE ROWNUM < :Y + 1
ORDER BY EMPLOYEE_ID)
WHERE RN BETWEEN :X AND :Y;

2.SELECT *
FROM HR.EMPLOYEES
WHERE EMPLOYEE_ID IN (SELECT EMPLOYEE_ID
FROM HR.EMPLOYEES
GROUP BY ROWNUM, EMPLOYEE_ID
HAVING ROWNUM BETWEEN :X AND :Y)
ORDER BY EMPLOYEE_ID;

3.SELECT *
FROM HR.EMPLOYEES
WHERE ROWID IN (SELECT ROWID
FROM (SELECT * FROM HR.EMPLOYEES ORDER BY EMPLOYEE_ID)
WHERE ROWNUM <= :Y
MINUS
SELECT ROWID
FROM (SELECT * FROM HR.EMPLOYEES ORDER BY EMPLOYEE_ID)
WHERE ROWNUM < :X)

One Response

  1. Thanks a lot for these useful instruction tips.It was very helpful for me!!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: