Bird
0
0

Consider a table employees(id, department, salary) with an index on (department, salary). What will be the result of this query?

medium📝 query result Q5 of 15
PostgreSQL - Indexing Strategies
Consider a table employees(id, department, salary) with an index on (department, salary). What will be the result of this query?

SELECT department, salary FROM employees WHERE department = 'Sales';

Assuming PostgreSQL uses an index-only scan, what rows are returned?
AAll employees in Sales with their department and salary
BAll employees with their id and salary
COnly employees with salary above average
DNo rows because id is missing in index
Step-by-Step Solution
Solution:
  1. Step 1: Check index columns and query columns

    The index covers 'department' and 'salary'. The query selects these two columns and filters on 'department'.
  2. Step 2: Understand index-only scan output

    Since all needed columns are in the index, PostgreSQL can use an index-only scan and return all employees in 'Sales' with their department and salary.
  3. Final Answer:

    All employees in Sales with their department and salary -> Option A
  4. Quick Check:

    Index-only scan returns filtered rows with indexed columns = C [OK]
Quick Trick: Index-only scans return only indexed columns used in query [OK]
Common Mistakes:
  • Expecting id column in output without selecting it
  • Assuming salary filter without condition
  • Thinking index-only scan fails if id missing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes