SQL ORDER BY Function
Quick summary: The SQL ORDER BY clause sorts query results.
SQL ORDER BY Syntax
SELECT columns FROM table_name ORDER BY column1 [ASC|DESC], column2 [ASC|DESC]
SQL
SQL ORDER BY Basic examples
SELECT * FROM users ORDER BY created_at DESC;
SQL
Output:
Newest users first
Sorts rows in descending order.
SQL ORDER BY Real-world usage
SELECT * FROM products ORDER BY price ASC;
SQL
Output:
Cheapest products first
Sorts numeric columns.
SQL ORDER BY Edge cases
SELECT * FROM users ORDER BY email;
SQL
Output:
Alphabetical order
Default sort order is ascending.
SQL ORDER BY Common mistakes
Sorting without indexes
ORDER BY on large tables can be slow.
Incorrect
ORDER BY created_at
Correct
ORDER BY created_at /* indexed */
Index columns used for sorting.
SQL ORDER BY Frequently Asked Questions
What does ORDER BY do in SQL?
Sorts query results.
Default sort order?
Ascending (ASC).
Can sort descending?
Yes, using DESC.
Can sort by multiple columns?
Yes.
Use case?
Sorting results for display.
Common mistake?
Sorting large datasets without indexes.
Performance impact?
Can be expensive.
Can use expressions?
Yes.
Can use column index?
Yes (not recommended).
Used with LIMIT?
Yes.
Stable sorting?
Depends on DB.
Best practice?
Index sorted columns.