The order by keyword is used for sorting results
---------------------------------
Sort the rows
The order by clause is used to sort rows.
Orders:
Company |
Ordernumber |
Sega |
3412 |
ABC shop |
5678 |
W3schools |
2312 |
W3schools |
6798 |
Example
Sort companies alphabetically:
SELECT Company, OrderNumber FROM Orders ORDER BY Company |
Returned results:
Company |
Ordernumber |
ABC shop |
5678 |
Sega |
3412 |
W3schools |
6798 |
W3schools |
2312 |
Example
Sort by letters and numbers to companies:
SELECT Company, OrderNumber FROM Orders ORDER BY Company, OrderNumber |
Returned results:
Company |
Ordernumber |
ABC shop |
5678 |
Sega |
3412 |
W3schools |
2312 |
W3schools |
6798 |
Example
Sort companies in alphabetical order
SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC |
Returned results:
Company |
Ordernumber |
W3schools |
6798 |
W3schools |
2312 |
Sega |
3412 |
ABC shop |
5678 |
Example
Sort companies in reverse order of letters and numbers
SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC, OrderNumber ASC |
Returned results
Company |
Ordernumber |
W3schools |
2312 |
W3schools |
6798 |
Sega |
3412 |
ABC shop |
5678 |