0
0
PostgreSQLquery~20 mins

EXPLAIN output reading in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
EXPLAIN Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Understanding the output of a simple EXPLAIN query

Given the following SQL query and its EXPLAIN output, what is the estimated number of rows the query planner expects to process?

EXPLAIN SELECT * FROM employees WHERE department_id = 5;

Output:

Seq Scan on employees  (cost=0.00..35.50 rows=10 width=100)
A10
B35
C100
D5
Attempts:
2 left
💡 Hint

Look for the 'rows=' value in the EXPLAIN output.

query_result
intermediate
2:00remaining
Identifying the join type from EXPLAIN output

Look at this EXPLAIN output for a join query. What type of join is being used?

Nested Loop  (cost=0.00..50.00 rows=20 width=200)
  -> Seq Scan on table1  (cost=0.00..20.00 rows=100 width=100)
  -> Index Scan using idx_table2 on table2  (cost=0.00..0.30 rows=1 width=100)
ACross Join
BHash Join
CMerge Join
DNested Loop Join
Attempts:
2 left
💡 Hint

The first line of the EXPLAIN output usually shows the join type.

📝 Syntax
advanced
2:00remaining
Detecting syntax error in EXPLAIN query usage

Which of the following EXPLAIN queries will cause a syntax error in PostgreSQL?

AEXPLAIN SELECT * FROM employees WHERE salary > 50000;
BEXPLAIN SELECT FROM employees WHERE salary > 50000;
CEXPLAIN (FORMAT JSON) SELECT * FROM employees WHERE salary > 50000;
DEXPLAIN ANALYZE SELECT * FROM employees WHERE salary > 50000;
Attempts:
2 left
💡 Hint

Check the syntax of the SELECT statement inside EXPLAIN.

optimization
advanced
2:00remaining
Interpreting cost estimates in EXPLAIN output

Given this EXPLAIN output, what does the 'cost=0.00..100.00' represent?

Seq Scan on orders  (cost=0.00..100.00 rows=500 width=50)
AThe startup cost is 0.00 and total cost is 100.00
BThe query will take exactly 100 seconds to run
CThe cost is the number of rows scanned
DThe cost is the number of columns scanned
Attempts:
2 left
💡 Hint

Cost values show estimated resource usage, not time or row counts.

🧠 Conceptual
expert
2:00remaining
Understanding the effect of EXPLAIN ANALYZE on query execution

What is the main difference between running EXPLAIN and EXPLAIN ANALYZE on a query in PostgreSQL?

ABoth run the query but EXPLAIN ANALYZE shows more details about indexes
BEXPLAIN runs the query; EXPLAIN ANALYZE only shows the plan
CEXPLAIN shows estimated plan only; EXPLAIN ANALYZE runs the query and shows actual run times
DEXPLAIN ANALYZE disables all indexes during execution
Attempts:
2 left
💡 Hint

Think about whether the query is executed or just planned.