Introduction
In relational database theory, tables are treated as unordered mathematical sets. When you execute SELECT * FROM employees;, the database does not guarantee any specific row order. Rows might arrive in insertion order, disk storage order, or parallel worker order.
To control the order and volume of your output, SQL provides two fundamental clauses:
Controls the sort order (ascending or descending) of returned rows.
Controls the maximum number of rows returned in the result set.
What Is ORDER BY?
The ORDER BY clause sorts query results according to one or more specified columns:
FROM employees
ORDER BY salary;
This sorts all employees by salary from lowest to highest.
ASC — Ascending Order
ASC sorts data from lowest to highest (numbers), A to Z (text), or oldest to newest (dates).
Because ASC is the default direction in mainstream SQL engines (PostgreSQL, MySQL, SQLite, Oracle), both queries below produce identical results:
-- is 100% equivalent to:
SELECT * FROM employees ORDER BY salary ASC;
DESC — Descending Order
The DESC keyword reverses the sort direction, ordering results from highest to lowest:
SELECT name, salary
FROM employees
ORDER BY salary DESC;
Amit (85k), Priya (48k), Vikram (92k), Rahul (62k)
1. Vikram (92k)
2. Amit (85k)
3. Rahul (62k)
4. Priya (48k)
Sorting Text and Dates
ORDER BY sorts text alphabetically and dates chronologically:
SELECT * FROM employees ORDER BY name ASC;
-- Newest orders first (Recent to Oldest)
SELECT * FROM orders ORDER BY order_date DESC;
Sorting by Multiple Columns (Tie-Breaking)
You can separate multiple column sort expressions with commas:
FROM employees
ORDER BY department ASC, salary DESC;
How SQL executes this:
- First, all rows are grouped and sorted by
department ASC(Engineering, HR, Sales). - When two or more rows have the same department (a tie), SQL sorts those specific tied rows by
salary DESC(highest earner in that department first).
[Engineering, Engineering, HR, Sales, Sales]
Engineering: Vikram (92k) before Amit (85k)
Sorting by Expressions and Aliases
ORDER BY can sort by computed expressions or column aliases declared in the SELECT clause:
FROM order_items
ORDER BY total_amount DESC;
What Is LIMIT?
The LIMIT clause restricts the maximum number of rows returned in the final result set:
If the table contains 1,000 products, only 5 rows are sent back to your application, conserving memory and network bandwidth. If fewer than 5 rows exist, all matching rows are returned.
ORDER BY + LIMIT (The Top-N Pattern)
Combining ORDER BY and LIMITis the standard design pattern for answering "Top-N" or "Bottom-N" business questions:
SELECT name, salary
FROM employees
ORDER BY salary DESC
LIMIT 3;
#1 Highest ... #1000 Lowest
Why LIMIT Without ORDER BY Can Be Misleading
Writing SELECT * FROM employees LIMIT 3; does notmean "Get the 3 highest earners" or "Get the 3 newest staff".
ORDER BY, the database simply returns whatever 3 rows happen to be processed first. This result can change between database restarts or server queries! To guarantee deterministic, meaningful results, always pair LIMIT with ORDER BY.LIMIT Combined With WHERE
You can filter subsets of rows before sorting and limiting:
SELECT name, salary
FROM employees
WHERE department = 'Sales'
ORDER BY salary DESC
LIMIT 2;
Understanding Complete SQL Query Structure
| Clause | Syntax Order | Core Purpose |
|---|---|---|
SELECT | 1 | Specifies which columns/expressions to display in output |
FROM | 2 | Specifies the source table(s) to read |
WHERE | 3 | Filters which individual rows qualify |
ORDER BY | 4 | Defines the sorting criteria and direction (ASC/DESC) |
LIMIT | 5 | Slices the maximum row count of the sorted result |
| # Rank | name | department | salary | city |
|---|---|---|---|---|
| #1 | Vikram Singh | Engineering | ₹92,000 | Delhi |
| #2 | Amit Verma | Engineering | ₹85,000 | Bengaluru |
| #3 | Rohan Joshi | Sales | ₹71,000 | Bengaluru |
| #4 | Kavita Nair | Engineering | ₹68,000 | Mumbai |
| #5 | Rahul Sharma | Sales | ₹62,000 | Mumbai |
SELECT * FROM employees ORDER BY salary DESC LIMIT 5;
- Rows are sorted by salary (DESC).
- Only the top 5 sorted records are sliced off and returned.
Common ORDER BY & LIMIT Mistakes
SELECT * FROM sales LIMIT 5 does NOT give you the 5 best sales. Always write ORDER BY amount DESC LIMIT 5.
Running an ORDER BY query changes only the immediate response format, never the physical storage of data.
Writing SELECT * FROM employees LIMIT 5 ORDER BY salary triggers a syntax error. LIMIT must always be placed at the very end of the query.
Practical Step-by-Step Exercises
| Task Goal | Target Table | Required SQL Solution | Pattern Used |
|---|---|---|---|
| 1. Sort by Salary Ascending | employees | SELECT * FROM employees ORDER BY salary ASC; | Basic ASC sort |
| 2. Most Expensive Products First | products | SELECT * FROM products ORDER BY price DESC; | Basic DESC sort |
| 3. Top 5 Highest Salaries | employees | SELECT * FROM employees ORDER BY salary DESC LIMIT 5; | Top-N pattern |
| 4. Top 3 Newest Orders | orders | SELECT * FROM orders ORDER BY order_date DESC LIMIT 3; | Chronological Top-N |
| 5. Multi-Column Tie-Breaker | employees | SELECT * FROM employees ORDER BY department ASC, salary DESC; | Hierarchical sort |
| 6. Top 3 Salaries in Sales | employees | SELECT * FROM employees WHERE department = 'Sales' ORDER BY salary DESC LIMIT 3; | WHERE + ORDER BY + LIMIT |
ORDER BY & LIMIT Best Practices
- Always Specify Sort Direction Explicitly: While ASC is default, writing
ASCorDESCmakes intent obvious to team members. - Always Pair LIMIT with ORDER BY for Top-N: Never write bare LIMIT queries when looking for best/worst/recent items.
- Index Frequently Sorted Columns: In high-scale databases, sorting millions of unindexed rows in memory causes performance lag. Indexes accelerate
ORDER BY ... LIMITqueries drastically. - Keep Syntax Order Strict: Remember the sequence: SELECT ➔ FROM ➔ WHERE ➔ ORDER BY ➔ LIMIT.
What You Should Know Now
- ✓ORDER BY: Sorts query output
- ✓ASC: Low to high / A to Z (Default)
- ✓DESC: High to low / Z to A
- ✓Multiple Columns: Breaks ties hierarchically
- ✓LIMIT: Restricts total rows returned
- ✓Top-N Pattern: ORDER BY + LIMIT combined
🎯 Knowledge Check Quiz: SQL ORDER BY & LIMIT
Test your understanding of result set sorting, ascending vs descending directions, multi-column tie-breaking, and Top-N slicing.