Help the world stop coronavirus! Stay home!

Prev Next

Hibernate / SQL

How the ROWNUM to be used together with the ORDER BY clause in SQL?

If an ORDER BY clause follows ROWNUM in the same query, then the rows will be reordered by the ORDER BY clause. The results can vary depending on the way the rows are accessed.

SELECT * FROM employees WHERE ROWNUM < 11 ORDER BY dept;

If you embed the ORDER BY clause in a subquery and place the ROWNUM condition in the top-level query, then you can force the ROWNUM condition to be applied after the ordering of the rows. For example, the following query returns the employees with the 10 smallest employee numbers. This is sometimes referred to as top-N reporting:

SELECT * FROM
   (SELECT * FROM employees ORDER BY dept)
   WHERE ROWNUM < 11;

❤Cash Back At Stores you Love !!!❤

Earn your $10 reward when you make your first purchase through Ebates by signing up with clicking below button.

Ebates Coupons and Cash Back

More Related questions...

Show more question and Answers...

Maven

Comments & Discussions