SQL ORDER BY Clause with Ascending Order

SQL ORDER BY Clause with Ascending Order

SQL ORDER BY Clause with Ascending Order

Interested in above project ,Click Below
WhatsApp
Telegram
LinkedIn

SQL ORDER BY Clause with Ascending Order

Whenever we want to sort the records based on the columns stored in the tables of the SQL database, we use the ORDER BY clause in SQL. This clause helps us organize records based on a specific column in a table. Initially, all the values stored in the selected column will be sorted, and then the corresponding rows will be displayed in the same order.

The ORDER BY clause allows us to sort records in ascending or descending order. When we use the ASC keyword, the sorting is in ascending order, while the DESC keyword sorts in descending order. If no keyword is specified, the sorting is done in ascending order by default.

Complete Python Course with Advance 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.

See also  Understanding SQL CROSS JOIN with Examples

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
Complete Advance AI topics:- 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.

See also  Understanding the SQL CREATE DATABASE Statement

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

🎓 Need Complete Final Year Project?

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

🛒 Visit UpdateGadh Store →
💬 Chat Now