Multi-Column Ordering with NULLS
Demonstrates ORDER BY with multiple columns, mixed sort directions, NULLS handling, and expression-based sorting for precise result ordering.
Code
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
CSV Column AnalyzerProfile every CSV column with inferred types, empty-cell counts, unique values, and numeric min and max ranges.JSON SorterSort JSON object keys by code point ascending or descending, recursively or top-level only, preserving array order.SemVer SorterParse and sort Semantic Version 2.0.0 strings with prerelease and build metadata, respecting prerelease-below-release precedence.
FAQ
- Which tables does the SQL touch?
- The references are `employees`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.