SQL Tutorial

SQL Tutorial SQL Introduction SQL Syntax SQL Data Types SQL OPERATORS SQL COMMANDS SQL Queries What are single row and multiple row subqueries? SQL Union Clause

SQL Database

SQL Create Database SQL DROP Database SQL SELECT Database

SQL Table

SQL TABLE SQL CREATE TABLE SQL COPY TABLE SQL ALTER TABLE SQL DELETE SQL TRUNCATE TABLE SQL DROP TABLE SQL UPDATE TABLE SQL INSERT TABLE

SQL SELECT

SQL SELECT Statement SQL SELECT WHERE Clause SQL SELECT IN Operator SQL BETWEEN Operator SQL SELECT BETWEEN Operator SQL SELECT AND Operator SQL SELECT OR Operator SQL SELECT LIKE Operator SQL SELECT DISTINCT SQL SELECT SUM SQL SELECT MAX SQL SELECT MIN SQL SELECT AVG

SQL Clause

SQL WHERE Clause SQL GROUP BY CLAUSE SQL ORDER BY Clause SQL HAVING Clause

SQL INSERT

SQL INSERT Statement SQL INSERT INTO Statement SQL INSERT INTO Values SQL INSERT INTO SELECT SQL Insert multiple rows

SQL JOIN

SQL JOIN SQL Inner Join SQL Left Join SQL Right Join SQL Full Join SQL CROSS Join

SQL OPERATOR

SQL Comparison SQL LOGICAL Operator SQL Cast Operator SQL Arithmetic

Difference

SQL vs NOSQL WHERE vs HAVING DELETE vs DROP GROUP BY vs ORDER BY DROP vs TRUNCATE SQL IN vs SQL EXISTS Difference between Delete, Drop and Truncate in SQL

MISC

SQL SubQuery SQL CASE Commit and Rollback in SQL Pattern Matching in SQL DDL Commands in SQL DML Commands in SQL Types of SQL Commands SQL COUNT SQL Primary Key SQL FOREIGN KEY SET Operators in SQL Check Constraint in SQL SQL EXCEPT SQL VIEW SQL WHERE Statement SQL CRUD Operation Where Condition in SQL TCL Commands in SQL Types of SQL JOINS SQL Nth Highest Salary SQL NOT OPERATOR SQL UNION ALL SQL INTERSECT SQL Data Definition Language SQL Data Manipulation Language SQL Data Control Language SQL CONSTRAINTS SQL Aggregate Operators SQL KEYS Codd’s Rules in SQL What is SQL Injection? Trigger In SQL SQL WHERE Multiple Conditions Truncate function in SQL SQL Formatter WEB SQL SQL Auto Increment Save Point in SQL space() function in SQL SQL Aggregate Functions SQL Topological Sorting SQL Injection SQL Cloning Tables SQL Aliases SQL Handling Duplicate Update Query in SQL Grant Command in SQL SQL SET Keyword SQL Order BY LIMIT SQL Order BY RANDOM

How To

How to use the BETWEEN operator in SQL How To Use INNER JOIN In SQL How to use LIKE in SQL How to use HAVING Clause in SQL How to use GROUP BY Clause in SQL How To Remove Duplicates In SQL How To Delete A Row In SQL How to add column in table in SQL ? How to drop a column in SQL? How to create a database in SQL? How to use COUNT in SQL? How to Create Temporary Table in SQL? How to Add Foreign Key in SQL? How to Add Comments in SQL? How To Use Group By Clause In SQL How To Use Having Clause In SQL How To Delete Column In Table How To Compare Date In SQL How index works in SQL How to calculate age from Date of Birth in SQL How to Rename Column name in SQL How to get current year in SQL server 2012? User Input in PL/SQL

SQL ORDER BY DESC

SQL stands for Structured Query Language, and it is used to handle data in RDBMS (relational databases). Several relational database management systems use it as a common language to communicate with the data contained in the database.

The capacity to sort data stored in a database based on numerous criteria is a core feature of SQL. In SQL, the "ORDER BY" phrase serves to sort data in ascending or descending order. In this post, we'll look at the "ORDER BY DESC" clause and see how it operates through examples.

SQL ORDER BY Clause

The SQL ORDER BY phrase can be used to arrange the SELECT query results. The SELECT statement is used to extract information from one or more database tables. The ORDER BY phrase arranges the data depending on one or more table columns.
The data is sorted in ascending order by default, but it may be sorted in descending order by using the "DESC" keyword.

The following is the syntax for the ORDER BY clause:

SELECT column1, column2, column3 FROM table_name ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], column3 [ASC | DESC];

The "SELECT" expression in the preceding syntax pulls data from the table "table name," and the "ORDER BY" clause arranges the data depending on the columns supplied in the SELECT statement. The "ASC" keyword can be used to sort data ascendingly, whereas the "DESC" keyword can be used to sort data descendingly.

SQL ORDER BY DESC

In the SQL ORDER BY clause, the "DESC" keyword can be used to organise the data in descending order. In other words, the data is arranged in reverse order, with the greatest values displayed initially and the lowest values displayed last.

The SQL ORDER BY DESC clause is written as follows:

SELECT column1, column2, column3 FROM table_name ORDER BY column1 DESC, column2 DESC, column3 DESC;

In the above syntax, the data is ordered in decreasing order depending on the columns supplied in the SELECT query.

SQL ORDER BY DESC Examples

Consider the following table, "Employees" to demonstrate how the SQL ORDER BY DESC clause operates using examples.

Employee_IdEmployee_NameEmployee_SalaryEmployee_Age
1Rahul Sharma5000028
2Priya Patel7500035
3Rajesh Singh6500042
4Anjali Gupta8000029
5Sanjay Mehta6000031
6Nisha Shah5500026
7Aakash Verma7000039
8Kavita Reddy8500033
9Rohit Kumar4500024
10Sunita Rao9000037

Case 1: Sorting the data based on the "Employee_Salary" column in order of decreasing

The SQL query sorts the data in decreasing order using the "Employee_Salary" column.

Query:

SELECT Employee_Id, Employee_Name, Employee_Age, Employee_Salary 
FROM Employees 
ORDER BY Employee_Salary DESC;

Output:

The following is the result of the mentioned SQL statement:

SQL ORDER BY DESC

Case 2: Sorting data in decreasing order based on numerous columns

The SQL query below arranges the data in decreasing order using the "Employee_Salary" and "Employee_Age" columns.

Query:

SELECT Employee_ID, Employee¬_Name, Employee_Age, Employee_Salary 
FROM Employees 
ORDER BY Employee_Salary DESC, Employee_Age DESC

Output:

The following is the result of the above SQL statement:

SQL ORDER BY DESC

In the above case, the data is first sorted in decreasing order by the "Employee_Salary" column. If there are numerous employees who have the same income, the data is sorted in decreasing order based on the "Employee_Age" column.

Case 3: Sorting data in descending order based on different data types in columns

In some cases, the data in a database table may include columns of varying data types. Take the "Student_data" table, which includes student data with columns "Student_id" (integer data type), "Student_name" (string data type), "Student_age" (integer data type), "Student_grade" (integer data type), and "Student_city" (string data type).

Student_idStudent_nameStudent_ageStudent_gradeStudent_city
1Emma Johnson17ANew York
2Oliver Williams16BLos Angeles
3Ava Davis16BChicago
4William Wilson15CHouston
5Sophia Jackson18APhoenix
6James Thompson17BPhiladelphia
7Isabella Brown15CSan Antonio
8Michael Martinez16BSan Diego
9Charlotte Garcia15CDallas
10Ethan Rodriguez17ASan Francisco

The SQL query sorts the data in decreasing order using the "Student_grade" column:

Query:

SELECT student_id, student_name, student_age, student_grade, student_city 
FROM student_data 
ORDER BY student_grade DESC;

Output:

The following is the result of the mentioned SQL statement:

SQL ORDER BY DESC

In this case, the data is ordered descendingly by the "Student_grades" column. The student with the highest grade point average (GPA) appears first, while the student with the lowest grade point average (GPA) appears last.

Case 4: Sorting data in decreasing order based on NULL values in columns

The data in a database table may contain NULL values in various instances. In such circumstances, we may sort the data in descending order by using the SQL IS NULL and IS NOT NULL operators.

Consider the table "Employees" which contains employee data with columns "Employee_id" (integer data type),

"Employee_name" (string data type),

"Employee_age" (integer data type),

"Employee_salary" (integer data type),

and "Employee_position" (string data type).

Employee_IdEmployee_NameEmployee_SalaryEmployee_AgeEmployee_Position
1Rahul Sharma5000028Manager
2Priya Patel7500035Senior Engineer
3Rajesh Singh6500042Marketing Executive
4Anjali Gupta8000029Software Developer
5Sanjay Mehta6000031Product Manager
6Nisha Shah5500026Sales Executive
7Aakash Verma7000039Account Manager
8Kavita Reddy8500033Lead Engineer
9Rohit Kumar4500024Operations Manager
10Sunita Rao9000037Human Resource Manager


The below SQL query arranges the data in decreasing order using the "employee salary" column:

SELECT employee_id, employee_name, employee_age, employee_salary, employee_position 
FROM employees
WHERE employee_salary IS NOT NULL 
ORDER BY employee_salary DESC;

Output:

The following is the result of the mentioned SQL statement:

SQL ORDER BY DESC

The data is sorted in decreasing order in this case based on the "Employee_salary" column. The employee with the greatest income is displayed first, followed by the employee with the lowest salary. To exclude NULL data from the sorting process, the SQL IS NOT NULL operator is utilised.

Case 5: Sorting data in decreasing order based on date data type columns

Date columns may exist in a database table in some instances. In such circumstances, we can sort the data in decreasing order using the SQL DATE data type.

Consider the "Order_data" table, which includes order data with the columns "Order_id" (integer data type), "Order_date" (date data type), "Order_amount" (integer data type), and "Customer_Id" (string data type).

Order_IdOrder_DateOrder_AmountCustomer_Id
12022-02-151350.75102
22022-01-05542.90125
32022-03-02998.50114
42022-02-18720.00108
52022-03-15285.30132
62022-01-251675.25107
72022-02-10890.40119
82022-03-09421.60129
92022-01-20599.99101
102022-02-281750.00112

The SQL query below sorts the data in decreasing order using the "Order_date" column:

Query:

SELECT order_id, order_date, order_amount, customer_id
FROM order_data 
ORDER BY order_date DESC;

Output:

The following is the result of the mentioned SQL statement:

SQL ORDER BY DESC

 In this case, the data is ordered descendingly by the "Order_date" column. The most current order shows first, followed by the oldest order.

Conclusion

In SQL, the ORDER BY DESC clause is used to sort data in descending order. The data is sorted in reverse order, with the highest values first and the lowest values last.

To obtain and sort data from a database, use the SQL ORDER BY DESC clause in conjunction with the SELECT command.

The data can be sorted by one or even more columns, and it can be sorted in decreasing order.

Understanding the SQL ORDER BY DESC clause and how it works is critical for properly managing data in a relational database.