The ORDERBY statement is used to sort the result set. The ORDERBY statement is used to sort the result set based on the specified column. By default, the ORDERBY statement sorts records in ascending order. If you want to sort records in descending order, you can use the DESC keyword. Original table (used in the example): Orders table: Company
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.
BY default, the order by statement sorts records in ascending ORDER.
If you want to sort records in descending order, you can use the DESC keyword.
Original table (used in the example ):
Orders table:
Company |
OrderNumber |
IBM |
3532 |
W3School |
2356 |
Apple |
4698 |
W3School |
6953 |
Instance 1
Display company names in alphabetical order:
SELECT Company, OrderNumber FROM Orders ORDER BY Company
Result:
Company |
OrderNumber |
Apple |
4698 |
IBM |
3532 |
W3School |
6953 |
W3School |
2356 |
Instance 2
Company is displayed in alphabetical order and OrderNumber is displayed in numerical order ):
SELECT Company, OrderNumber FROM Orders ORDER BY Company, OrderNumber
Result:
Company |
OrderNumber |
Apple |
4698 |
IBM |
3532 |
W3School |
2356 |
W3School |
6953 |
Instance 3
Display company names in alphabetical order:
SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC
Result:
Company |
OrderNumber |
W3School |
6953 |
W3School |
2356 |
IBM |
3532 |
Apple |
4698 |
Instance 4
Display company names in alphabetical order and sequence numbers:
SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC, OrderNumber ASC
Result:
Company |
OrderNumber |
W3School |
2356 |
W3School |
6953 |
IBM |
3532 |
Apple |
4698 |