employees with a million rows and an index on the department_id column. Which query will most likely run faster due to index usage?Query C uses the indexed column department_id directly in the WHERE clause, allowing PostgreSQL to use the index efficiently. Query A filters on salary, which is not indexed. Query B applies a function LOWER() on name, which prevents index usage unless a functional index exists. Query D applies arithmetic on department_id, which disables index usage.
EXISTS to efficiently check if an employee has any projects assigned in the projects table?Option C uses SELECT 1 which is the standard and efficient way to use EXISTS. Option C is valid but less efficient because SELECT * fetches all columns unnecessarily. Option C selects all columns explicitly, which is inefficient. Option C uses COUNT(*) inside EXISTS, which is incorrect because SELECT COUNT(*) always returns exactly one row, making the EXISTS always true regardless of matches.
PostgreSQL's query planner analyzes all possible join orders and chooses the most efficient one, regardless of the order written in the SQL query. Therefore, the written order does not always dictate execution order. Options B, C, and D are incorrect because the planner can reorder joins freely unless specific join hints or constraints are applied.
Correlated subqueries depend on the outer query row and execute repeatedly, which can cause slow performance on large tables. Option B is incorrect because index usage alone does not guarantee speed if the subquery runs many times. Option B is unrelated to subquery performance. Option B is incorrect because uncorrelated subqueries run once and are usually efficient.
orders table with a status column. You often run this query:SELECT COUNT(*) FROM orders WHERE status = 'completed';Which optimization will most improve performance?
Partial indexes index only rows matching a condition, so a partial index on status = 'completed' speeds up queries filtering on that status. Option D creates a full index, which is larger and less efficient for this specific query. Option D adds complexity and maintenance overhead. Option D changes query logic but does not optimize performance.