SQL - Multiple Column Ordering
Last Updated :
23 Jul, 2025
SQL is the standard language used for managing and manipulating databases. One of its powerful features is the ability to sort data using the ORDER BY clause, allowing us to arrange the query results in a meaningful way. By default, SQL sorts data in ascending order, but we can customize it to sort in descending order as well.
In this article, we will explain how to use the ORDER BY clause to sort data by one or more columns, demonstrate how to sort in both ascending and descending order, and explore techniques for sorting with multiple criteria to enhance query results.
ORDER BY Clause in SQL
The ORDER BY clause is used to sort the result set of a query in ascending (ASC) or descending (DESC) order. The syntax for ordering data is simple and straightforward. It sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.
Syntax:
SELECT * FROM table_name ORDER BY column_name;
Key Terms
- ASC (Ascending): This is the default sorting order, sorting data from the smallest to the largest value (e.g., 1 to 10 or A to Z).
- DESC (Descending): This sorts the data from the largest to the smallest value (e.g., 10 to 1 or Z to A).
Sorting by Multiple Columns
When sorting by multiple columns, the records are ordered first by the first column, then by the second, and so on. The first column listed in the ORDER BY clause will determine the primary sorting, and subsequent columns will determine the secondary and further sorting.
Syntax:
SELECT * FROM table_name
ORDER BY column1 ASC/DESC, column2 ASC/DESC, ...;
We will use the geeksforgeeks table in our geeks database to demonstrate the usage of the Multiple Column Ordering. This will help illustrate how to sort data based on more than one criterion, allowing us to achieve more refined and meaningful query results.
Query:
CREATE TABLE geeksforgeeks(
FIRSTNAME VARCHAR(20), LASTNAME VARCHAR(20),
CITY VARCHAR(20), AGE INT, GENDER VARCHAR(20));
INSERT INTO geeksforgeeks VALUES
('ROMY', 'Kumari', 'New Delhi', 22, 'female'),
('Pushkar', 'jha', 'New Delhi', 23, 'male'),
('Sujata', 'jha', 'Bihar', 30, 'female'),
('Roshini', 'Kumari', 'Bihar', 16, 'female'),
('Avinav', 'Pandey', 'New Delhi', 21, 'male'),
('Aman','Dhattarwal','Banglore', 30, 'male'),
('Aman','Agnihotri','Chennai', 23, 'male'),
('Aman','Malik','Agra', 35, 'male'),
('Bhawna','Dhattarwal','Banglore', 34, 'female'),
('Bhawna','Meena','Rajastha', 30, 'female')
SELECT * FROM geeksforgeeks;
Output

Example 1: Sorting by First Name and Age
This query sorts the table first by FIRSTNAME in ascending order, and then by AGE in ascending order for records with the same first name. It ensures that employees are listed alphabetically by name and then by their age in increasing order. If there are multiple records with the same first name, it then sorts by AGE.
Query:
SELECT * FROM geeksforgeeks ORDER BY FIRSTNAME, AGE;
Output

Example 2: Sorting by First Name, Last Name (Descending), and Age (Descending)
This query sorts the data first by FIRSTNAME in ascending order, then by LASTNAME in descending order for records with the same first name, and finally by AGE in descending order for records with the same last name. This ensures that the list is ordered by name, followed by the most recent ages, with a reverse order for last names.
Query:
SELECT * FROM geeksforgeeks ORDER BY FIRSTNAME, LASTNAME DESC, AGE DESC ;
Output

Conclusion
The ORDER BY clause in SQL is a powerful feature that helps to organize query results based on one or more columns. By using ASC for ascending order and DESC for descending order, we can structure our data in a way that best suits our analysis or reporting needs. Whether we are sorting by a single column or multiple columns, this feature helps bring clarity to our results. . It also allows for greater flexibility when handling large datasets, ensuring that the data is displayed in the most meaningful order.
Similar Reads
Selecting Multiple Columns Based On Condition in SQL SQL (Structured Query Language) is used to manage and query databases. One common requirement when querying data is selecting multiple columns based on specific conditions. Understanding how to use SQL for this purpose can enhance your ability to retrieve relevant data efficiently. In this article,
4 min read
sp_columns - SQL Server In SQL Server, managing and understanding database schemas is crucial for effective database administration and development. The sp_columns stored procedure is a valuable tool for retrieving detailed metadata about the columns of a specified table or view. In this article, We will learn about sp_col
6 min read
How to Order PysPark DataFrame by Multiple Columns ? In this article, we are going to order the multiple columns by using orderBy() functions in pyspark dataframe. Ordering the rows means arranging the rows in ascending or descending order, so we are going to create the dataframe using nested list and get the distinct data. orderBy() function that sor
2 min read
PL/SQL ORDER BY Clause In PL/SQL, the ORDER BY clause is a vital tool that allows for the sorting of query results by one or more columns, either in ascending or descending order. In this article, We will learn about ORDER BY clause in PL/SQL, its syntax, functionality, and practical usage through examples.Understanding O
7 min read
SQLite ORDER BY Clause SQLite is the most popular database engine which is written in C programming language. It is a serverless, easy-to-use relational database system and it is open source and self-contained. In this article, you will gain knowledge on the SQLite ORDER BY clause. By the end of this article, you will get
8 min read
How to Find the Maximum of Multiple Columns in SQL Server? When working with SQL Server databases, there are times when we need to find the maximum value among multiple columns. This task can be accomplished using various techniques within SQL queries. By using functions like CASE and GREATEST, SQL Server provides efficient ways to determine the maximum val
4 min read