SQL ORDER BY Statement usage

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 columns.

The order BY statement sorts the records by default in ascending sequence.

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

The original table (in the example):

Orders table:

Company
OrderNumber
Ibm 3532
W3school 2356
Apple 4698
W3school 6953

Instance 1

Display company names in alphabetical order:

ORDER BY Company

Results:

Company
OrderNumber
Apple 4698
Ibm 3532
W3school 6953
W3school 2356

Instance 2

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

ORDER BY Company, OrderNumber

Results:

Company
OrderNumber
Apple 4698
Ibm 3532
W3school 2356
W3school 6953

Instance 3

Show company name in reverse alphabetical order:

ORDER BY Company DESC

Results:

Company
OrderNumber
W3school 6953
W3school 2356
Ibm 3532
Apple 4698

Instance 4

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

ORDER BY Company DESC, OrderNumber ASC

Results:

company ordernumber
W3sch Ool 2356
w3school 6953
IBM 3532
Apple 4698

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.