ORACLE first order by then ROWNUM

Source: Internet
Author: User

Java code
Select t. *, t. rowid, rownum from test1 t

Name KM CJ ROWID ROWNUM

Zhang San Language 80 AAAHhOAALAAATSIAAA 1
James math 86 AAAHhOAALAAATSIAAB 2
James 75 AAAHhOAALAAATSIAAC 3
Li Si language 79 AAAHhOAALAAATSIAAD 4
Li Si math 85 AAAHhOAALAAATSIAAE 5
Li Si English 78 AAAHhOAALAAATSIAAF 6

Java code
Select t. *, t. rowid, rownum from test1 t where rownum <3 order by CJ


Zhang San Language 80 AAAHhOAALAAATSIAAA 1
James math 86 AAAHhOAALAAATSIAAB 2

The nested method can be used successfully.
Java code
Select t. *, t. rowid, rownum from (select * from test1 t order by CJ) t where rownum <3

James 75 AAAHhOAALAAATSIAAF 1
Li Si English 78 AAAHhOAALAAATSIAAF 2

However, nesting affects efficiency. What if it is not nested?
If you do not define a primary key or a field that can uniquely identify a record, rowid is used to uniquely identify the record,
Rowid will not change, but if your table changes the tablespace, rowid may also change.

However, if you set the CJ field as the primary key, run
Java code
Select t. *, t. rowid, rownum from test1 t where rownum <3 order by CJ

James 75 AAAHhOAALAAATSIAAC 1
Li Si English 78 AAAHhOAALAAATSIAAF 2

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.