πŸŽ‰ 75% of content is free forever β€” Unlock Premium from $10/mo β†’
CW
Search courses…
πŸ’Ό Servicesℹ️ Aboutβœ‰οΈ ContactView Pricing Plansfrom $10

ORDER BY Clause

SQL FundamentalsDML🟒 Free Lesson

Advertisement

SQL Fundamentals

The ORDER BY Clause

Put your data in order β€” alphabetically, numerically, or chronologically.

  • Ascending & Descending β€” control sort direction with ASC and DESC
  • Multi-Column Sort β€” break ties with secondary sort columns
  • Custom Ordering β€” use CASE for non-standard sort orders

ORDER BY transforms raw data into meaningful, organized results.

What is ORDER BY?

DfORDER BY

A SQL clause that sorts the result set by one or more columns. Results can be sorted in ascending (default) or descending order.

ASC vs DESC SortingORDER BY salary ASCsalary$54,000$62,000$75,000$85,000$91,000Low to High (Default)ORDER BY salary DESCsalary$91,000$85,000$75,000$62,000$54,000High to LowReversed
SELECT column1, column2
FROM table_name
ORDER BY column1 ASC|DESC;
ComponentDescriptionExample
ORDER BYKeywordRequired
column1Column to sort bysalary
ASCAscending order (default)A→Z, 1→10
DESCDescending orderZ→A, 10→1

Ascending Order (Default)

-- ASC is optional (default behavior)
SELECT first_name, last_name, salary
FROM employees
ORDER BY salary;

-- Explicit ASC (same result)
SELECT first_name, last_name, salary
FROM employees
ORDER BY salary ASC;
first_namelast_namesalary
DanWilson54000
BobSmith62000
AliceJohnson75000
CarolWilliams85000
EveBrown91000

Descending Order

-- DESC for highest to lowest
SELECT first_name, last_name, salary
FROM employees
ORDER BY salary DESC;
first_namelast_namesalary
EveBrown91000
CarolWilliams85000
AliceJohnson75000
BobSmith62000
DanWilson54000

Sort by Multiple Columns

DfMulti-Column Sort

When sorting by multiple columns, the first column determines the primary order. Rows with identical values in the first column are then sorted by the second column, and so on.

-- Sort by department, then salary within department
SELECT department, first_name, salary
FROM employees
ORDER BY department ASC, salary DESC;
departmentfirst_namesalary
EngineeringEve91000
EngineeringCarol85000
EngineeringAlice75000
MarketingBob62000
MarketingDan54000

You can mix ASC and DESC in multi-column sorts. Each column has its own direction independent of the others.

Sort by Column Position

-- Sort by the 3rd column in SELECT list
SELECT first_name, last_name, salary
FROM employees
ORDER BY 3 DESC;

Using column positions in ORDER BY makes code harder to maintain. If you change the SELECT list, the sort may break. Always use column names instead.

Sort with Expressions

-- Sort by computed value
SELECT name, price, stock
FROM products
ORDER BY price * stock DESC;

-- Sort by string length
SELECT first_name, last_name
FROM employees
ORDER BY LENGTH(last_name) DESC;

-- Sort by date difference
SELECT first_name, hire_date
FROM employees
ORDER BY DATEDIFF(CURRENT_DATE, hire_date) DESC;

Sort with Aliases

-- Use alias in ORDER BY
SELECT
    first_name,
    last_name,
    salary * 12 AS annual_salary
FROM employees
ORDER BY annual_salary DESC;
first_namelast_nameannual_salary
EveBrown1092000
CarolWilliams1020000
AliceJohnson900000

Sort with NULL

DfNULL Sorting

NULL values sort differently depending on the database. In most systems, NULLs appear first in ASC order and last in DESC order. PostgreSQL provides explicit NULLS FIRST and NULLS LAST options.

-- PostgreSQL: Explicit NULL handling
SELECT first_name, email
FROM customers
ORDER BY email NULLS FIRST;

SELECT first_name, email
FROM customers
ORDER BY email NULLS LAST;
DatabaseDefault Behavior
MySQLNULLs sort first in ASC, last in DESC
PostgreSQLConfigurable with NULLS FIRST/LAST
SQL ServerNULLs sort first in ASC, last in DESC
OracleNULLs sort last in ASC, first in DESC

CASE in ORDER BY

-- Custom sort order
SELECT first_name, last_name, department
FROM employees
ORDER BY
    CASE department
        WHEN 'Executive' THEN 1
        WHEN 'Engineering' THEN 2
        WHEN 'Marketing' THEN 3
        ELSE 4
    END;

-- Sort by priority
SELECT task_name, priority
FROM tasks
ORDER BY
    CASE priority
        WHEN 'high' THEN 1
        WHEN 'medium' THEN 2
        WHEN 'low' THEN 3
    END;

Use CASE in ORDER BY when you need a non-alphabetical, non-numeric sort order β€” like sorting by priority, status, or custom categories.

ORDER BY with LIMIT

-- Top 5 highest salaries
SELECT first_name, last_name, salary
FROM employees
ORDER BY salary DESC
LIMIT 5;

-- Bottom 3 products by price
SELECT name, price
FROM products
ORDER BY price ASC
LIMIT 3;

-- Most recent orders
SELECT *
FROM orders
ORDER BY order_date DESC
LIMIT 10;

Performance Tips

TipDescriptionImpact
Add LIMITSort fewer rowsHigh
Index sort columnAvoid filesortHigh
Avoid SELECT *Less data to sortMedium
Avoid expressionsCan't use indexMedium
Use covering indexAll data in indexHigh
-- Without index: Full sort needed
SELECT * FROM orders
ORDER BY order_date DESC
LIMIT 10;

-- With index: Already sorted
CREATE INDEX idx_orders_date ON orders(order_date);
SELECT * FROM orders
ORDER BY order_date DESC
LIMIT 10;

The ORDER BY clause is evaluated after SELECT, so you can reference column aliases defined in the SELECT list. However, avoid expressions in ORDER BY when possible β€” they prevent index usage.

Common Mistakes

-- BAD: Using column position
SELECT first_name, last_name, salary
FROM employees
ORDER BY 3 DESC;

-- GOOD: Using column name
SELECT first_name, last_name, salary
FROM employees
ORDER BY salary DESC;

-- BAD: ORDER BY without LIMIT for large tables
SELECT * FROM logs
ORDER BY created_at DESC;

-- GOOD: Add LIMIT
SELECT * FROM logs
ORDER BY created_at DESC
LIMIT 1000;

-- BAD: ORDER BY on multiple columns without clear need
SELECT * FROM products
ORDER BY name, price, stock, category, created_at;

-- GOOD: Only necessary columns
SELECT * FROM products
ORDER BY name;

Practice Exercises

Exercise 1: Sort employees by hire_date from newest to oldest.

SELECT * FROM employees
ORDER BY hire_date DESC;

Exercise 2: Sort products by price (lowest first), then by name alphabetically for same price.

SELECT name, price, category
FROM products
ORDER BY price ASC, name ASC;

Exercise 3: Find the 3 most recently hired employees.

SELECT first_name, last_name, hire_date
FROM employees
ORDER BY hire_date DESC
LIMIT 3;

Exercise 4: Sort customers by city alphabetically, then by last name within each city.

SELECT first_name, last_name, city
FROM customers
ORDER BY city ASC, last_name ASC;

Key Takeaways

  1. ORDER BY sorts your results β€” ascending (ASC) by default
  2. Use DESC for descending order β€” highest to lowest, Z to A
  3. Sort by multiple columns for tie-breaking
  4. Always use column names, not positions β€” more maintainable
  5. NULLs sort differently by database β€” use NULLS FIRST/LAST for explicit control
⭐

Premium Content

ORDER BY Clause

Unlock this lesson and 900+ advanced tutorials with a Premium plan.

🎯End-to-end Projects
πŸ’ΌInterview Prep
πŸ“œCertificates
🀝Community Access

Already a member? Log in

Need Expert SQL Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement