SQL ORDER BY Clause with Ascending Order
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:
ID | Name | Age | Address | Salary |
---|---|---|---|---|
1 | Aman Sharma | 27 | Delhi | 30000 |
2 | Priya Verma | 24 | Mumbai | 40000 |
3 | Ravi Kapoor | 35 | Kolkata | 55000 |
4 | Neha Singh | 29 | Bangalore | 42000 |
5 | Rajesh Kumar | 41 | Hyderabad | 60000 |
Query to Sort Customers by Name in Ascending Order:
SELECT * FROM customers ORDER BY Name ASC;
Output:
ID | Name | Age | Address | Salary |
---|---|---|---|---|
1 | Aman Sharma | 27 | Delhi | 30000 |
4 | Neha Singh | 29 | Bangalore | 42000 |
2 | Priya Verma | 24 | Mumbai | 40000 |
5 | Rajesh Kumar | 41 | Hyderabad | 60000 |
3 | Ravi Kapoor | 35 | Kolkata | 55000 |
All records are sorted alphabetically by the Name
column.
Example 2: Sorting by Address in Ascending Order
SELECT * FROM customers ORDER BY Address;
Output:
ID | Name | Age | Address | Salary |
---|---|---|---|---|
4 | Neha Singh | 29 | Bangalore | 42000 |
1 | Aman Sharma | 27 | Delhi | 30000 |
5 | Rajesh Kumar | 41 | Hyderabad | 60000 |
3 | Ravi Kapoor | 35 | Kolkata | 55000 |
2 | Priya Verma | 24 | Mumbai | 40000 |
Example 3: Sorting by Salary in Ascending Order
SELECT * FROM customers ORDER BY Salary ASC;
Output:
ID | Name | Age | Address | Salary |
---|---|---|---|---|
1 | Aman Sharma | 27 | Delhi | 30000 |
2 | Priya Verma | 24 | Mumbai | 40000 |
4 | Neha Singh | 29 | Bangalore | 42000 |
3 | Ravi Kapoor | 35 | Kolkata | 55000 |
5 | Rajesh Kumar | 41 | Hyderabad | 60000 |
Example 4: Sorting by Age in Ascending Order
SELECT * FROM customers ORDER BY Age;
Output:
ID | Name | Age | Address | Salary |
---|---|---|---|---|
2 | Priya Verma | 24 | Mumbai | 40000 |
1 | Aman Sharma | 27 | Delhi | 30000 |
4 | Neha Singh | 29 | Bangalore | 42000 |
3 | Ravi Kapoor | 35 | Kolkata | 55000 |
5 | Rajesh Kumar | 41 | Hyderabad | 60000 |
Sorting Data in Another Table (Agents Table)
Consider the agents
table with the following records:
AID | Name | WorkArea | Profit_Percent | ContactNumber | Salary |
---|---|---|---|---|---|
1 | Ankit Jain | Delhi | 5 | 9898123456 | 42000 |
2 | Ravi Kumar | Mumbai | 3 | 9876543210 | 38000 |
3 | Priya Sharma | Kolkata | 7 | 9765432109 | 50000 |
4 | Rakesh Verma | Bangalore | 4 | 9654321098 | 45000 |
5 | Neha Gupta | Hyderabad | 6 | 9543210987 | 47000 |
Query to Sort Agents by Name in Ascending Order:
SELECT * FROM agents ORDER BY Name ASC;
Output:
AID | Name | WorkArea | Profit_Percent | ContactNumber | Salary |
---|---|---|---|---|---|
1 | Ankit Jain | Delhi | 5 | 9898123456 | 42000 |
5 | Neha Gupta | Hyderabad | 6 | 9543210987 | 47000 |
3 | Priya Sharma | Kolkata | 7 | 9765432109 | 50000 |
4 | Rakesh Verma | Bangalore | 4 | 9654321098 | 45000 |
2 | Ravi Kumar | Mumbai | 3 | 9876543210 | 38000 |
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.
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
Post Comment