0
0
PostgreSQLquery~20 mins

Common query optimization patterns in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Query Optimization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Effect of Index Usage on Query Performance
Consider a table employees with a million rows and an index on the department_id column. Which query will most likely run faster due to index usage?
ASELECT * FROM employees WHERE department_id = 5;
BSELECT * FROM employees WHERE LOWER(name) = 'john';
CSELECT * FROM employees WHERE salary > 50000;
DSELECT * FROM employees WHERE department_id + 1 = 6;
Attempts:
2 left
💡 Hint
Indexes speed up queries that use the indexed column directly in conditions without transformations.
📝 Syntax
intermediate
2:00remaining
Correct Use of EXISTS for Efficient Filtering
Which of the following queries correctly uses EXISTS to efficiently check if an employee has any projects assigned in the projects table?
ASELECT * FROM employees e WHERE EXISTS (SELECT p.* FROM projects p WHERE p.employee_id = e.id);
BSELECT * FROM employees e WHERE EXISTS (SELECT * FROM projects p WHERE p.employee_id = e.id);
CSELECT * FROM employees e WHERE EXISTS (SELECT 1 FROM projects p WHERE p.employee_id = e.id);
DSELECT * FROM employees e WHERE EXISTS (SELECT COUNT(*) FROM projects p WHERE p.employee_id = e.id);
Attempts:
2 left
💡 Hint
EXISTS only checks for presence, so selecting 1 is enough and efficient.
🧠 Conceptual
advanced
2:00remaining
Understanding the Impact of JOIN Order on Query Performance
In PostgreSQL, which statement about the order of tables in a JOIN clause is true regarding query optimization?
AThe query planner can reorder JOINs regardless of the written order to optimize performance.
BThe order of tables in the JOIN clause always determines the join order used by the query planner.
CJOIN order cannot be changed by the planner if explicit JOIN hints are not used.
DThe first table in the JOIN clause is always used as the driving table in the execution plan.
Attempts:
2 left
💡 Hint
PostgreSQL's planner is smart and can reorder joins for best performance.
🔧 Debug
advanced
2:00remaining
Identifying the Cause of Slow Query with Subquery
A query uses a subquery in the WHERE clause to filter rows, but it runs very slowly on a large table. Which of the following is the most likely cause?
AThe subquery uses an index, so it should be fast regardless of size.
BThe subquery is correlated and runs once per row, causing many executions.
CThe main query uses SELECT *, which slows down the subquery.
DThe subquery is uncorrelated and runs only once, so it cannot cause slowness.
Attempts:
2 left
💡 Hint
Correlated subqueries run repeatedly for each row in the outer query.
optimization
expert
2:00remaining
Optimizing Aggregation Queries with Partial Indexes
You have a large orders table with a status column. You often run this query:

SELECT COUNT(*) FROM orders WHERE status = 'completed';

Which optimization will most improve performance?
ARewrite the query to use <code>GROUP BY status</code> instead of WHERE.
BCreate a full index on the <code>status</code> column for all rows.
CAdd a materialized view that stores the count of completed orders and refresh it periodically.
DCreate a partial index on <code>status</code> for rows where <code>status = 'completed'</code>.
Attempts:
2 left
💡 Hint
Partial indexes index only a subset of rows, making lookups faster for specific conditions.