SQL Order By
The SQL ORDER BY Keyword
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.
ORDER BY Syntax
Demo Database
Below is a selection from the "Consumer" table :
Examples
The following SQL statement selects all customers from the "Customers" table, sorted by the "Country" column:
ORDER BY DESC Example
The following SQL statement selects all customers from the "Customers" table, sorted DESCENDING by the "Country" column
ORDER BY Several Columns Example
The following SQL statement selects all customers from the "Customers" table, sorted by the "Country" and the "Customer Name" column:
ORDER BY Several Columns Example 2
The following SQL statement selects all customers from the "Customers" table, sorted ascending by the "Country" and descending by the "Customer Name" column:
Exercises :
Table - Customer
Write a query to display the orders according to the order number arranged by ascending order.
Write a SQL statement to display the customer name, city, and grade, etc. and the display will be arranged according to the smallest customer ID.
Write a SQL statement to make a report with salesman ID, order date and highest purchase amount in such an arrangement that, the smallest salesman ID will come first along with their smallest order date.
Write a SQL statement to display customer name, city and grade in such a manner that, the customer holding highest grade will come first.
Write a SQL statement to arrange the orders according to the order date in such a manner that the latest date will come first then previous dates.
Last updated