SQL Tutorial – ORDER BY clause in SQL.

You have a table and you want to order the data in ascending or descending order. For example, you want to sort this table by population in ascending or descending order.

Solution –

To order the data in SQL, We use the ORDER BY clause, followed by column name and the way you want to order it.

Syntax –

ORDER BY column_name ASC|DESC

To sort the data in ascending order we use the ASC keyword and to sort the data in descending order we use the DESC keyword. By default SQL, order the data in ascending order, so if you want you can skip the ASC keyword.

SELECT *
FROM city
ORDER BY Population DESC;

You can also sort the data by multiple columns, please check the below posts related to sorting.

  1. Sort data by Multiple Columns in SQL.
  2. Sort data based on conditions in ORDER BY clause.
  3. Order NULL Values First or Last in SQL.

Rating: 1 out of 5.

Leave a Reply