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
Creating a Covering Index with INCLUDE in PostgreSQL
📖 Scenario: You work for an online bookstore. The database has a table called books that stores information about each book, including its id, title, author, price, and stock.To speed up queries that search by author and also display the title and price, you want to create a special index called a covering index.
🎯 Goal: Create a covering index on the books table that indexes the author column and includes the title and price columns to make queries faster.
📋 What You'll Learn
Create the books table with columns id, title, author, price, and stock.
Add a covering index on the author column.
Include the title and price columns in the index using the INCLUDE clause.
💡 Why This Matters
🌍 Real World
Covering indexes help speed up database queries by including extra columns in the index, reducing the need to access the main table data.
💼 Career
Database administrators and backend developers use covering indexes to optimize query performance in real-world applications.
Progress0 / 4 steps
1
Create the books table
Write a SQL statement to create a table called books with these columns and types: id as integer primary key, title as text, author as text, price as numeric(6,2), and stock as integer.
PostgreSQL
Hint
Use CREATE TABLE books and define each column with its type. Remember to set id as the primary key.
2
Define the index name
Create a variable called index_name and set it to the string 'idx_books_author' to hold the name of the index you will create.
PostgreSQL
Hint
Use the PostgreSQL psql command \set to define a variable called index_name with the value 'idx_books_author'.
3
Create the covering index with INCLUDE
Write a SQL statement to create an index named idx_books_author on the author column of the books table. Include the title and price columns in the index using the INCLUDE clause.
PostgreSQL
Hint
Use CREATE INDEX idx_books_author ON books (author) INCLUDE (title, price); to create the covering index.
4
Verify the index creation
Write a SQL query to list all indexes on the books table by selecting from pg_indexes where tablename = 'books'.
PostgreSQL
Hint
Use SELECT * FROM pg_indexes WHERE tablename = 'books'; to see the indexes on the books table.
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
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.
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.
Final Answer:
To add extra columns to the index for faster SELECT queries without searching on them -> Option D
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
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.
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);
Final Answer:
CREATE INDEX idx_email ON users(email) INCLUDE (last_login); -> Option A
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
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.
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.
Final Answer:
Use the index to find rows and return both last_name and first_name without accessing the table -> Option B
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
Step 1: Check the syntax of the CREATE INDEX statement
The statement has an opening parenthesis after INCLUDE but no closing parenthesis.
Step 2: Identify the syntax error
Missing closing parenthesis causes the syntax error.
Final Answer:
Missing closing parenthesis after customer_id -> Option C
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
Step 1: Identify the filtering and selected columns in the query
The query filters on product_id and selects price and stock.
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.
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.
Final Answer:
CREATE INDEX idx_product ON products(product_id) INCLUDE (price, stock); -> Option A
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