Skip to main content

Sorting

Updated Aug 23, 2019 ·

Sample Table

We'll use a films table for th examples.

idtitlerelease_yearcountrydurationlanguagecertificationgrossbudget
1Inception2010USA148EnglishPG-13829895144160000000
2Parasite2019South Korea132KoreanR25759015211400000
3The Godfather1972USA175EnglishR2461209866000000
4Amélie2001France122FrenchR17420000010000000
5Coco2017USA105SpanishPG807082196175000000

Using ORDER BY

In SQL, the ORDER BY keyword sorts the results based on one or more fields. By default, it sorts in ascending order, which means from the smallest to the largest value or alphabetically from A to Z.

  • To sort films by budget from smallest to largest:

    SELECT title, budget
    FROM films
    ORDER BY budget;

    Output:

    titlebudget
    The Godfather6000000
    Parasite11400000
    Amélie10000000
    Inception160000000
    Coco175000000
  • To sort films alphabetically by title:

    SELECT title
    FROM films
    ORDER BY title;

    Output:

    title
    Amélie
    Coco
    Inception
    Parasite
    The Godfather

Ascending

You can explicitly specify ascending order using the ASC keyword, though it's optional since ascending is the default order.

Sorting Titles in Ascending Order:

SELECT title
FROM films
ORDER BY title ASC;

Output:

title
Amélie
Coco
Inception
Parasite
The Godfather

Descending

To sort results in descending order, use the DESC keyword.

  • Sorting by Budget from Largest to Smallest:

    SELECT title, budget
    FROM films
    ORDER BY budget DESC;

    Output:

    titlebudget
    Coco175000000
    Inception160000000
    Amélie10000000
    Parasite11400000
    The Godfather6000000
  • If there were NULL values in the budget field, you could use a WHERE clause to exclude them:

    SELECT title, budget
    FROM films
    WHERE budget IS NOT NULL
    ORDER BY budget DESC;

Sorting Fields

You don’t have to include the field you're sorting by in the SELECT statement, though it can be helpful for clarity.

Sorting by Release Year but Selecting Only Title:

SELECT title
FROM films
ORDER BY release_year;

Output:

title
The Godfather
Amélie
Inception
Coco
Parasite

ORDER BY Multiple Fields

ORDER BY can sort by multiple fields. It first sorts by the first field and then by the subsequent fields if there are ties.

Sorting by Release Year and then Budget:

SELECT title, release_year, budget
FROM films
ORDER BY release_year, budget;

Output:

titlerelease_yearbudget
The Godfather19726000000
Amélie200110000000
Inception2010160000000
Coco2017175000000
Parasite201911400000

Different Orders

You can specify different orders for different fields.

Sorting by Release Year in Ascending and Budget in Descending:

SELECT title, release_year, budget
FROM films
ORDER BY release_year ASC, budget DESC;

Output:

titlerelease_yearbudget
The Godfather19726000000
Amélie200110000000
Inception2010160000000
Coco2017175000000
Parasite2019257590152

Order of Execution

In SQL, ORDER BY is processed after the FROM, WHERE, and SELECT clauses, and just before the LIMIT clause if used.

  • **Order of Execution:

    1. FROM
    2. WHERE
    3. SELECT
    4. ORDER BY
    5. LIMIT