Multi-Column Ordering with NULLS

Demonstrates ORDER BY with multiple columns, mixed sort directions, NULLS handling, and expression-based sorting for precise result ordering.

SqlData Fetching

Code

snippet
SELECT
    employee_id,
    first_name,
    last_name,
    department_id,
    hire_date,
    salary,
    manager_id
FROM employees
ORDER BY
    department_id ASC NULLS LAST,
    salary DESC NULLS FIRST,
    last_name ASC,
    CASE
        WHEN manager_id IS NULL THEN 0
        ELSE 1
    END ASC;

How the snippet works

What runs is SELECT, with employees as the object involved.

Breaking the statements into their working parts: an ORDER BY that fixes the output order.

Applying it

The employees table is the working surface here.

Statements that matter

SELECT
    employee_id,
    first_name,
    last_name,
    department_id,
    hire_date,
    salary,
    manager_id
FROM employees
ORDER BY
    department_id ASC NULLS LAST,
    salary DESC NULLS FIRST,
    last_name ASC,
    CASE
        WHEN manager_id IS NULL THEN 0
        ELSE 1
    END ASC;

Interactive Tools for This Topic

FAQ

Which tables does the SQL touch?
The references are `employees`.