Use rownum in Oracle

Source: Internet
Author: User

In Oracle, rownum is the sequence number generated when data is retrieved. Therefore, you must pay attention to the specified rowmun rows of data to be sorted.

 

SQL> select rownum, id, name from student order by name;

 

ROWNUM ID NAME

 

3 200003 Li San

 

2 200002 Wang 'er

 

1 200001 Zhang Yi

 

4 200004 Zhao Si

 

It can be seen that rownum is not the serial number generated by the name column. The system assigns the number of the record row according to the sequence in which the record is inserted, and the rowid is also allocated sequentially. To solve this problem, you must use the subquery

 

SQL> select rownum, id, name from (select * from student order by name );

 

ROWNUM ID NAME

 

1 200003 Li San

 

2 200002 Wang 'er

 

3 200001 Zhang Yi

 

4 200004 Zhao Si

Related Article

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.