Using the ORDER BY Statement to Sort Query Results

Introduction

The ORDER BY statement allows us to arrange query results in a specific order based on one or more columns. Therefore, we can present data in a more structured manner.

Syntax:

SELECT *
FROM table
ORDER BY column

Here, table refers to the table from which we are fetching data, and column represents the column by which we want to sort the results.

The ORDER BY comes after SELECT and FROM but before LIMIT

Ascending and Descending Order:

DESC can be added to sort in descending order. The default is usually in ascending order(lowest to highest).

Sorting by Multiple Columns:

We can also ORDER BY multiple columns. For example, in the query above the highest number sold was 16 whereby we have several similar rows. We can use years as customer as our second order statement and the rows are now arranged differently. First sorted by nb_sold column then years_as_customer column.

Mixed Order:

The columns can also be sorted in different orders one in descending and the next in ascending. For example:

You can notice the difference between the first and the second example when sorting with years_as_customer column. The first column is sorted in descending order while the second column is in ascending order

NB: When sorting in ascending order the keyword ASC is not necessary. However, using it doesn't affect the result and can be included for clarity.

Differences in the ORDER BY Statement Across Various SQL Flavors

  1. MYSQL:

    The ORDER BY clause can also include the LIMIT clause directly. This allows you to limit the number of rows returned after sorting.

  2. PostgreSQL:

    allows referencing using column positions and not necessarily their names.

  3. Oracle:

    Allows the use of the NULLS FIRST and NULLS LAST clauses within the ORDER BY statement. This specifies whether NULL values should appear first or last in the sorted results.

     SELECT *
     FROM product_sales
     ORDER BY sales NULLS FIRST
    
    1. SQL Server:

      Supports the OFFSET and FETCH clauses for paging purposes. These clauses are often used in conjunction with the ORDER BY clause to implement pagination in query results.

       SELECT employee_name, salary
       FROM employees
       ORDER BY salary DESC
       OFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY;
      
      1. SQLite:

        Allows for the COLLATE keyword, which is used to specify a collation sequence for sorting. This is useful when dealing with internationalization and different character sets.

         SELECT * FROM product_sales
         ORDER BY nb_sold DESC, years_as_customer COLLATE NOCASE
         LIMIT 10;
        
      2. IBM Db2:

        Db2 supports the FETCH FIRST clause, similar to SQL Server's FETCH clause, for limiting the number of rows returned from the result set.

SELECT * FROM product_sales
ORDER BY nb_sold DESC, years_as_customer
FETCH FIRST 3 ROWS ONL;

Further Reading

Udacity SQL course

DataCamp introduction to SQL

Practice writing your queries online