Open In App

How to Sorting Data According to More Than One Column in SQL Server

Last Updated : 07 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Sorting data is the basic operation of a database environment. There is the SQL Server which is one of the most common relational database management systems that has a very powerful sorting function based on one or more columns. Though sorting within one column is straightforward, sorting data according to multiple columns and managing a proper approach becomes a complex task.

In this article, we discuss the complexities associated with grouping data by multiple columns in SQL Server using different approaches and effective techniques.

Understanding Multi-Column Sorting

Multi-column sorting refers to arranging the data rows where the values of two or more columns determine the sorting. It becomes especially helpful in such cases when we want to sort data based on some criteria.

As an example, when we are working on customer database data we can sort customers first by country and then by last name

Syntax for Multi-Column Sorting:

In SQL Server, multi-column sorting is achieved by specifying multiple columns in the ORDER BY clause. The syntax is straightforward:

SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ...;

Each column in the ORDER BY clause defines a level of sorting. Data is sorted based on the first column specified, and in case of ties, the subsequent columns are used to further refine the order.

Exanples of Sorting Data According to More Than One Column in SQL Server

CREATE TABLE Students (
StudentID INT,
Name VARCHAR(50),
Age INT,
Grade VARCHAR(1)
);

INSERT INTO Students (StudentID, Name, Age, Grade)
VALUES
(1, 'John', 20, 'A'),
(2, 'Alice', 22, 'B'),
(3, 'Emily', 21, 'C'),
(4, 'Michael', 19, 'B'),
(5, 'Sophia', 20, 'A');

output:

| StudentID | Name    | Age | Grade |
|-----------|---------|-----|-------|
| 1 | John | 20 | A |
| 2 | Alice | 22 | B |
| 3 | Emily | 21 | C |
| 4 | Michael | 19 | B |
| 5 | Sophia | 20 | A |

Example 1: Sort by Name in Ascending Order:

SELECT *
FROM Students
ORDER BY Name ASC;

output:

| StudentID | Name    | Age | Grade |
|-----------|---------|-----|-------|
| 2 | Alice | 22 | B |
| 3 | Emily | 21 | C |
| 1 | John | 20 | A |
| 4 | Michael | 19 | B |
| 5 | Sophia | 20 | A |

Explanation: This query sorts the student data alphabetically by name in ascending order.

Example 2: Sort by Age in Descending Order

SELECT *
FROM Students
ORDER BY Age DESC;

output:

| StudentID | Name    | Age | Grade |
|-----------|---------|-----|-------|
| 2 | Alice | 22 | B |
| 3 | Emily | 21 | C |
| 1 | John | 20 | A |
| 5 | Sophia | 20 | A |
| 4 | Michael | 19 | B |

Explanation: This query sorts the student data by age in descending order, from oldest to youngest.

Example 3: Sort by Grade in Ascending Order and then by Name in Descending Order

SELECT *
FROM Students
ORDER BY Grade ASC, Name DESC;

output:

| StudentID | Name    | Age | Grade |
|-----------|---------|-----|-------|
| 5 | Sophia | 20 | A |
| 1 | John | 20 | A |
| 4 | Michael | 19 | B |
| 2 | Alice | 22 | B |
| 3 | Emily | 21 | C |

Explanation: This query first sorts the student data by grade in ascending order, and within each grade, it sorts the names in descending order.

Example 4: Sort by Age in Ascending Order, Nulls Last

SELECT *
FROM Students
ORDER BY Age ASC NULLS LAST;

output:

| StudentID | Name    | Age | Grade |
|-----------|---------|-----|-------|
| 4 | Michael | 19 | B |
| 1 | John | 20 | A |
| 5 | Sophia | 20 | A |
| 3 | Emily | 21 | C |
| 2 | Alice | 22 | B |Explanation:

Explanation: This query sorts the student data by age in ascending order, placing NULL values at the end of the result set.

Optimizing Performance

Sort process of a large data set is essential requirement for maintaining proper performance in applications related to database. To optimize multi-column sorting in SQL Server, consider the following strategies:To optimize multi-column sorting in SQL Server, consider the following strategies:

  • Indexing: Composed of the columns used for sorting, the indexes help to speed up the access and sorting of the information.
  • Query Optimization: In the process of this step, apply conditions in order to reduce the volume of records that have to be sorted.
  • Limit Sorting Columns: Demand the minimum number of columns for sorting to those which are used in the query’s question.

Conclusion

Sorting two columns in SQL Server in a flexible way that gives the ability to manipulate result sets is one of highlights of SQL Server. Knowing the syntax, handling NULL values and tuning up performance will empower you to master multi­column sorting and provide solutions to the database needs. Knowing and using these skills will allow you to be more efficient and effective in using your SQL queries, thus result into quality performance and productivity.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads