Part 22: Multi-Column Sorting & Sorting by Expressions
Sort queries can prioritize multiple columns sequentially.
1. Multi-Column Sorting
-- Sort by Department alphabetically, then by Salary highest to lowest within that department:
SELECT department, last_name, salary
FROM employees
ORDER BY department ASC, salary DESC;
2. Sorting by Expressions and Calculations
SELECT item_name, quantity, unit_price
FROM inventory
ORDER BY (quantity * unit_price) DESC;
Key Takeaways
- Database sorts tie-breaks in the order columns are listed in
ORDER BY. - You can sort directly by calculated mathematical expressions.
In Part 23, we will examine Handling NULL Values in Sorting: NULLS FIRST and NULLS LAST!

