Advanced · Lesson 9 of 18

Query Optimization Basics

Writing correct SQL is step one. Writing FAST SQL is step two.

Use EXPLAIN to see how the database executes your query:

EXPLAIN QUERY PLAN
SELECT * FROM employees WHERE salary > 100000;

Optimization tips:

  • Avoid SELECT * - select only columns you need
  • Use indexes on columns in WHERE and JOIN clauses
  • Filter early with WHERE to reduce rows processed
  • Use LIMIT when you only need a few rows
  • Avoid functions on indexed columns in WHERE

Bad: WHERE UPPER(first_name) = 'JOHN' Good: WHERE first_name = 'John'

Even small optimizations matter at scale with millions of rows.

Check your understanding

What is a key SQL optimization best practice?