Skip to main content
Module 1 · Lesson 4 of 44/57 in the course~12 min
Module lessons (4/4)

Sorting with ORDER BY

Once FROM and WHERE have selected the rows and SELECT has calculated the final columns, ORDER BY sequences the result before delivering it to the client (and before applying LIMIT).

1. Mental Model: Multi-key and Position of NULLs

DirectionBehavior on numbers / dates / textsWhere do NULL go in PostgreSQL?
ASC (default)From smallest to largest (A → Z, old → recent)At the bottom (NULLS LAST implied)
DESCFrom largest to smallest (Z → A, recent → old)At the beginning (NULLS FIRST implied!)
ORDER BY a, b DESCSort first by a ASC, and if a is equal, by b DESCASC/DESC applies only to the single column!
NULLS LASTForce NULL in queue even with DESCExplicit and portable

2. Progressive & Annotated Examples

SQL
-- Esempio 1: classifica decrescente con tie-breaker alfabetico sul cognome
SELECT first_name, last_name, salary
FROM employees
ORDER BY salary DESC, last_name ASC;
SQL
-- Esempio 2: attenzione! In PostgreSQL department_id DESC mette Andrea (NULL) per PRIMO
SELECT first_name, department_id, salary
FROM employees
ORDER BY department_id DESC, salary DESC;
SQL
-- Esempio 3: forzare i NULL in fondo con NULLS LAST
SELECT first_name, department_id, salary
FROM employees
ORDER BY department_id DESC NULLS LAST, salary DESC;
SQL
-- Esempio 4: ordinare libri combinando filtro WHERE ed elenco di criteri
SELECT title, genre, year, pages
FROM books
WHERE available = TRUE
ORDER BY year DESC, pages ASC;

3. Interactive Laboratory: Position of ORDER BY in the Pipeline

Notice how ORDER BY clicks after WHERE, aggregations and SELECT — and exactly one step before LIMIT:

4. Diagnostic Verification (Quiz)

SQL Quiz#sql.m1.l4.e5
Ready

In PostgreSQL, where will Andrea Costa (who has department_id = NULL) appear when running this query?

SQL
SELECT first_name, department_id FROM employees ORDER BY department_id DESC;
Answer options
SQL Quiz#sql.m1.l4.e6
Ready

Which direction is column department_id sorted in this query?

SQL
SELECT first_name, department_id, salary FROM employees ORDER BY department_id, salary DESC;
Answer options

5. Progressive Practical Laboratory

Warm-Up Stage

Warm-Up: Descending salary ranking

Sort employees from highest paid to lowest paid.

Exercise#sql.m1.l4.e1
Attempts: 0Loading…

Shows last_name and salary of employees, from highest to lowest paid.

Loading editor…
Show hint

Use ORDER BY salary DESC at the end of the query.

Solution available after 3 attempts

Practice Stage

Practice: Multi-column sorting

Sort first by date of hire in ascending order and, in the same case, by surname.

Exercise#sql.m1.l4.e2
Attempts: 0Loading…

Shows last_name, hired_on and salary of all employees, ordered by the most senior in the company (hired_on ascending) and, for the same date, by salary descending.

Loading editor…
Show hint

Specify first hired_on ASC and then salary DESC separated by commas.

Solution available after 3 attempts

Build Stage

Build: Explicit NULL checking

Prevents records without a department from ending up at the top of a descending sort.

Exercise#sql.m1.l4.e3
Attempts: 0Loading…

Shows first_name, department_id and salary sorted by decreasing department_id with NULLs at the bottom (NULLS LAST), and for the same department by decreasing salary.

Loading editor…
Show hint

Add NULLS LAST immediately after department_id DESC.

Solution available after 3 attempts

Practice Stage

Practice: Catalog ordered in the Library

Combine WHERE and ORDER BY on two columns in the books table.

Exercise#sql.m1.l4.e4
Attempts: 0Loading…

Shows title, year and pages of available books (available = TRUE), ordered from the longest (pages descending) and, with the same number of pages, from the oldest to the newest (year ascending).

Loading editor…
Show hint

Add ORDER BY pages DESC, year ASC after WHERE.

Solution available after 3 attempts