SQL Tutorial
SQL Database
SQL Table
SQL Select
SQL Order By
SQL Insert
SQL Update
SQL Delete
Difference
SQL Injection
SQL String Functions
Miscl
- SQL Formatter
- SQL group by
- SQL add/drop/update column operation
- SQL CAST Function
- SQL Comments
- SQL CONCAT Function
- CTE (Common Table Expression)SQL
- How to use distinct in SQL?
- Joining Three or More Tables in SQL
- What is Web SQL?
- How to create functions in SQL?
- How to run SQL Script?
- How to Delete Duplicate Rows in SQL?
- Nth Highest salary
- 12 Codd's Rules
- SQL EXCEPT
- Types of SQL JOIN
- Change datatype of column in SQL
- SQL Auto Increment
- SQL Like
- Commit and Rollback in SQL
- SQL Concatenate
- SQL get month from the date
- Savepoint in SQL
- SQL ORDER BY DATE
- TIME Datatype in SQL
- SQL BETWEEN
- CRUD Operations in SQL
- SQL INDEX
- Scalar Functions in SQL
- SET Operators in SQL
- Types of SQL Commands
- TCL Commands in SQL
- SQL Subquery
- SQL View
- Constraints in SQL
- Pattern Matching in SQL
- SQL Date Functions
- DDL Commands in SQL
- DML Commands in SQL
- SQL CASE
- SQL Inner Join
- SQL IN Operator
- Check Constraint in SQL
- SQL CLAUSES
- SQL LOGICAL OPERATORS
- Delete Column from Table
- Add Column in the Table
- Delete one row in SQL
- Change the Column Value
- How to Add Foreign Key in SQL
- Add a Primary Key
- Insert One or More rows
- How to Use LIKE in SQL
- Cursor in SQL
- Difference Between DROP and Truncate
- SQL Comparison Operators
- SQL COUNT WHERE
- SQL SELECT MIN
- SQL Stored Procedure
- SQL SELECT AVG
- SQL SELECT MAX
- SQL ADD COLUMN
- How to use Auto-Increment in SQL
- SQL Languages
- SQL Arithmetic Operators
- How to Use GROUP BY in SQL
- How to Use ORDER BY in SQL
- Trigger in SQL
- What is Race Condition
- SQL COUNT DISTINCT
PL/SQL Tutorial
Sql Interview Question
SQl Quiz
SQL ORDER BY Clause
- Whenever we want to sort the records based on the columns stored in the tables of the SQL database, then we consider using the ORDER BY clause in SQL.
- The ORDER BY clause in SQL will help us to sort the records based on the specific column of a table. This means that all the values stored in the column on which we are applying ORDER BY clause will be sorted, and the corresponding column values will be displayed in the sequence in which we have obtained the values in the earlier step.
- Using the ORDER BY clause, we can sort the records in ascending or descending order as per our requirement. The records will be sorted in ascending order whenever the ASC keyword is used with ORDER by clause. DESC keyword will sort the records in descending order.
- If no keyword is specified after the column based on which we have to sort the records, in that case, the sorting will be done by default in the ascending order.
Before writing the queries for sorting the records, let us understand the syntax.
Syntax to sort the records in ascending order:
Syntax to sort the records in descending order:
Syntax to sort the records in ascending order without using ASC keyword:
Let us explore more on this topic with the help of examples. We will use the MySQL database for writing the queries in examples.
Consider we have customers table with the following records:
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Himani Gupta | 21 | Modinagar | 22000 |
2 | Shiva Tiwari | 22 | Bhopal | 21000 |
3 | Ajeet Bhargav | 45 | Meerut | 65000 |
4 | Ritesh Yadav | 36 | Azamgarh | 26000 |
5 | Balwant Singh | 45 | Varanasi | 36000 |
6 | Mahesh Sharma | 26 | Mathura | 22000 |
7 | Rohit Shrivastav | 19 | Ahemdabad | 38000 |
8 | Neeru Sharma | 29 | Pune | 40000 |
9 | Aakash Yadav | 32 | Mumbai | 43500 |
10 | Sahil Sheikh | 35 | Aurangabad | 68800 |
Example 1:
Write a query to sort the records in the ascending order of the customer names stored in the customers table.
Query:
Here in a SELECT query, an ORDER BY clause is applied on the column 'Name' to sort the records. ASC keyword will sort the records in ascending order.
You will get the following output:
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
9 | Aakash Yadav | 32 | Mumbai | 43500 |
3 | Ajeet Bhargav | 45 | Meerut | 65000 |
5 | Balwant Singh | 45 | Varanasi | 36000 |
1 | Himani Gupta | 21 | Modinagar | 22000 |
6 | Mahesh Sharma | 26 | Mathura | 22000 |
8 | Neeru Sharma | 29 | Pune | 40000 |
4 | Ritesh Yadav | 36 | Azamgarh | 26000 |
7 | Rohit Shrivastav | 19 | Ahemdabad | 38000 |
10 | Sahil Sheikh | 35 | Aurangabad | 68800 |
2 | Shiva Tiwari | 22 | Bhopal | 21000 |
All the records present in the customers table are displayed in the ascending order of the customer's name.
Example 2:
Write a query to sort the records in the ascending order of the addresses stored in the customers table.
Query:
Here in a SELECT query, an ORDER BY clause is applied to the 'Address' column to sort the records. No keyword is used after the ORDER BY clause. Hence, the records, by default, will be sorted in ascending order.
You will get the following output:
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
7 | Rohit Shrivastav | 19 | Ahemdabad | 38000 |
10 | Sahil Sheikh | 35 | Aurangabad | 68800 |
4 | Ritesh Yadav | 36 | Azamgarh | 26000 |
2 | Shiva Tiwari | 22 | Bhopal | 21000 |
6 | Mahesh Sharma | 26 | Mathura | 22000 |
3 | Ajeet Bhargav | 45 | Meerut | 65000 |
1 | Himani Gupta | 21 | Modinagar | 22000 |
9 | Aakash Yadav | 32 | Mumbai | 43500 |
8 | Neeru Sharma | 29 | Pune | 40000 |
5 | Balwant Singh | 45 | Varanasi | 36000 |
All the records present in the customers table are displayed in the ascending order of the customer's address.
Example 3:
Write a query to sort the records in the descending order of the customer salary stored in the customers table.
Query:
Here in a SELECT query, an ORDER BY clause is applied on the column ?Salary? to sort the records. DESC keyword will sort the records in descending order.
You will get the following output:
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
10 | Sahil Sheikh | 35 | Aurangabad | 68800 |
3 | Ajeet Bhargav | 45 | Meerut | 65000 |
9 | Aakash Yadav | 32 | Mumbai | 43500 |
8 | Neeru Sharma | 29 | Pune | 40000 |
7 | Rohit Shrivastav | 19 | Ahemdabad | 38000 |
5 | Balwant Singh | 45 | Varanasi | 36000 |
4 | Ritesh Yadav | 36 | Azamgarh | 26000 |
6 | Mahesh Sharma | 26 | Mathura | 22000 |
1 | Himani Gupta | 21 | Modinagar | 22000 |
2 | Shiva Tiwari | 22 | Bhopal | 21000 |
All the records present in the customers table are displayed in the descending order of the customer's salary.
Example 4:
Write a query to sort the records in the descending order of the customer age stored in the customers table.
Query:
Here in a SELECT query, an ORDER BY clause is applied on the column 'Age' to sort the records. DESC keyword will sort the records in descending order.
You will get the following output:
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
3 | Ajeet Bhargav | 45 | Meerut | 65000 |
5 | Balwant Singh | 45 | Varanasi | 36000 |
4 | Ritesh Yadav | 36 | Azamgarh | 26000 |
10 | Sahil Sheikh | 35 | Aurangabad | 68800 |
9 | Aakash Yadav | 32 | Mumbai | 43500 |
8 | Neeru Sharma | 29 | Pune | 40000 |
6 | Mahesh Sharma | 26 | Mathura | 22000 |
2 | Shiva Tiwari | 22 | Bhopal | 21000 |
1 | Himani Gupta | 21 | Modinagar | 22000 |
7 | Rohit Shrivastav | 19 | Ahemdabad | 38000 |
All the records present in the customers table are displayed in the descending order of the customer's age.
Consider we have another table named agents with the following records:
AID | Name | WorkArea | Profit_Percent | ContactNumber | Salary |
---|---|---|---|---|---|
1 | Gurpreet Singh | Bangalore | 1 | 9989675432 | 43000 |
2 | Sakshi Kumari | Chennai | 5 | 8190567342 | 25000 |
3 | Prachi Desai | Mumbai | 2 | 9056123432 | 60000 |
4 | Shivani More | Pune | 3 | 8894236789 | 35500 |
5 | Pallavi Singh | Delhi | 4 | 7798092341 | 38700 |
6 | Rohini Kulkarni | Ambala | 8 | 7890945612 | 25670 |
7 | Shweta Dixit | Chandigarh | 6 | 8898786453 | 31670 |
8 | Sonakshi Tiwari | Udaipur | 2 | 9809453421 | 25050 |
9 | Anushka Tripathi | Ujjain | 9 | 8909124326 | 38000 |
10 | Devika Sharma | Goa | 7 | 7864523145 | 44050 |
Example 1:
Write a query to sort the records in the ascending order of the agent names stored in the agents table.
Query:
Here in a SELECT query, an ORDER BY clause is applied on the column 'Name' to sort the records. ASC keyword will sort the records in ascending order.
You will get the following output:
AID | Name | WorkArea | Profit_Percent | ContactNumber | Salary |
---|---|---|---|---|---|
9 | Anushka Tripathi | Ujjain | 9 | 8909124326 | 38000 |
10 | Devika Sharma | Goa | 7 | 7864523145 | 44050 |
1 | Gurpreet Singh | Bangalore | 1 | 9989675432 | 43000 |
5 | Pallavi Singh | Delhi | 4 | 7798092341 | 38700 |
3 | Prachi Desai | Mumbai | 2 | 9056123432 | 60000 |
6 | Rohini Kulkarni | Ambala | 8 | 7890945612 | 25670 |
2 | Sakshi Kumari | Chennai | 5 | 8190567342 | 25000 |
4 | Shivani More | Pune | 3 | 8894236789 | 35500 |
7 | Shweta Dixit | Chandigarh | 6 | 8898786453 | 31670 |
8 | Sonakshi Tiwari | Udaipur | 2 | 9809453421 | 25050 |
All the records present in the agents table are displayed in the ascending order of the agent's name.
Example 2:
Write a query to sort the records in the descending order of the work area stored in the agents table.
Query:
Here in a SELECT query, an ORDER BY clause is applied on the column 'WorkArea' to sort the records. DESC keyword will sort the records in descending order.
You will get the following output:
AID | Name | WorkArea | Profit_Percent | ContactNumber | Salary |
---|---|---|---|---|---|
9 | Anushka Tripathi | Ujjain | 9 | 8909124326 | 38000 |
8 | Sonakshi Tiwari | Udaipur | 2 | 9809453421 | 25050 |
4 | Shivani More | Pune | 3 | 8894236789 | 35500 |
3 | Prachi Desai | Mumbai | 2 | 9056123432 | 60000 |
10 | Devika Sharma | Goa | 7 | 7864523145 | 44050 |
5 | Pallavi Singh | Delhi | 4 | 7798092341 | 38700 |
2 | Sakshi Kumari | Chennai | 5 | 8190567342 | 25000 |
7 | Shweta Dixit | Chandigarh | 6 | 8898786453 | 31670 |
1 | Gurpreet Singh | Bangalore | 1 | 9989675432 | 43000 |
6 | Rohini Kulkarni | Ambala | 8 | 7890945612 | 25670 |
All the records present in the agents table are displayed in the descending order of the customer's work area.
Example 3:
Write a query to sort the records in the ascending order of the agent salary stored in the agents table.
Query:
Here in a SELECT query, an ORDER BY clause is applied on the column 'Salary' to sort the records. No keyword is used after the ORDER BY clause. Hence, the records, by default, will be sorted in ascending order.
You will get the following output:
AID | Name | WorkArea | Profit_Percent | ContactNumber | Salary |
---|---|---|---|---|---|
2 | Sakshi Kumari | Chennai | 5 | 8190567342 | 25000 |
8 | Sonakshi Tiwari | Udaipur | 2 | 9809453421 | 25050 |
6 | Rohini Kulkarni | Ambala | 8 | 7890945612 | 25670 |
7 | Shweta Dixit | Chandigarh | 6 | 8898786453 | 31670 |
4 | Shivani More | Pune | 3 | 8894236789 | 35500 |
9 | Anushka Tripathi | Ujjain | 9 | 8909124326 | 38000 |
5 | Pallavi Singh | Delhi | 4 | 7798092341 | 38700 |
1 | Gurpreet Singh | Bangalore | 1 | 9989675432 | 43000 |
10 | Devika Sharma | Goa | 7 | 7864523145 | 44050 |
3 | Prachi Desai | Mumbai | 2 | 9056123432 | 60000 |
All the records present in the agents table are displayed in the ascending order of the customer's salary.
Example 4:
Write a query to sort the records in the descending order of the agent salary stored in the agents table.
Query:
Here in a SELECT query, an ORDER BY clause is applied on the column 'Salary' to sort the records. DESC keyword will sort the records in descending order.
You will get the following output:
AID | Name | WorkArea | Profit_Percent | ContactNumber | Salary |
---|---|---|---|---|---|
3 | Prachi Desai | Mumbai | 2 | 9056123432 | 60000 |
10 | Devika Sharma | Goa | 7 | 7864523145 | 44050 |
1 | Gurpreet Singh | Bangalore | 1 | 9989675432 | 43000 |
5 | Pallavi Singh | Delhi | 4 | 7798092341 | 38700 |
9 | Anushka Tripathi | Ujjain | 9 | 8909124326 | 38000 |
4 | Shivani More | Pune | 3 | 8894236789 | 35500 |
7 | Shweta Dixit | Chandigarh | 6 | 8898786453 | 31670 |
6 | Rohini Kulkarni | Ambala | 8 | 7890945612 | 25670 |
8 | Sonakshi Tiwari | Udaipur | 2 | 9809453421 | 25050 |
2 | Sakshi Kumari | Chennai | 5 | 8190567342 | 25000 |
All the records present in the agents table are displayed in the descending order of the customer's address.