Master SQL SELECT ORDER BY with Practical Examples

SQL, or Structured Query Language, is a standard language for managing and manipulating databases. One of the most fundamental SQL operations is the SELECT statement, used to retrieve data from one or more tables. A common requirement when using SELECT is to sort the result-set, which is achieved using the ORDER BY clause. Let's dive into how to use ORDER BY with SELECT to sort your SQL results.

SQL Basics Made Easy 💻 | SELECT, WHERE & ORDER BY with Examples
SQL Basics Made Easy 💻 | SELECT, WHERE & ORDER BY with Examples

Before we proceed, it's crucial to understand that ORDER BY sorts the selected data in ascending or descending order based on the specified column(s). By default, it sorts in ascending order (from A-Z or 0-9). If you want to sort in descending order (Z-A or 9-0), you can use the DESC keyword.

SQL ORDER BY Explained with Examples | Sort Data in SQL
SQL ORDER BY Explained with Examples | Sort Data in SQL

Using ORDER BY with SELECT

To use ORDER BY with SELECT, simply append the ORDER BY clause to your SELECT statement. Here's a basic syntax:

SQL ORDER BY clause Explained for Beginners
SQL ORDER BY clause Explained for Beginners

SELECT column_name FROM table_name ORDER BY column_name;

Ascending Order

SQL Basics Cheat Sheet for Beginners | Learn SQL Fast 💻
SQL Basics Cheat Sheet for Beginners | Learn SQL Fast 💻

The following SQL statement retrieves data from the 'employees' table and sorts the result-set in ascending order based on the 'last_name' column:

SELECT * FROM employees ORDER BY last_name;

This will return the employee details sorted from A to Z based on their last names.

SQL Basics Every Beginner Must Learn | Ultimate SQL Cheat Sheet
SQL Basics Every Beginner Must Learn | Ultimate SQL Cheat Sheet

Descending Order

To sort in descending order, use the DESC keyword in the ORDER BY clause:

SELECT * FROM employees ORDER BY last_name DESC;

SQL Query Execution Order Explained | Logical Order of SQL Query Processing
SQL Query Execution Order Explained | Logical Order of SQL Query Processing

This will return the employee details sorted from Z to A based on their last names.

Sorting by Multiple Columns

SQL SELECT Statement Explained for Beginners — Save This
SQL SELECT Statement Explained for Beginners — Save This
SQL Cheat Sheet for Beginners
SQL Cheat Sheet for Beginners
SQL Execution Order Explained — Why SELECT Runs 5th (Cheat Sheet)
SQL Execution Order Explained — Why SELECT Runs 5th (Cheat Sheet)
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 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 queries don
SQL queries don
Learn SQL Fast 🚀 | Beginner SQL Commands Cheat Sheet for Data Analysts
Learn SQL Fast 🚀 | Beginner SQL Commands Cheat Sheet for Data Analysts
SQL Query Cheat Sheet | Beginner SQL Commands, Syntax & Examples
SQL Query Cheat Sheet | Beginner SQL Commands, Syntax & Examples
SQL SELECT Statement | Syntax, Examples & Queries
SQL SELECT Statement | Syntax, Examples & Queries
SQL SELECT Explained with Examples | Beginner SQL Guide
SQL SELECT Explained with Examples | Beginner SQL Guide
SQL Cheatsheet PDF | SQL Commands, Joins & Queries Explained
SQL Cheatsheet PDF | SQL Commands, Joins & Queries Explained
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 Query Execution Order
SQL Query Execution Order
SQL Cheatsheet PDF | SQL Commands, Joins & Queries Explained
SQL Cheatsheet PDF | SQL Commands, Joins & Queries Explained
SQL GROUP BY Explained Visually (Beginner-Friendly Guide)
SQL GROUP BY Explained Visually (Beginner-Friendly Guide)
SQL ORDER BY Explained
SQL ORDER BY Explained
SQL Cheatsheet PDF | SQL Commands, Joins & Queries Explained
SQL Cheatsheet PDF | SQL Commands, Joins & Queries Explained

Sometimes, you might want to sort your results based on multiple columns. You can do this by separating the column names with commas in the ORDER BY clause:

Sorting by Two Columns

The following statement sorts the 'employees' table first by 'last_name' and then by 'first_name' in ascending order:

SELECT * FROM employees ORDER BY last_name, first_name;

Sorting with Combined Ascending and Descending Order

You can also sort by one column in ascending order and another in descending order:

SELECT * FROM employees ORDER BY last_name DESC, first_name;

NULL Values in ORDER BY

By default, NULL values are placed at the beginning when sorting in ascending order and at the end when sorting in descending order. This behavior can be changed using the NULLS FIRST or NULLS LAST options:

Sorting NULL Values First

SELECT * FROM employees ORDER BY last_name NULLS FIRST;

Sorting NULL Values Last

SELECT * FROM employees ORDER BY last_name NULLS LAST;

Practicing and exploring these SQL SELECT and ORDER BY examples will help you master sorting in no time. Happy coding!

Related Articles

W3 School Sql Pivot Instr In Oracle Sql W3schools W3 Sql Not Sql W3 With Sql Variables W3 Sql While W3 Sql W3b W3 Order By Sql Create User W3 W3 Sql Using