ORDER BY Clause in SQL
Video Tutorial
Overview
ORDER BY clause helps us sort the records of a column in a table using the ASC and DESC keywords in ascending and descending order, respectively. We can sort one column, multiple columns, sort without using the ASC/DESC keyword, sort using both ASC/DESC keywords and according to relative column position.
Scope
- This article explains what ORDER BY clause is in SQL?
- It shows us how to use it in multiple ways to sort the records based on column names and positions depending upon the keyword used.
- It also explains the working of the ASC and DESC keywords.
Introduction to ORDER BY Clause in SQL
ORDER BY clause in SQL sorts the records of the column of a table in the SQL database. It helps us sort the column in both ascending and descending order. The ASC keyword helps us sort in ascending order while the DESC sorts in descending order. If no keyword is specified in which we have to sort the records in the column, it will take its default value. `It sorts the records in ascending order by default.
The ORDER BY clause can only be for SELECT statements.
Syntax
-
Sort according to one column
We use ASC and DESC keywords to sort a column in ascending and descending order, respectively.
SYNTAX:
-
Sort according to multiple columns
Multiple columns are sorted simultaneously by separating their names by the comma(,) operator. The names of the two columns must be unique. The records are first sorted by the first column and then the sorted list by the second column if two rows had some value earlier.
SYNTAX:
-
Sort the records in ascending order
ASC keyword is used to sort the records in ascending order. SYNTAX:
-
Sort the records in descending order
DESC keyword is used to sort the records in descending order. SYNTAX:
-
Sort the records in ascending order without using the ASC keyword
When ASC or DESC keyword is not specified, the ORDER BY clause sorts the records in ascending order.
Examples
Consider the table Employees having the following records:
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Amish | 32 | Ahmedabad | 2000.00 |
2 | Nisha | 25 | Delhi | 1500.00 |
3 | Suman | 23 | Kota | 2000.00 |
4 | Yashwant | 25 | Mumbai | 6500.00 |
5 | Durgesh | 27 | Bhopal | 8500.00 |
6 | Esha | 22 | MP | 4500.00 |
7 | Khansha | 24 | Indore | 10000.00 |
8 | Amisha | 28 | Kolkata | 1000.00 |
9 | Sarfaraz | 24 | Patna | 6000.00 |
-
Sort according to a single column
SELECT * FROM Employees ORDER BY NAME ASC;
This SQL query will sort all the records by their names in ascending order.
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Amish | 32 | Ahmedabad | 2000.00 |
8 | Amisha | 28 | Kolkata | 1000.00 |
5 | Durgesh | 27 | Bhopal | 8500.00 |
6 | Esha | 22 | MP | 4500.00 |
7 | Khansha | 24 | Indore | 10000.00 |
2 | Nisha | 25 | Delhi | 1500.00 |
9 | Sarfaraz | 24 | Patna | 6000.00 |
3 | Suman | 23 | Kota | 2000.00 |
4 | Yashwant | 25 | Mumbai | 6500.00 |
-
Sort according to multiple columns
SELECT * FROM Employees ORDER BY NAME ASC, ID DESC;
This SQL Query collects all the Employees data and sorts it first according to NAME in ascending order and then according to their ID in descending order.
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Amish | 32 | Ahmedabad | 2000.00 |
8 | Amisha | 28 | Kolkata | 1000.00 |
5 | Durgesh | 27 | Bhopal | 8500.00 |
6 | Esha | 22 | MP | 4500.00 |
7 | Khansha | 24 | Indore | 10000.00 |
2 | Nisha | 25 | Delhi | 1500.00 |
9 | Sarfaraz | 24 | Patna | 6000.00 |
3 | Suman | 23 | Kota | 2000.00 |
4 | Yashwant | 25 | Mumbai | 6500.00 |
Note: You can get the same output by skipping the ASC keyword in the SQL Query as it is the default value of the ORDER By clause.
SELECT * FROM Employees ORDER BY NAME, ID DESC;
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Amish | 32 | Ahmedabad | 2000.00 |
8 | Amisha | 28 | Kolkata | 1000.00 |
5 | Durgesh | 27 | Bhopal | 8500.00 |
6 | Esha | 22 | MP | 4500.00 |
7 | Khansha | 24 | Indore | 10000.00 |
2 | Nisha | 25 | Delhi | 1500.00 |
9 | Sarfaraz | 24 | Patna | 6000.00 |
3 | Suman | 23 | Kota | 2000.00 |
4 | Yashwant | 25 | Mumbai | 6500.00 |
-
Sorting without using ASC/DESC attribute
If the keywords are not specified, the records sort in ascending order by taking the default value of the ORDER BY clause. This method is not for sorting in descending order.
SELECT * FROM Employees ORDER BY NAME;
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Amish | 32 | Ahmedabad | 2000.00 |
8 | Amisha | 28 | Kolkata | 1000.00 |
5 | Durgesh | 27 | Bhopal | 8500.00 |
6 | Esha | 22 | MP | 4500.00 |
7 | Khansha | 24 | Indore | 10000.00 |
2 | Nisha | 25 | Delhi | 1500.00 |
9 | Sarfaraz | 24 | Patna | 6000.00 |
3 | Suman | 23 | Kota | 2000.00 |
4 | Yashwant | 25 | Mumbai | 6500.00 |
-
Using both ASC and DESC attributes
When sorting your records in SQL using the ORDER BY clause, you can use both ASC and DESC keywords to perform sorting in a single SELECT statement.
SELECT * FROM Employees ORDER BY NAME ASC, ID DESC;
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Amish | 32 | Ahmedabad | 2000.00 |
8 | Amisha | 28 | Kolkata | 1000.00 |
5 | Durgesh | 27 | Bhopal | 8500.00 |
6 | Esha | 22 | MP | 4500.00 |
7 | Khansha | 24 | Indore | 10000.00 |
2 | Nisha | 25 | Delhi | 1500.00 |
9 | Sarfaraz | 24 | Patna | 6000.00 |
3 | Suman | 23 | Kota | 2000.00 |
4 | Yashwant | 25 | Mumbai | 6500.00 |
This example returns all the records sorted by NAME in ascending order and then a secondary sorting by ID in descending order. It happens only when there are records with the same names. It gets sorted according to the ID in descending order.
-
Sorting in descending order
SELECT * FROM Employees ORDER BY NAME DESC;
This SQL Query sorts the records in the table in the descending order of their names.
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
4 | Yashwant | 25 | Mumbai | 6500.00 |
3 | Suman | 23 | Kota | 2000.00 |
9 | Sarfaraz | 24 | Patna | 6000.00 |
2 | Nisha | 25 | Delhi | 1500.00 |
7 | Khansha | 24 | Indore | 10000.00 |
6 | Esha | 22 | MP | 4500.00 |
5 | Durgesh | 27 | Bhopal | 8500.00 |
8 | Amisha | 28 | Kolkata | 1000.00 |
1 | Amish | 32 | Ahmedabad | 2000.00 |
-
Sorting by relative position or column number
The column number is an integer that determines the relative position of the column in a table. It must be greater than 0 but not the number of columns so that the column is present in the table.
It disturbs the readability of the code and makes it difficult to predict. The sequence in which columns occur depends on the physical order of the table. The physical order often gets changed by adding, deleting, or updating the table. Thus, the query using relative position gives a different result on changing the order of columns in the table. Referring to the ORDER BY clause by column names has no impact on the results making it more suitable.
SELECT * FROM Employees ORDER BY 3 ASC;
In this example, we have sorted the table using column number 3, i.e., AGE ascending order.
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
6 | Esha | 22 | MP | 4500.00 |
3 | Suman | 23 | Kota | 2000.00 |
7 | Khansha | 24 | Indore | 10000.00 |
9 | Sarfaraz | 24 | Patna | 6000.00 |
2 | Nisha | 25 | Delhi | 1500.00 |
4 | Yashwant | 25 | Mumbai | 6500.00 |
5 | Durgesh | 27 | Bhopal | 8500.00 |
8 | Amisha | 28 | Kolkata | 1000.00 |
1 | Amish | 32 | Ahmedabad | 2000.00 |
Conclusion
- Use the ORDER BY clause to sort records returned by the SELECT clause.
- Use ASC keyword to sort in ascending order and DESC keyword to sort in DESC order.
- No keywords are required for sorting in ascending order as it is the default value of the ORDER BY clause.
- Sorting can be done using multiple columns if two rows have the same value in the sorted column.