0

I have a category table and need to do pagination for the same. I am using JSF, and query in XML. Is there any way to optimize this query better.

SELECT
    *
FROM
    (
        SELECT
            categories.*,
            ROWNUM rn
        FROM
            (
                SELECT
                    *
                FROM
                    (
                        SELECT
                            *
                        FROM
                           categories
                        WHERE
                          name LIKE 'FAMILY%'
                        ORDER BY
                            id
                    )
                WHERE
                    ROWNUM < 21
            ) categories
    )
WHERE
    rn > 10;
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
jef
  • 23
  • 5

2 Answers2

2

On the database version 12 an newer there are offset and fetch features available, so your query might be looking like the following

SELECT *
  FROM categories
 WHERE name LIKE 'FAMILY%'
 ORDER BY id
OFFSET 10 rows -- skips first 10 rows of the result
 FETCH 11 rows only;

Try that one please

The DB version can be checked using the following query

select * from v$version;
ekochergin
  • 4,109
  • 2
  • 12
  • 19
-1

Are you looking for a query like this?

SELECT * FROM (
SELECT ROW_NUMBER() OVER (ORDER BY id) ROWNUM,* FROM categories
WHERE name LIKE 'FAMILY%') as categories where ROWNUM between 10 and 21
Sreepu
  • 123
  • 1
  • 1
  • 14
  • this worked for me but between 1 to 10+ only data is populated, if i start from between 2 to 20 empty data is populated. Is there any issue with ROWNUM? – jef Feb 06 '20 at 08:23
  • This query won't compile in Oracle for several reasons: `ROWNUM` is a pseudo-column and you cannot use it as an alias; using `AS` before a table/sub-query alias is syntactically invalid; and you need to prefix the `*` with the table name/alias when you are also selecting other columns. – MT0 Feb 06 '20 at 08:26