sql order by clause with descending order

SQL ORDER BY Clause with Descending Order

SQL ORDER BY Clause with Descending Order

Whenever we want to sort records based on specific columns in an SQL database, we use the ORDER BY clause. This clause allows us to organize data in either ascending or descending order, making it easier to analyze and retrieve information efficiently.

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

Understanding the ORDER BY Clause

The ORDER BY clause sorts the values in a particular column and then displays the entire dataset based on that sorted order. By default, SQL sorts data in ascending order if no sorting keyword is specified. However, if we need the data in descending order, we use the DESC keyword.

Syntax for Sorting in Descending Order:

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

Let’s explore the ORDER BY clause using MySQL examples.

Example 1: Sorting Customers by Name in Descending Order

Consider a customers table with the following records:

ID NAME AGE CITY INCOME
1 Aman Verma 30 Delhi 50000
2 Rahul Sharma 28 Mumbai 45000
3 Kiran Mehta 35 Pune 52000
4 Neha Kapoor 40 Kolkata 60000
5 Pooja Singh 32 Chennai 48000
6 Rohit Malhotra 25 Jaipur 41000
7 Arjun Khanna 27 Hyderabad 47000
8 Sneha Choudhary 29 Ahmedabad 49000
9 Varun Joshi 31 Bangalore 53000
10 Meera Nair 34 Chandigarh 51000

Query:

SELECT * FROM customers ORDER BY Name DESC;

Output:

The records are displayed in descending order of the Name column.

Example 2: Sorting Customers by City in Descending Order

Query:

SELECT * FROM customers ORDER BY City DESC;

Output:

Records are arranged in descending order of the City column.

Example 3: Sorting Customers by Income in Descending Order

Query:

SELECT * FROM customers ORDER BY Income DESC;

Output:

Records are arranged in descending order of the Income column.

Example 4: Sorting Customers by Age in Descending Order

Query:

SELECT * FROM customers ORDER BY Age DESC;

Output:

Records are arranged in descending order of the Age column.

Example 5: Sorting Agents by Name in Descending Order

Consider another table agents with the following records:

AID NAME REGION COMMISSION PHONE SALARY
1 Vikram Sethi Bangalore 1% 9876543210 45000
2 Priya Nanda Chennai 5% 8765432109 35000
3 Aman Kapoor Mumbai 2% 7654321098 60000
4 Rhea Desai Pune 3% 6543210987 37500
5 Nitin Joshi Delhi 4% 5432109876 40000
6 Anjali Saxena Jaipur 8% 4321098765 27000
7 Dev Sharma Chandigarh 6% 3210987654 32000
8 Kunal Batra Udaipur 2% 2109876543 26000
9 Sneha Pillai Ujjain 9% 1098765432 39000
10 Akash Verma Goa 7% 0198765432 46000

Query:

SELECT * FROM agents ORDER BY Name DESC;

Output:

Records are displayed in descending order of the Name column.

Example 6: Sorting Agents by Salary in Descending Order

Query:

SELECT * FROM agents ORDER BY Salary DESC;

Output:

Records are displayed in descending order of the Salary column.

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

Conclusion

The ORDER BY clause is a crucial tool in SQL that helps organize records efficiently. By using the DESC keyword, we can easily sort records in descending order, making data analysis more convenient and insightful.


order by ascending sql
sql order by specific sequence
sql order by clause with descending order with where clause
sql order by clause with descending order multiple columns
sql order by clause with descending order
sql order by descending not working
sql order by descending example
sql order by descending syntax
group by and order by in sql
sql order of operations
sql order by clause with descending order w3schools
sql order by clause with descending order example
sql order by clause with descending order oracle
sql order by clause with descending order multiple columns

Post Comment