SQL ORDER BY Clause

SQL ORDER BY Clause: Sorting Data in a Structured Manner

SQL ORDER BY Clause

Introduction

When working with databases, retrieving data in a meaningful order is crucial for readability and analysis. To assist us in sorting records according to particular table columns, SQL offers the ORDER BY clause. This clause allows us to arrange data in either ascending (ASC) or descending (DESC) order.

In this blog post, we will explore the ORDER BY clause in SQL, understand its syntax, and see real-world examples using MySQL.

Complete Python Course with Advance topics:-Click Here
SQL Tutorial :-Click Here

Syntax of ORDER BY Clause

Sorting in Ascending Order

SELECT ColumnName1, ..., ColumnNameN FROM TableName ORDER BY ColumnName ASC;

Sorting in Descending Order

SELECT ColumnName1, ..., ColumnNameN FROM TableName ORDER BY ColumnName DESC;

Default Sorting (Ascending Order)

SELECT ColumnName1, ..., ColumnNameN FROM TableName ORDER BY ColumnName;

Note: If no keyword (ASC or DESC) is specified, SQL sorts the data in ascending order by default.

Example Scenarios

Let’s look at a table of customers that contains the following information:

ID Name Age Address Salary
1 Raj Verma 28 Delhi 50000
2 Simran Kaur 25 Chandigarh 48000
3 Amit Singh 30 Lucknow 55000
4 Pooja Rao 27 Hyderabad 52000
5 Aditya Joshi 29 Pune 53000
6 Meera Sharma 26 Jaipur 49000
7 Vikram Das 31 Kolkata 60000
8 Anjali Nair 24 Bangalore 47000
9 Suresh Patil 32 Mumbai 58000
10 Rohan Iyer 35 Chennai 62000

Example 1: Sorting in Ascending Order by Customer Name

Query:

SELECT * FROM customers ORDER BY Name ASC;

Output:

ID Name Age Address Salary
9 Aakash Yadav 32 Mumbai 43500
3 Amit Singh 45 Meerut 65000
5 Balwant Singh 45 Varanasi 36000
1 Raj Verma 21 Delhi 22000
6 Meera Sharma 26 Mathura 22000
8 Anjali Nair 29 Pune 40000
4 Pooja Rao 36 Azamgarh 26000
7 Vikram Das 19 Ahmedabad 38000
10 Rohan Iyer 35 Aurangabad 68800
2 Simran Kaur 22 Bhopal 21000

Example 2: Address Sorting in Ascending Order

Query:

SELECT * FROM customers ORDER BY Address;

Output:

ID Name Age Address Salary
7 Rohit Shrivastav 19 Ahmedabad 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

Example 3: Salary Sorting in Declining Order

Query:

SELECT * FROM customers ORDER BY Salary DESC;

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 Ahmedabad 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

Conclusion

The ORDER BY clause in SQL is a powerful tool that enables database users to organize data effectively. Whether sorting alphabetically, numerically, or chronologically, this clause enhances data retrieval and usability.

  • Use ASC for ascending order.
  • Use DESC for descending order.
  • Omitting the sorting keyword defaults to ascending order.

Mastering the ORDER BY clause will help you manage and analyze database records efficiently, improving data presentation and decision-making.

Download New Real Time Projects :-Click here
Complete Advance AI topics:- CLICK HERE


sql order by clause
sql order by clause is invalid in views
sql order by clause is used for
sql group by clause
sql group by clause with case statement
sql group by clause multiple columns
sql server order by clause
sql query order by clause
sql order by desc limit 1
sql order by desc null last
sql order by clause example
group by clause in sql
sql order by clause
how does the sql order by clause work
what does the sql order by clause 2
sql order by clause is invalid in views

Post Comment