SQL Server TOP

Source: Internet
Author: User
I believe that many SQL statements have been used by many people. The usage of TOP statements is: SELECTTOP (n) FROMtablenameWHEREcondition. We can easily go from the tablename table to the first n data entries that meet the conditions. Bytes --------------------------------------------------------------------------------------

I believe that many SQL statements have been used by many people. The use of the TOP statement is: SELECT TOP (n) FROM tablename WHERE condition. We can easily go FROM the tablename table to the first n pieces of data that meet the conditions. Bytes --------------------------------------------------------------------------------------

I believe many SQL statements have been used by many people.

The usage is as follows:

Select top (n)

FROM tablename

WHERE condition

We can easily go from the tablename table to the first n data entries that meet the conditions.

Bytes --------------------------------------------------------------------------------------

For example, we have tables

Aaa

The data in the table is:

Qq qq2

A1 11

A2 11

A3 11

A4 11

Run:

Select top (2) qq, qq2
FROMaaa

The result is:

Qqqq2

A1 11

A2 11

This is exactly what we want. It is really good and convenient to use.

Bytes --------------------------------------------------------------------------------------

However, today, a program runs out and finds that it is not what you want.

This is the case,

The data is still the data above. I added an order by statement for the Search Condition above the search condition. Of course, this orderby is a bit special, that is, the values of all columns to be sorted are the same.

If the TOP condition is normal

SELECT qq, qq2
FROMaaa
ORDER BYqq2

The result is the same as the order in which no orderby is stored in the database.

Qq qq2

A1 11

A2 11

A3 11

A4 11

When we add a TOP (1) instance, normally, Hong Kong virtual hosts and virtual hosts should be retrieved from the first server in the United States, that is, [a1] [11 ].

Select top (1) qq, qq2
FROMaaa
ORDER BYqq2

However, the data actually retrieved is as follows:

Qqqq2

A2 11

At this time, I feel that SQL Server is a bit different from our common sense.

Similarly, when we change to TOP (2), the third and second pieces are retrieved.

In TOP (3), the fourth, third, and second pieces are retrieved.

SQL Server may not be able to do this because the values of the sorted fields are the same and equivalent data can be considered. The returned data has no impact on the business.

* In programming, we may occasionally encounter unexpected things and surprise us.

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.