SQL Tutorial

SQL ORDER BY Clause with Ascending Order

SQL ORDER BY Clause with Ascending Order
SQL ORDER BY Clause with Ascending Order

SQL ORDER BY Clause with Ascending Order

Whenever we need to sort records based on columns stored in SQL database tables, we use the ORDER BY clause. This clause helps organize records according to a specific column in a table. The values in the selected column are sorted first, and the corresponding rows are then displayed in the same order.

The ORDER BY clause allows us to sort records in ascending or descending order. The ASC keyword sorts records in ascending order, while the DESC keyword sorts them in descending order. If no keyword is specified, the records are sorted in ascending order by default.

SQL ORDER BY Clause with Ascending Order

Complete Advance AI Topics: Click Here
SQL Tutorial:
Click Here

Syntax to Sort Records in Ascending Order

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

Syntax without Using ASC Keyword (Defaults to Ascending Order)

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

Let’s explore this further with some examples using a MySQL database.

Example 1: Sorting by Customer Name

Consider a customers table with the following records:

IDNameAgeAddressSalary
1Aman Sharma27Delhi30000
2Priya Verma24Mumbai40000
3Ravi Kapoor35Kolkata55000
4Neha Singh29Bangalore42000
5Rajesh Kumar41Hyderabad60000

Query to Sort Customers by Name in Ascending Order:

SELECT * FROM customers ORDER BY Name ASC;

Output:

IDNameAgeAddressSalary
1Aman Sharma27Delhi30000
4Neha Singh29Bangalore42000
2Priya Verma24Mumbai40000
5Rajesh Kumar41Hyderabad60000
3Ravi Kapoor35Kolkata55000

All records are sorted alphabetically by the Name column.

Example 2: Sorting by Address in Ascending Order

SELECT * FROM customers ORDER BY Address;

Output:

IDNameAgeAddressSalary
4Neha Singh29Bangalore42000
1Aman Sharma27Delhi30000
5Rajesh Kumar41Hyderabad60000
3Ravi Kapoor35Kolkata55000
2Priya Verma24Mumbai40000

Example 3: Sorting by Salary in Ascending Order

SELECT * FROM customers ORDER BY Salary ASC;

Output:

IDNameAgeAddressSalary
1Aman Sharma27Delhi30000
2Priya Verma24Mumbai40000
4Neha Singh29Bangalore42000
3Ravi Kapoor35Kolkata55000
5Rajesh Kumar41Hyderabad60000

Example 4: Sorting by Age in Ascending Order

SELECT * FROM customers ORDER BY Age;

Output:

IDNameAgeAddressSalary
2Priya Verma24Mumbai40000
1Aman Sharma27Delhi30000
4Neha Singh29Bangalore42000
3Ravi Kapoor35Kolkata55000
5Rajesh Kumar41Hyderabad60000

Sorting Data in Another Table (Agents Table)

Consider the agents table with the following records:

AIDNameWorkAreaProfit_PercentContactNumberSalary
1Ankit JainDelhi5989812345642000
2Ravi KumarMumbai3987654321038000
3Priya SharmaKolkata7976543210950000
4Rakesh VermaBangalore4965432109845000
5Neha GuptaHyderabad6954321098747000

Query to Sort Agents by Name in Ascending Order:

SELECT * FROM agents ORDER BY Name ASC;

Output:

AIDNameWorkAreaProfit_PercentContactNumberSalary
1Ankit JainDelhi5989812345642000
5Neha GuptaHyderabad6954321098747000
3Priya SharmaKolkata7976543210950000
4Rakesh VermaBangalore4965432109845000
2Ravi KumarMumbai3987654321038000

Download New Real Time Projects :-Click here

Conclusion

The ORDER BY clause is essential for organizing and analyzing SQL data efficiently. By default, it sorts records in ascending order, and when paired with the ASC keyword, it explicitly maintains that order. Understanding this clause enables users to retrieve structured and meaningful data from their database tables effortlessly.

By leveraging ORDER BY, businesses can enhance data reporting, improve database navigation, and optimize queries for a better data retrieval experience.


group by clause in sql
order by ascending sql
order by ascending sql
sql order by specific sequence
sql order by clause with ascending order where clause
sql order by clause with ascending order
sql order by ascending where
sql order by clause with ascending order and where clause
sql order by ascending syntax
sql order by clause with ascending order multiple columns
group by and order by in sql
sql order by date
sql order by clause with ascending order w3schools
sql order by clause with ascending order example
sql order by clause with ascending order oracle
sql order by clause with ascending order multiple columns

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat with us