简体   繁体   中英

Oracle 11g and SQL TOP query

While using SELECT TOP 5 * FROM SOMETABLE gives me an error

ORA-00923: FROM keyword not found where expected

I am using Oracle 11g . I am aware of using rownum for doing the same thing but just wondering SQL TOP usage is not at all supported in Oracle ? Anything need to do extra to make SQL TOP working in Oracle ??

Oracle does not support TOP . Use ROWNUM

SELECT * FROM your_table
WHERE ROWNUM <= 5

SQLFiddle example

No, Oracle does not support TOP .

As you point out, the best approach is to use rownum . Another option is the analytical function ROW_NUMBER .

The rownum keyword, while it gets you the said no. of records, does so only after applying the order by clause if you have one.

So if the SQL server query is as below, it will give you 10 most recently created records.

Select TOP 10 * from mytable order by created_date desc

But to fit Oracle, when you write this, it gets you the 10 records (that may not be the most recent ones) and arranges them in descending order, which is not what you wanted.

Select * from mytable where rownum < 10 order by created_date desc

So writing with an additional select like this would help:

SELECT * FROM (Select * from mytable order by created_date desc) where rownum < 10 

SQL TOP不适用于Oracle。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM