Bird
0
0

Given a table products(id, name, price) with an index on (id, price), what will be the output of this query if an index-only scan is used?

medium📝 query result Q4 of 15
PostgreSQL - Indexing Strategies
Given a table products(id, name, price) with an index on (id, price), what will be the output of this query if an index-only scan is used?

SELECT id, price FROM products WHERE price > 100;
ARows with id and price where price is greater than 100
BRows with id and name where price is greater than 100
CAll rows regardless of price
DError due to missing columns in index
Step-by-Step Solution
Solution:
  1. Step 1: Check index columns and query columns

    The index covers 'id' and 'price'. The query selects 'id' and 'price' and filters on 'price'. All needed columns are in the index.
  2. Step 2: Determine output with index-only scan

    Since the index covers the query, PostgreSQL can use an index-only scan and return rows with 'id' and 'price' where price > 100.
  3. Final Answer:

    Rows with id and price where price is greater than 100 -> Option A
  4. Quick Check:

    Index covers selected columns = A [OK]
Quick Trick: Index-only scan returns only indexed columns requested [OK]
Common Mistakes:
  • Expecting columns not in index to appear
  • Assuming error if some columns missing in index but not selected
  • Thinking all rows are returned regardless of filter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes