The ORDER BY clause is used to sort query results in ascending (ASC) or descending (DESC) order.
By default, SQL sorts data in ascending order.
Without ORDER BY, SQL does not guarantee the order of returned rows.
Sorting is important for:
SELECT column_name
FROM table_name
ORDER BY column_name;
Ascending Order:
SELECT *
FROM employes
ORDER BY emp_name ASC;
Descending Order:
SELECT *
FROM employes
ORDER BY emp_name DESC;
| Column | Description |
|---|---|
| emp_id | Employee ID |
| emp_name | Employee Name |
| dept_id | Department ID |
| manager_id | Manager ID |
ORDER BY runs near the end of logical execution order — after SELECT has already chosen the output columns, and right before LIMIT cuts the result down.
flowchart LR
A[SELECT: columns projected] --> B[ORDER BY: rows sorted]
B --> C[LIMIT: rows cut]
style B fill:#22863a,color:#ffffff
NULLs sort?This is the single most portability-breaking behavior in ORDER BY, and this module’s own dataset has NULL values in manager_id — sorting by manager_id will actually surface this difference, not just describe it hypothetically.
| Engine | Default position of NULL in ASC order |
|---|---|
| PostgreSQL | Last |
| Oracle | Last |
| MySQL | First |
| SQL Server | First |
Make it explicit instead of relying on the default:
-- PostgreSQL / Oracle — supported directly
SELECT *
FROM employes
ORDER BY manager_id ASC NULLS LAST;
-- MySQL / SQL Server — no NULLS LAST keyword; force it with a CASE
SELECT *
FROM employes
ORDER BY (manager_id IS NULL), manager_id ASC;
| emp_id | emp_name | dept_id |
|---|---|---|
| 1 | Ammar | 1 |
| 2 | Riya | 2 |
| 3 | Sahil | 1 |
| 4 | Priya | 3 |
| 5 | Arjun | 2 |
SELECT *
FROM employes
ORDER BY emp_name;
Ammar
Arjun
Priya
Riya
Sahil
SELECT *
FROM employes
ORDER BY emp_id DESC;
5 Arjun
4 Priya
3 Sahil
2 Riya
1 Ammar
SELECT *
FROM employes
ORDER BY dept_id;
SELECT *
FROM employes
ORDER BY dept_id, emp_name;
SQL first sorts by department and then alphabetically within each department.
Show newest employees first.
ORDER BY joining_date DESC
Show highest revenue first.
ORDER BY revenue DESC
Show top spending customers.
ORDER BY total_spent DESC
Show departments by employee count.
ORDER BY employee_count DESC
Assuming SQL automatically returns sorted data.
❌ Wrong Thinking
SELECT *
FROM employes;
Result order is not guaranteed.
✅ Correct
SELECT *
FROM employes
ORDER BY emp_name;
Using LIMIT before ORDER BY.
❌ Wrong
SELECT *
FROM employes
LIMIT 3
ORDER BY emp_name;
✅ Correct
SELECT *
FROM employes
ORDER BY emp_name
LIMIT 3;
SELECT — SELECT emp_name FROM employes ORDER BY dept_id; is valid in most engines even though dept_id isn’t in the output, because ORDER BY has access to the full row, not just the projected columns. Some engines restrict this when DISTINCT is also used, since deduplication happens before a column not in the output could be used to sort.NULLs — see Dialect Differences above; don’t assume a default without checking your engine if the sort order of NULLs matters to the result.SQL executes queries in this order:
ASC = Small → Large
ORDER BY emp_id ASC;
DESC = Large → Small
ORDER BY emp_id DESC;
GLOSSARY.md — “collation” and “deterministic” definitionsFAQ.md — “why does my query return rows in a different order every time?”INTERVIEW_PREP.md — see “Does ORDER BY put NULLs first or last?”