SQL Descending Sorting - Complete Beginner Tutorial
Quick Answer
In SQL, descending sorting is achieved by using the ORDER BY clause followed by the DESC keyword. This sorts the query results from highest to lowest based on the specified column(s).
Learning Objectives
- Understand how to sort SQL query results in descending order.
- Learn to use the ORDER BY clause with DESC keyword.
- Apply descending sorting to multiple columns in SQL queries.
Introduction
Sorting data is a fundamental operation in SQL to organize query results.
Descending sorting arranges data from the highest value to the lowest.
This tutorial explains how to use descending sorting with practical examples.
Sorting data effectively helps reveal meaningful insights.
What is Descending Sorting in SQL?
Descending sorting means ordering data from the largest to the smallest value.
In SQL, this is done using the ORDER BY clause with the DESC keyword.
- By default, ORDER BY sorts data in ascending order (lowest to highest).
- Adding DESC reverses this order to descending.
- You can sort by one or multiple columns.
Using ORDER BY DESC Clause
The syntax to sort data in descending order is straightforward.
You specify the column to sort by, followed by DESC.
- Example syntax: SELECT * FROM table_name ORDER BY column_name DESC;
- This returns all rows sorted from highest to lowest based on column_name.
Example: Sorting Employees by Salary Descending
Suppose you have an employees table with a salary column.
To get employees ordered by highest salary first, use ORDER BY salary DESC.
Sorting by Multiple Columns
You can sort by more than one column to refine the order.
Each column can have its own sorting direction.
- Syntax: ORDER BY column1 DESC, column2 ASC;
- This sorts first by column1 descending, then by column2 ascending.
Example: Sort by Department Descending and Name Ascending
To sort employees by department in descending order and then by name ascending:
ORDER BY department DESC, name ASC
Practical Example
This query retrieves all products sorted by price from highest to lowest.
This query sorts employees by department in descending order and then by last name ascending.
Examples
SELECT * FROM products ORDER BY price DESC;This query retrieves all products sorted by price from highest to lowest.
SELECT * FROM employees ORDER BY department DESC, last_name ASC;This query sorts employees by department in descending order and then by last name ascending.
Best Practices
- Always specify the sorting direction explicitly for clarity.
- Use descending sorting when you want to see highest or latest values first.
- Combine ascending and descending sorts for multi-column ordering as needed.
Common Mistakes
- Forgetting to add DESC and getting ascending order instead.
- Assuming ORDER BY sorts descending by default.
- Not specifying sorting direction when sorting by multiple columns.
Hands-on Exercise
Sort Customers by Registration Date Descending
Write a SQL query to list customers ordered by their registration date from newest to oldest.
Expected output: Customers listed starting with the most recently registered.
Hint: Use ORDER BY registration_date DESC.
Sort Products by Category Descending and Price Ascending
Write a SQL query to sort products first by category descending, then by price ascending.
Expected output: Products ordered by category from Z to A, and within each category by price low to high.
Hint: Use ORDER BY category DESC, price ASC.
Interview Questions
How do you sort query results in descending order in SQL?
InterviewUse the ORDER BY clause followed by the column name and the DESC keyword, for example: ORDER BY column_name DESC.
Can you sort by multiple columns with different sort directions?
InterviewYes, you can specify each column with ASC or DESC, like ORDER BY column1 DESC, column2 ASC.
What is the default sorting order if you omit ASC or DESC?
InterviewThe default sorting order is ascending (ASC) if no direction is specified.
MCQ Quiz
1. What is the best first step when learning Descending Sorting?
A. Understand the purpose and basic idea
B. Skip directly to advanced implementation
C. Ignore examples and practice
D. Memorize terms without context
Correct answer: A
Starting with the purpose and basic idea makes later examples and practice easier to understand.
2. Which activity helps reinforce Descending Sorting?
A. Reading once without practice
B. Building or writing a small practical example
C. Avoiding review questions
D. Skipping the summary
Correct answer: B
A small practical example helps connect the topic to real usage.
3. Which statement is most accurate about this topic?
A. In SQL, descending sorting is achieved by using the ORDER BY clause followed by the DESC keyword.
B. Descending Sorting never needs examples
C. Descending Sorting is unrelated to practical work
D. Descending Sorting should be learned without checking results
Correct answer: A
The correct option is based on the available topic explanation.
Key Takeaways
- Descending sorting orders data from highest to lowest.
- Use ORDER BY column_name DESC to sort in descending order.
- Multiple columns can be sorted with mixed ascending and descending orders.
- In SQL, descending sorting is achieved by using the ORDER BY clause followed by the DESC keyword.
- This sorts the query results from highest to lowest based on the specified column(s).
Summary
Descending sorting in SQL is done using ORDER BY with the DESC keyword.
It orders data from highest to lowest based on the specified column.
You can sort by multiple columns with different directions to organize data effectively.
Frequently Asked Questions
What does DESC mean in SQL?
DESC stands for descending and sorts data from highest to lowest.
Is ascending the default sort order in SQL?
Yes, if you do not specify ASC or DESC, SQL sorts data in ascending order by default.
Can I sort by multiple columns in descending order?
Yes, you can specify DESC for each column individually in the ORDER BY clause.
Does ORDER BY affect the data stored in the table?
No, ORDER BY only affects the order of rows returned by the query, not the stored data.





