Execution Plans · Query Tuning · EXPLAIN · Indexes · Guide + 25 Interview Questions

Understanding SQL Execution Plans & Query Tuning: EXPLAIN, Indexes & Optimization

A SQL execution plan shows how a database will execute a query — the order of operations, joins, filters, and scans. Understanding execution plans is essential for query tuning: identifying bottlenecks, adding the right indexes, and choosing the best join strategy. This guide covers EXPLAIN, query plans, indexes, join algorithms, statistics, and the 25 interview questions that actually come up — with deep, descriptive theory aligned to the topic.

🎯 Key Takeaways
25 Interview Questions Beginner to Advanced EXPLAIN, Indexes, Joins, Statistics Updated Dec 3, 2026
Kailas Warade ✓ Verified Author
Data Engineer · Oracle Certified · SQL & PL/SQL
Kailas works with SQL, PL/SQL, Python, and PySpark to build data pipelines. He writes the SQL interview guides at Sankalan Data Tech, focused on practical, real-world answers.
🏆 Oracle Certified 💼 Data Engineer 📝 SQL & PL/SQL Guides 📍 Pune, India
📤 Share:

On This Page

What Execution Plans Really Are

A SQL execution plan shows how a database will execute a query — the order of operations, joins, filters, and scans.

-- Execution plan flow
Query → Parse → Optimize → Execute Plan → Results

The core insight is that the same query can be executed many ways. The optimizer chooses the best plan based on statistics, indexes, and query structure.

Understanding execution plans is essential for query tuning — identifying bottlenecks and fixing them.

Execution plans are the starting point for all query optimization work.

EXPLAIN in Depth

EXPLAIN is a SQL command that shows the execution plan for a query without actually running it.

-- EXPLAIN: estimated plan
EXPLAIN SELECT * FROM employees WHERE department_id = 5;

-- EXPLAIN ANALYZE: actual plan with timing
EXPLAIN ANALYZE SELECT * FROM employees WHERE department_id = 5;

EXPLAIN shows the estimated plan — what the optimizer thinks will happen. EXPLAIN ANALYZE actually runs the query and shows the real timing and row counts.

EXPLAIN is safe to run anytime. EXPLAIN ANALYZE actually executes the query, so be careful with expensive queries.

EXPLAIN is the first step in query tuning — it shows where the time goes.

Scans: Full Table vs Index

A full table scan reads every row in a table. An index scan uses an index to find only relevant rows.

-- Full table scan: reads every row
SELECT * FROM employees;  -- no filter, full scan

-- Index scan: uses index to find rows
SELECT * FROM employees WHERE department_id = 5;
-- If indexed on department_id, uses index scan

Full table scans are slow for large tables. Index scans are faster for selective queries.

A full table scan usually indicates a missing index or a query that cannot use an index.

Always check the execution plan to see which scan type is used.

Indexes in Depth

Indexes speed up queries by allowing the database to find rows without scanning the whole table.

-- Single-column index
CREATE INDEX idx_dept ON employees(department_id);

-- Composite index (multiple columns)
CREATE INDEX idx_dept_name ON employees(department_id, last_name);

-- Covering index (includes all needed columns)
CREATE INDEX idx_covering ON employees(department_id, salary);

A single-column index works for queries filtering on that column. A composite index works for queries filtering on multiple columns (in order).

A covering index includes all columns needed by a query, so the database does not need to access the table itself.

Indexes speed up reads but slow down writes (INSERT/UPDATE/DELETE). Balance is key.

Join Algorithms in Depth

Join algorithms are the methods a database uses to combine rows from two tables.

-- Join algorithms
Nested Loop: compare every row
Hash Join: build hash table, probe
Merge Join: sort both, merge

Nested Loop Join. Compares every row in one table with every row in another. Simple but slow for large tables. Good for small tables or when one side is small.

Hash Join. Builds a hash table from one table and probes it with the other. Fast for large tables with equality joins.

Merge Join. Sorts both tables and merges them. Efficient when both inputs are already sorted.

The optimizer chooses the join algorithm based on table sizes and available indexes.

Statistics and the Optimizer

SQL statistics are metadata about data distribution that the query optimizer uses to choose the best execution plan.

-- Update statistics
ANALYZE TABLE employees;

-- Statistics include
Row counts, distinct values, histograms

The optimizer uses statistics to estimate cardinality (how many rows a query will return) and choose the best plan.

Outdated statistics lead to bad plan choices. Regularly update statistics.

A cost-based optimizer chooses the plan with the lowest estimated cost. A rule-based optimizer uses fixed rules.

25 SQL Execution Plans & Query Tuning Interview Questions and Answers

These are the execution plan and query tuning questions that come up most often in SQL and data engineering interviews. Every answer stands on its own — read them in any order, or work through them in sequence to build a complete understanding of execution plans, EXPLAIN, indexes, join algorithms, statistics, and query tuning.

🔍

No matching questions found

Try a keyword like EXPLAIN, index, join, or statistics.

A SQL execution plan shows how a database will execute a query.

-- Execution plan flow
Query → Parse → Optimize → Execute Plan → Results

It shows the order of operations, joins, filters, and scans.

Understanding execution plans is essential for query tuning.

EXPLAIN is a SQL command that shows the execution plan for a query without actually running it.

EXPLAIN SELECT * FROM employees WHERE department_id = 5;

EXPLAIN shows the estimated plan.

It is safe to run anytime.

EXPLAIN shows the estimated plan. EXPLAIN ANALYZE actually runs the query and shows the actual plan with real timing.

-- EXPLAIN: estimated
EXPLAIN SELECT ...;

-- EXPLAIN ANALYZE: actual
EXPLAIN ANALYZE SELECT ...;

EXPLAIN is safe. EXPLAIN ANALYZE actually executes.

A query plan is a tree of operations the database will perform to execute a query.

-- Query plan includes
Scans, joins, filters, sorts

It shows the order of operations.

It is the roadmap for execution.

A query profile shows how a query was actually executed.

-- Query profile shows
Time, data scanned, operators, bottlenecks

It shows real timing and row counts.

It is more accurate than a query plan.

A query plan shows how a query will be executed. A query profile shows how it was actually executed.

Aspect Query Plan Query Profile
When Before execution After execution
Accuracy Estimated Actual
Use Predict issues Diagnose issues

Query plans predict. Query profiles diagnose.

A full table scan reads every row in a table.

-- Full table scan
SELECT * FROM employees;  -- no filter

It is slow for large tables.

It usually indicates a missing index.

An index scan uses an index to find rows.

-- Index scan
SELECT * FROM employees WHERE department_id = 5;
-- If indexed, uses index scan

It is faster than a full table scan for selective queries.

It requires an index on the filter column.

A full table scan reads every row. An index scan uses an index to find only relevant rows.

Aspect Full Table Scan Index Scan
Rows read All rows Only relevant rows
Speed Slow for large tables Fast for selective queries
Requires No index Index on filter column

Index scans are faster for selective queries.

A covering index includes all columns needed by a query.

-- Covering index
CREATE INDEX idx_covering ON employees(department_id, salary);

-- Query uses only index, no table access
SELECT department_id, salary FROM employees WHERE department_id = 5;

It improves performance by avoiding table access.

It is ideal for frequently-run queries.

A composite index is an index on multiple columns.

-- Composite index
CREATE INDEX idx_dept_name ON employees(department_id, last_name);

It is useful for queries filtering on multiple columns.

Column order matters — put the most selective column first.

A clustered index determines the physical order of data. A non-clustered index is a separate structure that points to data.

Aspect Clustered Index Non-Clustered Index
Physical order Determines it Does not
Count per table One Many
Example Primary key Secondary index

Clustered index determines physical order. Non-clustered is a pointer structure.

A join algorithm is the method a database uses to combine rows from two tables.

-- Join algorithms
Nested Loop, Hash Join, Merge Join

The optimizer chooses based on table sizes and indexes.

Different algorithms have different performance characteristics.

A nested loop join compares every row in one table with every row in another.

-- Nested loop: O(n × m)
-- Good for small tables

It is simple but slow for large tables.

Good when one side is small or indexed.

A hash join builds a hash table from one table and probes it with the other.

-- Hash join: O(n + m)
-- Good for large tables with equality joins

It is fast for large tables with equality joins.

It requires memory for the hash table.

A merge join sorts both tables and merges them.

-- Merge join: O(n log n + m log m)
-- Good when both inputs are sorted

It is efficient when both inputs are already sorted.

It requires sorting if inputs are not sorted.

SQL statistics are metadata about data distribution.

-- Statistics include
Row counts, distinct values, histograms

The optimizer uses them to choose plans.

Outdated statistics lead to bad plans.

A query optimizer is the part of the database that chooses the best execution plan.

-- Optimizer chooses based on
Statistics, indexes, query structure

It aims for the lowest cost plan.

It is the brain of query execution.

Cardinality estimation is the optimizer's guess of how many rows a query will return.

-- Cardinality estimation
-- Affects plan choice

Accurate estimation leads to good plans.

Inaccurate estimation leads to bad plans.

A cost-based optimizer chooses the plan with the lowest estimated cost.

-- Cost-based: lowest cost
-- Based on statistics

It is more flexible than rule-based.

It is the standard in modern databases.

A rule-based optimizer chooses the plan based on fixed rules.

-- Rule-based: fixed rules
-- Less flexible than cost-based

It is simpler but less accurate.

It is mostly legacy now.

A query hint is a directive that tells the optimizer to use a specific plan or index.

-- Query hint example
SELECT /*+ INDEX(employees idx_dept) */ * FROM employees;

It overrides the optimizer's default choice.

Use sparingly — only when the optimizer makes a bad choice.

A correlated subquery references columns from the outer query.

-- Correlated subquery
SELECT * FROM employees e
WHERE salary > (SELECT AVG(salary) FROM employees WHERE dept = e.dept);

It can be slow because it may execute once per row.

Rewrite as a JOIN when possible for better performance.

A subquery is a query nested inside another. A join combines rows from two tables.

-- Subquery: nested query
-- Join: combines tables

Joins are usually faster for large datasets.

Subqueries are better for existence checks.

Well-designed query tuning follows a few key principles.

  • Use EXPLAIN — always start with the execution plan.
  • Add indexes — on filter and join columns.
  • Avoid SELECT * — scan only needed columns.
  • Update statistics — keep them fresh.
  • Avoid correlated subqueries — rewrite as JOINs when possible.
  • Use appropriate joins — let the optimizer choose, or hint if needed.
-- Well-tuned query
SELECT e.name, d.dept_name
FROM employees e
JOIN departments d ON e.dept_id = d.id
WHERE e.department_id = 5;

-- With index on department_id

These principles produce queries that are fast and efficient.

Mistakes I Keep Seeing in Interviews

These come up repeatedly. None of them are exotic, and every one of them is easy to avoid once you know it is coming.

Mistake one: not using EXPLAIN. Always start with the execution plan.
Mistake two: confusing EXPLAIN with EXPLAIN ANALYZE. One estimates, one executes.
Mistake three: ignoring full table scans. They are slow and usually indicate a missing index.
Mistake four: not understanding join algorithms. They affect performance significantly.
Mistake five: not updating statistics. Outdated statistics lead to bad plans.
Mistake six: overusing query hints. Let the optimizer choose unless it makes a bad choice.

Follow-Up Questions Interviewers Ask

Getting the first answer right is only half the interview. What usually decides the outcome is what happens next. Here is what to prepare for on the execution plan topic.

After “What is an execution plan?” — expect

  • “What is EXPLAIN?”
  • “What is EXPLAIN ANALYZE?”
  • “What is the difference?”
  • “How do you read a plan?”

After “Indexes” — expect

  • “What is a covering index?”
  • “What is a composite index?”
  • “What is the difference from clustered?”
  • “When would you use one?”

After “Join algorithms” — expect

  • “What is a nested loop join?”
  • “What is a hash join?”
  • “What is a merge join?”
  • “When would you use each?”

After “Statistics” — expect

  • “What is cardinality estimation?”
  • “What is a cost-based optimizer?”
  • “What is a query hint?”
  • “How do you update statistics?”

For every question above, prepare a second-level answer. The candidates who pass are not the ones who memorized the first answer — they are the ones who kept going for another thirty seconds and said something useful about why it matters.

Quick Quiz

Five questions to check whether the topics above have landed.

1. What does EXPLAIN do?
2. What is the difference between EXPLAIN and EXPLAIN ANALYZE?
3. What does a full table scan indicate?
4. Which join algorithm is best for large tables with equality joins?
5. What is a covering index?