In this tutorial, i’ll show you How toorder by 2 columns in Sql.
You need to display records from a given table sorted by two columns.
List all products sorted by Category ID (descending), then by UnitPrice:
If you want to select records from a table but would like to see them sorted according to two columns, you can do so with
ORDER BY. This clause comes at the end of your SQL query.
ORDER BY keyword, add the name of the column by which you’d like to sort records first (in our example, salary). Then, after a comma, add the second column (in our example,
last_name). You can modify the sorting order (ascending or descending) separately for each column. If you want to use ascending (low to high) order, you can use the
ASC keyword; this keyword is optional, though, as that is the default order when none is specified. If you want to use descending order, put the
DESC keyword after the appropriate column (in the example, we used descending order for the
In our example, we first sorted the result by salary in descending order (higher salaries to lower ones) and then by last name in ascending order within those already sorted records.
Leave a Comment