W3Schools SQL Order By DESC Tutorial Sorting Guide

When it comes to SQL, sorting data in descending order is a common requirement. One of the most popular online resources for learning SQL is W3Schools, which provides a simple and effective guide to the 'ORDER BY' clause for achieving this. Let's delve into how you can use 'ORDER BY DESC' in SQL, as explained by W3Schools.

SQL Basics Cheat Sheet for Beginners | Learn SQL Fast ๐Ÿ’ป
SQL Basics Cheat Sheet for Beginners | Learn SQL Fast ๐Ÿ’ป

W3Schools presents SQL in an easy-to-understand format, making it perfect for beginners and experienced professionals alike. Its 'ORDER BY' clause tutorial is no exception, offering a clear pathway to organizing data in your SQL queries.

Learn SQL Fast ๐Ÿš€ | Beginner SQL Commands Cheat Sheet for Data Analysts
Learn SQL Fast ๐Ÿš€ | Beginner SQL Commands Cheat Sheet for Data Analysts

Understanding the ORDER BY Clause

The 'ORDER BY' clause is fundamental in SQL for sorting records. By default, it sorts data in ascending order, but we can change this to descending order using the 'DESC' keyword.

7 SQL Projects for Beginners | Build a Real Data Portfolio
7 SQL Projects for Beginners | Build a Real Data Portfolio

W3Schools demonstrates the basic syntax: 'ORDER BY column_name [ASC/DESC]', where 'ASC' stands for ascending, and 'DESC' for descending. If you omit 'ASC', it defaults to ascending order.

Sorting in Descending Order (DESC)

SQL Cheat Sheet for Beginners
SQL Cheat Sheet for Beginners

The 'DESC' keyword is used to sort the result-set in descending order. For instance,

SELECT * FROM Customers ORDER BY Country DESC;

This will return all customers, sorted by country in descending order, so the countries starting with the letter 'Z' will appear at the beginning.

SQL queries don
SQL queries don

Mixing Ascending and Descending Orders

W3Schools also shows how to use both 'ASC' and 'DESC' in the same 'ORDER BY' clause to mix sorting orders. For example,

SELECT * FROM Orders ORDER BY OrderDate DESC, OrderNo ASC;

SQL Cheatsheet PDF | SQL Commands, Joins & Queries Explained
SQL Cheatsheet PDF | SQL Commands, Joins & Queries Explained

First, this will sort by 'OrderDate' in descending order, and then by 'OrderNo' in ascending order. This is particularly useful when you want to prioritize one criterion over another.

Sorting with NULL Values

SQL in 60 days
SQL in 60 days
SQL Query Execution Order
SQL Query Execution Order
SQL Cheatsheet PDF | SQL Commands, Joins & Queries Explained
SQL Cheatsheet PDF | SQL Commands, Joins & Queries Explained
SQL Execution Order | execution order interview questions
SQL Execution Order | execution order interview questions
SQL Cheatsheet PDF | SQL Commands, Joins & Queries Explained
SQL Cheatsheet PDF | SQL Commands, Joins & Queries Explained
SQL Basics Tutorial for Beginners โ€“ Structured Query Language Commands, Syntax & Examples Guide
SQL Basics Tutorial for Beginners โ€“ Structured Query Language Commands, Syntax & Examples Guide
Sql Cheat Sheet, Learn Sql, Learn Web Development, Excel Tutorials, Sql Server, Career Options, Data Analyst, Data Scientist, Data Analysis
Sql Cheat Sheet, Learn Sql, Learn Web Development, Excel Tutorials, Sql Server, Career Options, Data Analyst, Data Scientist, Data Analysis
SQL Cheatsheet In 2026 ๐Ÿš€
SQL Cheatsheet In 2026 ๐Ÿš€
SQL Excution Order
SQL Excution Order
SQL CASE WHEN Explained with Examples | Beginner SQL Tutorial
SQL CASE WHEN Explained with Examples | Beginner SQL Tutorial
SQL
SQL
SQL Essentials Cheat Sheet | The Only SQL Notes You'll Need for Revision
SQL Essentials Cheat Sheet | The Only SQL Notes You'll Need for Revision
CQF - SQL
CQF - SQL
an image of the server list
an image of the server list
SQL EXECUTION ORDER ๐Ÿ”ฅ
SQL EXECUTION ORDER ๐Ÿ”ฅ
SQL Cheatsheet PDF | SQL Commands, Joins & Queries Explained
SQL Cheatsheet PDF | SQL Commands, Joins & Queries Explained
2.4K views ยท 102 reactions | ๐ŸŽฏ SQL Cheatsheet โ€“ Everything You Need at a Glance Master SQL fundamentals with this clean and powerful SQL Cheatsheet ๐Ÿš€ Perfect for beginners, developers, data analysts, and inte | Codewithprashantt
2.4K views ยท 102 reactions | ๐ŸŽฏ SQL Cheatsheet โ€“ Everything You Need at a Glance Master SQL fundamentals with this clean and powerful SQL Cheatsheet ๐Ÿš€ Perfect for beginners, developers, data analysts, and inte | Codewithprashantt
SQL Cheat Sheet for Beginners | SELECT, JOINS, GROUP BY, Queries & Commands Quick Revision
SQL Cheat Sheet for Beginners | SELECT, JOINS, GROUP BY, Queries & Commands Quick Revision
an image of a table with the names and numbers for different types of data sources
an image of a table with the names and numbers for different types of data sources

W3Schools also covers how to deal with NULL values in your 'ORDER BY' clause. SQL has specific rules for sorting NULL values:

  • In ascending order, NULL values are placed at the end.
  • In descending order, NULL values are placed at the beginning.

To change this behavior, you can use the 'NULLS LAST' and 'NULLS FIRST' keywords, or explicitly handle NULL values in your 'ORDER BY' clause.

Displaying NULL Values First

The 'NULLS FIRST' keyword can be used to place NULL values at the beginning of your sorted data:

SELECT * FROM Customers ORDER BY Country NULLS FIRST;

This will display all NULL values for the 'Country' column first, followed by the rest of the data.

Displaying NULL Values Last

Similarly, the 'NULLS LAST' keyword places NULL values at the end:

SELECT * FROM Customers ORDER BY Country NULLS LAST;

In this case, all non-NULL values are displayed before the NULL values, which is likely more useful in most scenarios.

W3Schools offers a wealth of resources for learning and practicing SQL. Their 'ORDER BY' clause tutorial is an excellent starting point for anyone looking to master SQL sorting. Start exploring W3Schools today to enhance your SQL skills and unlock the full potential of your databases.

Related Articles

Pl Sql W3schools W3schools Oracle Pl Sql What Is Order By 1 2 3 In Sql W3 Sql Null Case When Sql W3 W3 School Sql With Learn Pl Sql W3schools Sql Unpivot W3 Sql Create User W3 W3 Sql View