Differences and exercises between RowNum and Row_number () in the Oracle database

Source: Internet
Author: User

Specifically for rownum It is the number of the Oracle system order assigned to the rows returned from the query, the first row returned is assigned 1, the second row is two, and so on, this is a field that can be used to limit the total number of rows returned by the query, since rownum always starts with 1. But more than 1 of the natural number in rownum do equal to the judgment is considered false condition, so can not find Rownum=n (n "1 natural number), so find the second line after the record can be used to solve the sub-query method, to sub-query rownum alias; The two methods are similar to those that are less than a certain value, but for a value that is not equal to one or a value to a value, it is easier to rank with row_number () aliases than with rownum pseudo-columns, because pseudo-columns are always searched from the beginning;
See the following code for specific usage and differences;

--Take out the top 5 highest paid

Select Empno,ename,sal,rownum from EMP;

SELECT * FROM (SELECT * from emp ORDER BY sal Desc) where rownum<=5;

SELECT * FROM (select Ename,sal,row_number () over (order by Sal Desc) as num from emp) where num<=5;

SELECT * FROM (select Ename,sal,row_number () over (order by Sal Desc) from EMP) where rownum<=5

--The top 3 wages
SELECT * from emp where Sal >=any (SELECT * FROM (select Sal to emp order by sal Desc) where rownum<=3);

SELECT * FROM (SELECT * from emp ORDER BY sal Desc) where RowNum <4;

SELECT * FROM (select Ename,sal,empno,deptno, Row_number () Up (order by Sal Desc) from EMP) where rownum<4;

SELECT * FROM (select Ename,sal,empno,deptno, Row_number () Up (order by Sal Desc) as num from emp) where num<4


--Sort by salary, take 6th place to 10th place
--using pseudo-columns to get
SELECT * FROM (select Ename,sal,rownum r from (SELECT * to emp ORDER BY sal Desc) where rownum<=10) where r>5;

--Using the rank function to get
SELECT * FROM (select Ename,sal,row_number () over (order by Sal Desc) as num from EMP) where num>5 and num<=10;


-------employees who get the fourth wage rank by salary from high to low

SELECT * FROM (select Ename,sal,row_number () over (order by Sal Desc) as num from emp) where num=4;

SELECT * FROM (select Ename,sal,rownum r from (SELECT * to emp ORDER BY sal Desc) where rownum<=4) where r=4;

"Enterprise Framework Source code" SPRINGMVC MyBatis or Hibernate ehcache level two cache maven non-and MAVEN version "WebSocket Instant Messaging" get ""

Differences and exercises between RowNum and Row_number () in the Oracle database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.