I have the following SQL query:
select
ID, COLUMN1, COLUMN2
from
(select ID, COLUMN1, COLUMN2, row_number() over (order by 2 DESC) NO from A_TABLE)
where
NO between 0 and 100
What I am trying to do is to select the first 100 records of the query
select ID, COLUMN1, COLUMN2 from ATABLE order by 2 DESC
And here are the problems:
Apparently, the
order by
clause is not working. I've noticed that I have to add anotherorder by 2 DESC
clause, just after(...) from ATABLE
, for my query to work. Is there something I do wrong? Or is it expected behaviour?How can I add a
where
clause? Let's say I need to select only the first 100 records of the tablewhere COLUMN1 like '%value%'
. I've tried adding the where clause after(...) from ATABLE
but it produced an error...
Help? Thanks.
PS: I'm using Oracle 10g R2.
Here you will get the limited record from the oracle database without the usage of
rownum
Why don't you use
rownum is a pseudo column that counts rows in the result set after the where clause has been applied.
Is this what you're trying to get?
Because it's a pseudo column that is strictly a counter of rows resulting from the where clause it will not allow you to do pagination (i.e. between 200 & 300).
This is probably what you're looking for:
Check out this Oracle FAQ. In particular this part:
To answer your first question: Don't use a column number in your order by clause, but use the column name. I don't fully understand your second question, because adding a WHERE in your most inner SELECT should do the trick:
P.S. (to willcodejavaforfood) I think using row_number() is better when you want the rows to be ordered. It saves an inner view (big win for readability).