The SQL ORDER BY keyword is used to sort the result-set in ascending or descending order.
ORDER BY
The ORDER BY keyword is used to sorts the records by default it sorts in ascending order.
DESC
ASC
SELECT column_1, column_2 FROM table_name ORDER BY column_1, column_2 ASC | DESC;
ORDER BY example SQL statement to selects all customers from the “Customers” table, sorted by the “id” column:
SELECT * FROM customers ORDER BY id;
ORDER BY DESC example SQL statement to selects all customers from the “Customers” table, sorted DESCENDING by the “id” column:
SELECT * FROM customers ORDER BY id DESC;
ORDER BY multiple Columns SQL statement selects all customers from the “Customers” table, sorted by the “Country” and the “id” column.
SELECT * FROM customers ORDER BY country, id;
country
id
ORDER BY individual Columns SQL statement selects all customers from the “Customers” table, sorted ascending by the “Country” and descending by the “city” column:
SELECT * FROM customers ORDER BY country ASC, city DESC;