Oracle ORDER BY Clause 📶
Mentor's Note: Data in a table is like a pile of laundry.
ORDER BYis the process of folding it and putting it into neat drawers. Without this clause, Oracle does not guarantee any specific order! 💡
🌟 The Scenario: The Leaderboard 🏆
You are managing a sports academy.
- Goal: You want to see the top scorers first.
- Action: You sort the list by
scorein Descending order.
💻 1. Basic Syntax
SELECT column_name
FROM table_name
ORDER BY column_name [ASC | DESC];
Example: Sort by Salary (High to Low)
SELECT first_name, salary
FROM employees
ORDER BY salary DESC;
🏗️ Architect's Note: Nulls & Performance 🛡️
- NULLS FIRST/LAST: In Oracle, NULLs are treated as the "Largest" values. By default, they appear at the bottom in ASC order. You can change this!
ORDER BY commission_pct DESC NULLS LAST;
- Expensive Sorts: Sorting is one of the most resource-intensive operations in Oracle. It uses the
TEMPtablespace. Avoid sorting millions of rows if you only need the Top 10!