How do you use the ORDER BY clause to sort query results in ascending and descending order?
How do you use the ORDER BY clause to sort query results in ascending and descending order?
419
30-Jun-2023
Updated on 02-Jul-2023
Aryan Kumar
02-Jul-2023The ORDER BY clause in SQL is used to sort the results of a SELECT statement. By default, the ORDER BY clause sorts the results in ascending order. To sort the results in descending order, you can use the DESC keyword.
For example, the following SELECT statement will return all of the rows from the
customerstable, sorted by thecustomer_namecolumn in ascending order:SQL
To sort the results in descending order, you can use the following SELECT statement:
SQL
You can also sort the results by multiple columns. To do this, you simply specify the names of the columns, separated by commas, in the ORDER BY clause. For example, the following SELECT statement will return all of the rows from the
customerstable, sorted by thecustomer_namecolumn in ascending order, and then by thecustomer_idcolumn in ascending order:SQL
You can also specify the direction of sorting for each column. To do this, you can use the ASC keyword for ascending order or the DESC keyword for descending order. For example, the following SELECT statement will return all of the rows from the
customerstable, sorted by thecustomer_namecolumn in descending order, and then by thecustomer_idcolumn in ascending order:SQL