Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Understanding Query Execution Plans
📖 Scenario: You are a database analyst who wants to understand how a database runs queries to improve performance. You will explore a simple SQL query and learn how to view its execution plan step-by-step.
🎯 Goal: Build a basic SQL query and learn how to generate and interpret its execution plan to see how the database processes the query.
📋 What You'll Learn
Create a simple table with data
Write a basic SELECT query
Use EXPLAIN to get the query execution plan
Identify key parts of the execution plan
💡 Why This Matters
🌍 Real World
Understanding query execution plans helps database users and developers optimize queries for faster and more efficient data retrieval.
💼 Career
Database administrators and developers use execution plans daily to troubleshoot slow queries and improve database performance.
Progress0 / 4 steps
1
Create a table with sample data
Write SQL code to create a table called employees with columns id (integer) and name (text). Insert these exact rows: (1, 'Alice'), (2, 'Bob'), (3, 'Charlie').
DBMS Theory
Hint
Use CREATE TABLE to define the table and INSERT INTO to add rows.
2
Write a simple SELECT query
Write a SQL query that selects all columns from the employees table using SELECT * FROM employees.
DBMS Theory
Hint
Use SELECT * FROM employees; to get all rows and columns.
3
Generate the query execution plan
Write a SQL statement using EXPLAIN before the SELECT query to show the query execution plan for SELECT * FROM employees.
DBMS Theory
Hint
Use EXPLAIN before your SELECT query to see how the database plans to run it.
4
Identify key parts of the execution plan
Add a comment below the EXPLAIN statement describing what the execution plan shows about how the database reads the employees table.
DBMS Theory
Hint
Look for terms like 'Seq Scan' or 'sequential scan' in the plan and explain it simply.
Practice
(1/5)
1. What is the main purpose of a query execution plan in a database?
easy
A. To backup the database
B. To show how the database will execute a query step-by-step
C. To create new tables automatically
D. To store the query results permanently
Solution
Step 1: Understand what a query execution plan is
A query execution plan explains the steps the database takes to run a query.
Step 2: Identify the main purpose
The plan helps users see how the database processes the query to optimize performance.
Final Answer:
To show how the database will execute a query step-by-step -> Option B
Quick Check:
Query execution plan = execution steps [OK]
Hint: Execution plans show query steps clearly [OK]
Common Mistakes:
Confusing plans with data storage
Thinking plans create tables
Assuming plans backup data
2. Which SQL command is commonly used to view the query execution plan before running a query?
easy
A. SHOW PLAN
B. RUN
C. EXPLAIN
D. DESCRIBE
Solution
Step 1: Recall the command to view execution plans
The SQL command EXPLAIN is used to display how a query will be executed.
Step 2: Differentiate from other commands
RUN executes queries, SHOW PLAN is not standard, and DESCRIBE shows table structure.
Final Answer:
EXPLAIN -> Option C
Quick Check:
View plan = EXPLAIN [OK]
Hint: Use EXPLAIN to see query plans [OK]
Common Mistakes:
Using RUN to view plans
Confusing DESCRIBE with EXPLAIN
Assuming SHOW PLAN is standard
3. Given the query SELECT * FROM employees WHERE department_id = 5;, what does the execution plan likely show?
medium
A. A full table scan of employees
B. A join operation with another table
C. A sort operation on employee names
D. An index scan on department_id if indexed
Solution
Step 1: Analyze the query condition
The query filters employees by department_id = 5. If department_id has an index, the database uses it.
Step 2: Understand execution plan behavior
If indexed, the plan shows an index scan to quickly find matching rows instead of scanning the whole table.
Final Answer:
An index scan on department_id if indexed -> Option D
Quick Check:
Indexed filter = index scan [OK]
Hint: Indexed columns use index scan in plans [OK]
Common Mistakes:
Assuming full table scan always happens
Expecting join when none exists
Thinking sorting is automatic
4. A query execution plan shows a full table scan but the query filters on a column that has an index. What is the likely cause?
medium
A. The query uses a function on the indexed column
B. The index is corrupted or unusable
C. The database always prefers full scans
D. The table is empty
Solution
Step 1: Understand why indexes may be ignored
If a query applies a function (like LOWER or CAST) on an indexed column, the index cannot be used directly.
Step 2: Rule out other options
Corrupted indexes are rare and usually cause errors; databases do not always prefer full scans; empty tables do not cause full scans.
Final Answer:
The query uses a function on the indexed column -> Option A
Quick Check:
Functions on indexed columns block index use [OK]
Hint: Functions on indexed columns disable index use [OK]
Common Mistakes:
Assuming index corruption without error
Believing full scans are default
Thinking empty tables cause scans
5. You want to optimize a slow query that joins two large tables. The execution plan shows a nested loop join causing delays. What is a good approach to improve performance?
hard
A. Create indexes on the join columns to enable hash or merge joins
B. Remove all indexes to force full table scans
C. Rewrite the query to use subqueries instead of joins
D. Increase the database cache size without changing the query
Solution
Step 1: Identify the cause of slow join
Nested loop joins are slow on large tables without indexes on join columns.
Step 2: Apply indexing to improve join method
Creating indexes on join columns allows the database to use faster join methods like hash or merge joins.
Step 3: Evaluate other options
Removing indexes slows queries; rewriting to subqueries may not help; increasing cache alone may not fix join inefficiency.
Final Answer:
Create indexes on the join columns to enable hash or merge joins -> Option A