SQL ORDER BY Clause

SQL ORDER BY Clause: Sorting Data in a Structured Manner

SQL ORDER BY Clause: Sorting Data in a Structured Manner

Interested in above project ,Click Below
WhatsApp
Telegram
LinkedIn

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:

IDNameAgeAddressSalary
1Raj Verma28Delhi50000
2Simran Kaur25Chandigarh48000
3Amit Singh30Lucknow55000
4Pooja Rao27Hyderabad52000
5Aditya Joshi29Pune53000
6Meera Sharma26Jaipur49000
7Vikram Das31Kolkata60000
8Anjali Nair24Bangalore47000
9Suresh Patil32Mumbai58000
10Rohan Iyer35Chennai62000

Example 1: Sorting in Ascending Order by Customer Name

Query:

SELECT * FROM customers ORDER BY Name ASC;

Output:

See also  Understanding SQL Tables: The Foundation of Data Organization

IDNameAgeAddressSalary
9Aakash Yadav32Mumbai43500
3Amit Singh45Meerut65000
5Balwant Singh45Varanasi36000
1Raj Verma21Delhi22000
6Meera Sharma26Mathura22000
8Anjali Nair29Pune40000
4Pooja Rao36Azamgarh26000
7Vikram Das19Ahmedabad38000
10Rohan Iyer35Aurangabad68800
2Simran Kaur22Bhopal21000

Example 2: Address Sorting in Ascending Order

Query:

SELECT * FROM customers ORDER BY Address;

Output:

IDNameAgeAddressSalary
7Rohit Shrivastav19Ahmedabad38000
10Sahil Sheikh35Aurangabad68800
4Ritesh Yadav36Azamgarh26000
2Shiva Tiwari22Bhopal21000
6Mahesh Sharma26Mathura22000
3Ajeet Bhargav45Meerut65000
1Himani Gupta21Modinagar22000
9Aakash Yadav32Mumbai43500
8Neeru Sharma29Pune40000
5Balwant Singh45Varanasi36000

Example 3: Salary Sorting in Declining Order

Query:

SELECT * FROM customers ORDER BY Salary DESC;

Output:

IDNameAgeAddressSalary
10Sahil Sheikh35Aurangabad68800
3Ajeet Bhargav45Meerut65000
9Aakash Yadav32Mumbai43500
8Neeru Sharma29Pune40000
7Rohit Shrivastav19Ahmedabad38000
5Balwant Singh45Varanasi36000
4Ritesh Yadav36Azamgarh26000
6Mahesh Sharma26Mathura22000
1Himani Gupta21Modinagar22000
2Shiva Tiwari22Bhopal21000

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.

See also  SQL vs NoSQL: A Detailed Comparison

  • 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

🎓 Need Complete Final Year Project?

Get Source Code + Report + PPT + Viva Questions (Instant Access)

🛒 Visit UpdateGadh Store →
💬 Chat Now