ORDER BY Clause
Question
What does ORDER BY do?
Hint
ORDER BY sorts ascending (ASC) by default; DESC for descending.
Explanation
ORDER BY sorts the result set by one or more columns. SELECT * FROM users ORDER BY name ASC sorts alphabetically. ORDER BY age DESC sorts from oldest to youngest. Multiple columns: ORDER BY last_name ASC, first_name ASC. NULL sorting varies by database (some put NULLs first, some last). ORDER BY can use column aliases and expressions. ORDER BY is applied after SELECT, WHERE, GROUP BY, and HAVING. For large result sets, ORDER BY can be expensive (requires sorting).
Related Resources
Related Lessons
- SELECT
Learn SELECT.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.