Oracle rownum + order by

Written on 22 August 2014, 09:39am

Tagged with: ,

Don’t do:


SELECT first_name, last_name
FROM employees 
WHERE  rownum = 1
ORDER BY creation_date DESC

You probably want to do this instead:


SELECT * from (
 SELECT first_name, last_name
 FROM employees 
 ORDER BY creation_date DESC
) where rownum = 1 

The first query takes the first row returned by Oracle and orders it by creation_date.
The second query gets the first row of the sub-query that contains the sorted records.
More on Stack Overflow

Versailles

Leave a response