How to ORDER BY Multiple Columns in SQL?

Learn via video course
FREE
View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Topics Covered

SQL (Structured Query Language) is used to interact with the database. Standard SQL commands include 'select', 'delete', 'alter', etc. In SQL, we use the ORDER BY keyword to specify the order in which rows appear in the result set. We can sort the rows in the appropriate order using the ORDER BY clause. The ORDER BY clause is used to order the returned records. We can arrange the results in ascending or descending order by using the ORDER BY clause. This clause can also be used with multiple columns.

SQL Server ORDER BY Clause

The order of rows in the result set is not guaranteed when using the SELECT statement to query data from a table. It indicates that SQL Server can return a result set with rows in any order.

The ORDER BY clause is the only way to ensure that the rows in the result set are sorted. The ORDER BY clause is used to sort the result set in ascending or descending order. By default, the ORDER BY keyword sorts the records in ascending order.

Syntax

ParameterDescription
column_name | expressionIt is a column name or expression for sorting the result set of the query.  If you give multiple columns, the result set is sorted first by the first column, then by the second column, and so on. Columns in the ORDER BY clause must correspond to either a select list column or columns defined in the table specified in the FROM clause.
ASC | DESCThe letters ASC or DESC indicate whether the values in the specified column should be ordered ascending or descending. The ASC sort order is from lowest to the highest value, while the DESC sort order is from highest to lowest. SQL Server uses ASC as the default sort order unless you explicitly specify DESC. In addition, SQL Server considers NULL to be the lowest value.

Steps to ORDER BY Multiple Columns in SQL

For applying for the ORDER BY clause to multiple column order, add the name of the column by which you want to order records first. The column that is entered in the first place will get sorted likewise.

Step - 1 : Create a Simple Database and Table

First, you must create a new SQL database.

The Hospital Database is created using the following query :

Step - 2 : Insert the Data into the Table

Now, you have to insert the data into the table. So, that ORDER BY clause can be applied to the inserted data.

The following query inserts a record of admitted patients into the Patient table of the Hospital database :

Step - 3 : View the Inserted Data of the Table without Using the ORDER BY Clause

The following query returns an unsorted list of Patients :

The following is the output of the following SELECT query :

Patient_IDPatient_NamePatient_DiseasePatient_AgePatient_City
1035JaiMalaria25Goa
1015MohitDiabetes44Mumbai
1003HariyashFever29Kochi
1044EktaCancer40Hyderabad
1025MohanCorona52Kolkata

Step - 4 : Use the ORDER BY Clause

The following query uses the ORDER BY clause to display all patient records by Patient_ID in ascending order :

The following is the output of the following SELECT query with ORDER BY clause :

Patient_IDPatient_NamePatient_DiseasePatient_AgePatient_City
1003HariyashFever29Kochi
1015MohitDiabetes44Mumbai
1025MohanCorona52Kolkata
1035JaiMalaria25Goa
1044EktaCancer40Hyderabad

This query first sorts patient records by Patient_Name, then if Patient_Name appears more than once, it sorts those rows by Patient_Age.

Patient_IDPatient_NamePatient_Age
1044Ekta40
1003Hariyash29
1035Jai25
1025Mohan52
1015Mohit44

Examples

We will use the customer's table in the sample database from the demonstration.

customers_sales
customer_id
First_Name
Last_Name
phone
email
street
city
state
zip_code

Example - 1 :

In this example, we have not specified ASC or DESC in the ORDER BY clause and used ASC as default.

The customer list is sorted by first name in ascending order using the following query :

First_NameLast_Name
AaronKnapp
AbbeyPugh
AbbyGamble
AbramCopeland
AdamHenderson
AdamThomton
AddieHahn

Example - 2 :

In this example, the ORDER BY clause sorted the result set according to values in the First_Name column in decreasing order because the DESC was explicitly specified.

The customer list is sorted by first name in descending order using the following query :

First_NameLast_Name
ZulemaBrowning
ZulemaClemons
ZoraidaPatton
ZoraFord
ZonaCameron
ZinaBonner
ZeniaBruce
ZelmaBrowning

Example - 3 :

In this example, the ORDER BY clause sorted the customer list by the city first, then by the first name.

The following statement retrieves the customers first_name, last_name, and city. It initially sorts the customer list by city first, then by first name.

cityFirst_NameLast_Name
AlbanyDouglassBlankenship
AlbanyMiGray
AlbanyPriscillaWilkins
AmarilloAndriaRivers
AmarilloDelaineEstes
AmarilloJonellRivas
AmarilloLuisTyler
AmarilloNarcisaKnapp

Example - 4 :

In this example, the ORDER BY clause sorts the city in descending order and then sorts the sorted result in ascending order by the first name.

The following statement sorts the customers in descending order by city and then sorts the sorted result set in ascending order by the first name.

cityFirst_NameLast_Name
Yuba CityLouanneMartin
Yorktown HeightsDemarcusReese
Yorktown HeightsJennaSaunders
Yorktown HeightsLatriciaLindsey
Yorktown HeightsShastaCombs
Yorktown HeightsShaunaEdwards
YonkersAaronKnapp
YonkersAlaneMunoz

Learn More

Conclusion

  • With the ORDER BY clause, you can select records from a database while also requesting that they be sorted by two columns. This clause is always added at the end of the SQL query.
  • Add the name of the column by which you want to arrange records after the ORDER BY keyword. The second column is then added followed by a comma. You can change the sorting order (ascending or descending) for each column separately.
  • You can use the ASC keyword to use ascending (low to high) order, however, this keyword is optional because it is the default order when nothing is specified. Put the DESC keyword after the appropriate column if you want to sort the data in descending order.