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
| Direction | Behavior on numbers / dates / texts | Where do NULL go in PostgreSQL? |
|---|---|---|
ASC (default) | From smallest to largest (A → Z, old → recent) | At the bottom (NULLS LAST implied) |
DESC | From largest to smallest (Z → A, recent → old) | At the beginning (NULLS FIRST implied!) |
ORDER BY a, b DESC | Sort first by a ASC, and if a is equal, by b DESC | ASC/DESC applies only to the single column! |
NULLS LAST | Force NULL in queue even with DESC | Explicit and portable |
2. Progressive & Annotated Examples
-- 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;-- 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;-- 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;-- 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)
In PostgreSQL, where will Andrea Costa (who has department_id = NULL) appear when running this query?
SELECT first_name, department_id FROM employees ORDER BY department_id DESC;Which direction is column department_id sorted in this query?
SELECT first_name, department_id, salary FROM employees ORDER BY department_id, salary DESC;5. Progressive Practical Laboratory
Warm-Up: Descending salary ranking
Sort employees from highest paid to lowest paid.
Shows last_name and salary of employees, from highest to lowest paid.
Show hint
Use ORDER BY salary DESC at the end of the query.
Solution available after 3 attempts
Practice: Multi-column sorting
Sort first by date of hire in ascending order and, in the same case, by surname.
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.
Show hint
Specify first hired_on ASC and then salary DESC separated by commas.
Solution available after 3 attempts
Build: Explicit NULL checking
Prevents records without a department from ending up at the top of a descending sort.
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.
Show hint
Add NULLS LAST immediately after department_id DESC.
Solution available after 3 attempts
Practice: Catalog ordered in the Library
Combine WHERE and ORDER BY on two columns in the books table.
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).
Show hint
Add ORDER BY pages DESC, year ASC after WHERE.
Solution available after 3 attempts