SQL ORDER BY clause

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 according to the specified column.
The ORDER BY statement sorts records in ascending order by default.
If you want to sort records in descending order, you can use the DESC keyword.
Original table (used in example):
Orders table:
Company OrderNumber
IBM 3532
W3School 2356
Apple 4698
W3School 6953
Example 1
shows company names in alphabetical order:
SELECT Company, OrderNumber FROM Orders ORDER BY Company
Result:
Company OrderNumber
Apple 4698
IBM 3532
W3School 6953
W3School 2356
Example 2
Displays the company name (Company) in alphabetical order and the order number (OrderNumber) in numerical order:
SELECT Company, OrderNumber FROM Orders ORDER BY Company, OrderNumber
Result:
Company OrderNumber
Apple 4698
IBM 3532
W3School 2356
W3School 6953
Example 3
Displays company names in reverse alphabetical order:
SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC
Result:
Company OrderNumber
W3School 6953
W3School 2356
IBM 3532
Apple 4698
Example 4
Displays companies in reverse alphabetical order Name, and display the order number in numerical order:
SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC, OrderNumber ASC
Result:
Company OrderNumber
W3School 2356
W3School 6953
IBM 3532
Apple 4698
Note: There are two equal company names in the above result ( W3School). Only this time, when the first column has the same value, the second column is in ascending order. This is also the case if some of the values ​​in the first column are nulls.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326564012&siteId=291194637