sql order by clause with descending order

SQL ORDER BY Clause with Descending Order

SQL ORDER BY Clause with Descending Order

Interested in above project ,Click Below
WhatsApp
Telegram
LinkedIn

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:

IDNAMEAGECITYINCOME
1Aman Verma30Delhi50000
2Rahul Sharma28Mumbai45000
3Kiran Mehta35Pune52000
4Neha Kapoor40Kolkata60000
5Pooja Singh32Chennai48000
6Rohit Malhotra25Jaipur41000
7Arjun Khanna27Hyderabad47000
8Sneha Choudhary29Ahmedabad49000
9Varun Joshi31Bangalore53000
10Meera Nair34Chandigarh51000

Query:

SELECT * FROM customers ORDER BY Name DESC;

Output:

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

See also  Understanding SQL DROP TABLE Command: A Comprehensive Guide

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:

AIDNAMEREGIONCOMMISSIONPHONESALARY
1Vikram SethiBangalore1%987654321045000
2Priya NandaChennai5%876543210935000
3Aman KapoorMumbai2%765432109860000
4Rhea DesaiPune3%654321098737500
5Nitin JoshiDelhi4%543210987640000
6Anjali SaxenaJaipur8%432109876527000
7Dev SharmaChandigarh6%321098765432000
8Kunal BatraUdaipur2%210987654326000
9Sneha PillaiUjjain9%109876543239000
10Akash VermaGoa7%019876543246000

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.

See also  SQL INSERT STATEMENT


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

🎓 Need Complete Final Year Project?

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

🛒 Visit UpdateGadh Store →
💬 Chat Now