Part 23: Handling NULL Values in Sorting: NULLS FIRST & NULLS LAST
When sorting a column that contains NULL values, ANSI SQL provides explicit placement controls.
1. Syntax
-- Put NULL values at the very bottom regardless of sort order:
SELECT employee_name, commission_pct
FROM sales_reps
ORDER BY commission_pct DESC NULLS LAST;
-- Put NULL values at the top:
SELECT employee_name, commission_pct
FROM sales_reps
ORDER BY commission_pct ASC NULLS FIRST;
Key Takeaways
- Use
NULLS LASTto ensure missing data doesn't clutter top results in descending leaderboards.
In Part 24, we explore Restricting Row Counts: LIMIT and OFFSET!

