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.
To order the data in SQL, We use the ORDER BY clause, followed by column name and the way you want to order it.
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.
Related Posts –
- Sort data by Multiple Columns in SQL.
- Sort data based on conditions in ORDER BY clause.
- Order NULL Values First or Last in SQL.