Order By Two Columns in SQL with Examples – Programming, Pseudocode Example, C# Programming Example
SQL

Order By Two Columns in SQL with Examples

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.

Example:

List all products sorted by Category ID (descending), then by UnitPrice:

Discussion:

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.

After the 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 salary column).

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: