Bird
Raised Fist0
PostgreSQLquery~5 mins

Covering indexes with INCLUDE in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a covering index in PostgreSQL?
A covering index is an index that contains all the columns needed to satisfy a query, so the database can get the data directly from the index without looking up the main table.
Click to reveal answer
beginner
What does the INCLUDE clause do in a PostgreSQL index?
The INCLUDE clause adds extra columns to the index as non-key columns. These columns are stored in the index but not used for sorting or searching, allowing queries to be covered by the index.
Click to reveal answer
intermediate
Why use INCLUDE columns instead of adding them as key columns in an index?
INCLUDE columns do not affect the index's sort order or size as much as key columns do. They help cover queries without slowing down index scans or increasing index maintenance cost significantly.
Click to reveal answer
beginner
Write a PostgreSQL command to create an index on column 'user_id' and include 'email' and 'created_at' columns.
CREATE INDEX idx_user_id_include ON users (user_id) INCLUDE (email, created_at);
Click to reveal answer
beginner
How does a covering index improve query performance?
It allows the database to answer queries using only the index without accessing the main table, reducing disk reads and speeding up query execution.
Click to reveal answer
What is the main benefit of using INCLUDE columns in a PostgreSQL index?
AThey make the index smaller by removing key columns.
BThey allow the index to cover more queries without increasing sort complexity.
CThey speed up data insertion by skipping index updates.
DThey automatically create foreign key constraints.
Which of these is true about columns added with INCLUDE in PostgreSQL indexes?
AThey replace the key columns in the index.
BThey are used for sorting and searching in the index.
CThey cannot be used to cover queries.
DThey are stored in the index but not used for sorting.
Which SQL command creates a covering index on 'order_id' including 'order_date' and 'customer_id'?
ACREATE INDEX idx_order ON orders (order_id, order_date, customer_id);
BCREATE INDEX idx_order ON orders (order_date, customer_id) INCLUDE (order_id);
CCREATE INDEX idx_order ON orders (order_id) INCLUDE (order_date, customer_id);
DCREATE INDEX idx_order ON orders INCLUDE (order_id, order_date, customer_id);
What happens if a query can be answered entirely from a covering index?
AThe database uses only the index, avoiding main table access.
BThe query runs slower because indexes are ignored.
CThe database reads the main table to get missing data.
DThe database rebuilds the index before running the query.
Which PostgreSQL version introduced the INCLUDE clause for indexes?
APostgreSQL 11
BPostgreSQL 9.2
CPostgreSQL 8.4
DPostgreSQL 13
Explain what a covering index is and how the INCLUDE clause helps create one in PostgreSQL.
Think about how indexes can store extra columns to answer queries fully.
You got /3 concepts.
    Describe the difference between key columns and INCLUDE columns in a PostgreSQL index.
    Consider how sorting and searching work in indexes.
    You got /3 concepts.

      Practice

      (1/5)
      1. What is the main purpose of using INCLUDE in a PostgreSQL index?
      easy
      A. To change the data type of indexed columns
      B. To create a unique constraint on the indexed columns
      C. To delete columns from the index
      D. To add extra columns to the index for faster SELECT queries without searching on them

      Solution

      1. Step 1: Understand the role of INCLUDE in indexes

        INCLUDE adds extra columns to the index that are not used for searching but can be returned in queries.
      2. Step 2: Identify the benefit of these extra columns

        These extra columns help avoid reading the main table, speeding up SELECT queries that need those columns.
      3. Final Answer:

        To add extra columns to the index for faster SELECT queries without searching on them -> Option D
      4. Quick Check:

        INCLUDE adds columns for SELECT speed [OK]
      Hint: INCLUDE adds columns to speed SELECT, not for searching [OK]
      Common Mistakes:
      • Thinking INCLUDE creates unique constraints
      • Believing INCLUDE removes columns
      • Assuming INCLUDE changes data types
      2. Which of the following is the correct syntax to create a covering index on table users for column email and include last_login?
      easy
      A. CREATE INDEX idx_email ON users(email) INCLUDE (last_login);
      B. CREATE INDEX idx_email ON users(email, last_login);
      C. CREATE INDEX idx_email ON users INCLUDE (email, last_login);
      D. CREATE INDEX idx_email ON users(email) WITH (last_login);

      Solution

      1. Step 1: Recall the syntax for INCLUDE in PostgreSQL indexes

        The correct syntax is to specify indexed columns first, then use INCLUDE for extra columns.
      2. Step 2: Match the syntax to the options

        CREATE INDEX idx_email ON users(email) INCLUDE (last_login); correctly uses CREATE INDEX idx_email ON users(email) INCLUDE (last_login);
      3. Final Answer:

        CREATE INDEX idx_email ON users(email) INCLUDE (last_login); -> Option A
      4. Quick Check:

        Indexed columns first, INCLUDE for extras [OK]
      Hint: Indexed columns before INCLUDE clause [OK]
      Common Mistakes:
      • Putting all columns inside parentheses without INCLUDE
      • Using WITH instead of INCLUDE
      • Including columns in wrong order
      3. Given the index CREATE INDEX idx_name ON employees(last_name) INCLUDE (first_name, department);, what will the query SELECT last_name, first_name FROM employees WHERE last_name = 'Smith'; most likely do?
      medium
      A. Use the index but still access the table to get first_name
      B. Use the index to find rows and return both last_name and first_name without accessing the table
      C. Scan the whole table because first_name is not indexed
      D. Return an error because first_name is not indexed

      Solution

      1. Step 1: Understand what INCLUDE columns do in the index

        INCLUDE columns are stored in the index to avoid accessing the main table for those columns.
      2. Step 2: Analyze the query and index usage

        The query filters on last_name (indexed) and selects first_name (included). The index covers both, so no table access needed.
      3. Final Answer:

        Use the index to find rows and return both last_name and first_name without accessing the table -> Option B
      4. Quick Check:

        INCLUDE columns avoid table access [OK]
      Hint: INCLUDE columns can be returned without table access [OK]
      Common Mistakes:
      • Assuming INCLUDE columns are not stored in the index
      • Thinking table scan is always needed
      • Confusing INCLUDE with indexed columns
      4. You wrote this index: CREATE INDEX idx_order ON orders(order_date) INCLUDE (customer_id; but get a syntax error. What is the problem?
      medium
      A. INCLUDE cannot be used with order_date
      B. You must list customer_id before order_date
      C. Missing closing parenthesis after customer_id
      D. INCLUDE requires at least two columns

      Solution

      1. Step 1: Check the syntax of the CREATE INDEX statement

        The statement has an opening parenthesis after INCLUDE but no closing parenthesis.
      2. Step 2: Identify the syntax error

        Missing closing parenthesis causes the syntax error.
      3. Final Answer:

        Missing closing parenthesis after customer_id -> Option C
      4. Quick Check:

        Parentheses must be balanced [OK]
      Hint: Check parentheses carefully in INCLUDE clause [OK]
      Common Mistakes:
      • Forgetting closing parenthesis
      • Misordering columns
      • Thinking INCLUDE needs multiple columns
      5. You want to speed up this query: SELECT product_id, price, stock FROM products WHERE product_id = 123; by creating a covering index. Which index is best?
      hard
      A. CREATE INDEX idx_product ON products(product_id) INCLUDE (price, stock);
      B. CREATE INDEX idx_product ON products(price, stock) INCLUDE (product_id);
      C. CREATE INDEX idx_product ON products(product_id, price, stock);
      D. CREATE INDEX idx_product ON products(product_id);

      Solution

      1. Step 1: Identify the filtering and selected columns in the query

        The query filters on product_id and selects price and stock.
      2. Step 2: Choose an index that filters on product_id and includes price and stock

        CREATE INDEX idx_product ON products(product_id) INCLUDE (price, stock); indexes product_id and includes price and stock, covering the query efficiently.
      3. Step 3: Compare other options

        CREATE INDEX idx_product ON products(price, stock) INCLUDE (product_id); indexes price and stock, not filtering column; CREATE INDEX idx_product ON products(product_id, price, stock); indexes all columns but includes unnecessary columns in index key; CREATE INDEX idx_product ON products(product_id); lacks included columns, so table access needed.
      4. Final Answer:

        CREATE INDEX idx_product ON products(product_id) INCLUDE (price, stock); -> Option A
      5. Quick Check:

        Filter column indexed, others included [OK]
      Hint: Index filter column, INCLUDE others for covering [OK]
      Common Mistakes:
      • Including filter columns instead of indexing them
      • Indexing all columns as keys unnecessarily
      • Not including selected columns causing table access