SQL ORDER BY statement

Source: Internet
Author: User

The order BY statement is used to sort the result set.

ORDER by statement

The order BY statement is used to sort the result set based on the specified column.

The order BY statement sorts records by default in ascending order (ASC) .

If you want to sort records in descending order , you can use the DESC keyword.

The original table (used in the example):

Orders table:

Company
OrderNumber
Ibm 3532
W3school 2356
Apple 4698
W3school 6953
Example 1

Show company name in alphabetical order:

ORDER BY Company
Results: Company
OrderNumber
Apple 4698
Ibm 3532
W3school 6953
W3school 2356
Example 2

Displays the company name in alphabetical order and displays the order number (OrderNumber) in numerical order:

ORDER BY Company, OrderNumber

Results:

Company
OrderNumber
Apple 4698
Ibm 3532
W3school 2356
W3school 6953
Example 3

Show company name in reverse alphabetical order:

ORDER BY Company DESC
Results: Company
OrderNumber
W3school 6953
W3school 2356
Ibm 3532
Apple 4698
Example 4

Displays the company name in reverse alphabetical order and displays the sequential number in numerical order:

ORDER BY Company DESC, OrderNumber ASC
Results: Company
OrderNumber
W3school 2356
W3school 6953
Ibm 3532
Apple 4698

Note : There are two equal company names (W3school) in the results above. Only this time, when there is the same value in the first column, the second column is in ascending order. This is the case if some of the values in the first column are nulls .

SQL ORDER BY statement

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.