Precedence comparison of rownum and order by in Oracle

Source: Internet
Author: User

1. Preface

  The company recently MySQL database switch oracle,oracle for a long time useless a little strange, just started to do the paging on the rownum and order by priority comparison. Check the information, the Internet said rownum priority is higher than the order by, that is, the results of the first fetch data, and then the results are sorted. With a skeptical attitude to write a two-day SQL statement, the result is ROWNUM priority is higher than the orderby, some of the order by priority is higher than ROWNUM. It was later concluded that the priority comparison of RowNum and order by was to be divided.

2. The subject

2, 1 order by index, primary key, Span style= "FONT-SIZE:18PT;" >

Select  from where <= Ten Order  by desc ;
Select from (Select from Test Order by desc where rownum<=;

2, 2 order by ordinary column, where amount is the normal column, the execution results are as follows, the results are not the same.  

1、SelectAmount fromTestwhereRowNum<=Ten Order  byAmountdesc;
2、SelectB.amount from(SelectA.amount fromTest AOrder byA.amountdesc) bwhereRowNum<=Ten;
  

3. Results

  The priority comparison of the rownum and order by is a case in point,when the order by index, the primary key, allows Oracle to sort by the field first, then the RowNum label, and the Order by normal column, rownum the after order by.

Precedence comparison of rownum and order by in Oracle

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.