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 |