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
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
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:
SELECT * FROM Customers
ORDER BY Country;
ORDER BY DESC Example
The following SQL statement selects all customers from the "Customers" table, sorted DESCENDING by the "Country" column
SELECT * FROM Customers
ORDER BY Country;
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:
SELECT * FROM Customers
ORDER BY Country, Customer Name;
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:
SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;
Exercises :
Table - Customer
customer_id cust_name city grade salesman_id
----------- ------------ ---------- ---------- -----------
3002 Nick Rimando New York 100 5001
3005 Graham Zusi California 200 5002
3001 Brad Guzan London 5005
3004 Fabian Johns Paris 300 5006
3007 Brad Davis New York 200 5001
3009 Geoff Camero Berlin 100 5003
3008 Julian Green London 300 5002
3003 Jozy Altidor Moscow 200 5007
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
Was this helpful?