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 partial index in PostgreSQL?
A partial index is an index built over a subset of table rows that satisfy a specific condition defined by a WHERE clause. It helps speed up queries that filter on that condition.
Click to reveal answer
beginner
How do you create a partial index with a WHERE clause?
Use the syntax: CREATE INDEX index_name ON table_name(column_name) WHERE condition; This creates an index only on rows where the condition is true.
Click to reveal answer
intermediate
Why use a partial index instead of a full index?
Partial indexes save space and improve performance by indexing only relevant rows. They make queries faster when filtering on the indexed condition and reduce maintenance overhead.
Click to reveal answer
intermediate
Can a partial index speed up queries that do not use the WHERE condition in the index?
No. Partial indexes only help queries that filter rows matching the WHERE condition used in the index. Queries outside that condition won't use the partial index.
Click to reveal answer
beginner
Example: What does this partial index do? CREATE INDEX idx_active_users ON users(last_login) WHERE active = true;
It creates an index on the last_login column but only for rows where the user is active (active = true). Queries filtering active users by last_login will be faster.
Click to reveal answer
What does a partial index in PostgreSQL do?
AIndexes all rows in a table
BIndexes only the first 100 rows
CIndexes columns with NULL values only
DIndexes only rows that satisfy a WHERE condition
✗ Incorrect
A partial index indexes only the rows that meet the specified WHERE condition.
Which SQL clause is used to define a partial index?
AWHERE
BHAVING
CGROUP BY
DORDER BY
✗ Incorrect
The WHERE clause specifies the condition for the partial index.
What is a benefit of using a partial index?
AIt indexes all columns in a table
BIt disables query optimization
CIt reduces index size by indexing fewer rows
DIt automatically updates table data
✗ Incorrect
Partial indexes reduce index size by indexing only rows that meet the condition.
Can a partial index speed up queries that do not filter on the indexed condition?
AYes, always
BNo, only queries matching the condition benefit
COnly if the table is small
DOnly if the index is rebuilt
✗ Incorrect
Partial indexes only help queries that filter rows matching the WHERE condition.
Which of these is a valid partial index creation statement?
ACREATE INDEX idx ON table(column) WHERE column > 100;
BCREATE INDEX idx ON table(column);
CCREATE INDEX idx ON table WHERE column > 100;
DCREATE INDEX idx WHERE column > 100 ON table(column);
✗ Incorrect
The correct syntax places the WHERE clause after the column list.
Explain what a partial index is and why you might use one in PostgreSQL.
Think about indexing only some rows instead of the whole table.
You got /3 concepts.
Describe how to create a partial index and give an example with a WHERE clause.
Remember the order: CREATE INDEX, ON table(column), WHERE condition.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a partial index with a WHERE clause in PostgreSQL?
easy
A. To index only rows that meet a specific condition
B. To index all rows in the table regardless of condition
C. To create a backup of the table
D. To delete rows that do not meet the condition
Solution
Step 1: Understand partial index concept
A partial index indexes only a subset of rows based on a condition.
Step 2: Role of WHERE clause
The WHERE clause defines which rows to include in the index.
Final Answer:
To index only rows that meet a specific condition -> Option A
Quick Check:
Partial index = conditional indexing [OK]
Hint: Partial indexes use WHERE to limit indexed rows [OK]
Common Mistakes:
Thinking partial indexes index all rows
Confusing partial index with table backup
Assuming partial index deletes rows
2. Which of the following is the correct syntax to create a partial index on a table orders for rows where status = 'pending'?
easy
A. CREATE INDEX idx_pending ON orders (status) WHERE status = 'pending';
B. CREATE INDEX idx_pending ON orders WHERE status = 'pending';
C. CREATE INDEX idx_pending ON orders WHERE status = 'pending' (status);
D. CREATE INDEX idx_pending ON orders (status) WHERE status == 'pending';
Solution
Step 1: Check index creation syntax
CREATE INDEX requires index name, table, columns, and optional WHERE clause.
Step 2: Validate WHERE clause and operators
Use single equals (=) for comparison, and specify columns to index.
Final Answer:
CREATE INDEX idx_pending ON orders (status) WHERE status = 'pending'; -> Option A
Quick Check:
Correct syntax includes columns and WHERE with = [OK]
Hint: Include columns before WHERE; use single = for condition [OK]
Common Mistakes:
Omitting columns in index definition
Using double equals (==) instead of single =
Placing WHERE before columns
3. Given the partial index: CREATE INDEX idx_active_users ON users (last_login) WHERE active = true; What will be the result of this query? SELECT * FROM users WHERE active = true AND last_login > '2024-01-01';
medium
A. The query will return no rows because partial indexes exclude all rows
B. The query will ignore the partial index and perform a full table scan
C. The query will cause a syntax error due to the partial index
D. The query will use the partial index and return matching rows quickly
Solution
Step 1: Understand partial index condition
The index covers rows where active = true, indexing last_login.
Step 2: Analyze query filter
The query filters on active = true and last_login > '2024-01-01', matching the index condition.
Final Answer:
The query will use the partial index and return matching rows quickly -> Option D
Quick Check:
Query matches partial index condition = uses index [OK]
Hint: Query filters must match partial index WHERE to use it [OK]
Common Mistakes:
Assuming partial index causes syntax errors
Thinking partial index excludes all rows
Believing query ignores partial index if condition partially matches
4. You created a partial index: CREATE INDEX idx_recent_orders ON orders (order_date) WHERE order_date > CURRENT_DATE - INTERVAL '30 days'; But queries filtering order_date > CURRENT_DATE - INTERVAL '30 days' are not using the index. What is the likely problem?
medium
A. Partial indexes cannot use date intervals
B. The index is missing the column order_date
C. The partial index condition uses a non-immutable function, so it cannot be used
D. The WHERE clause in the index is invalid syntax
Solution
Step 1: Check partial index WHERE clause
The WHERE clause uses CURRENT_DATE, which is non-immutable (changes daily).
Step 2: Understand index usage limitation
PostgreSQL partial indexes require immutable conditions to be used by queries.
Final Answer:
The partial index condition uses a non-immutable function, so it cannot be used -> Option C
Quick Check:
Non-immutable functions block partial index usage [OK]
Hint: Partial index WHERE must use immutable expressions [OK]
Common Mistakes:
Assuming syntax error causes issue
Thinking partial indexes can't use date columns
Ignoring function immutability rules
5. You want to speed up queries on a products table filtering only active products with price < 100. Which partial index is best to create?
hard
A. CREATE INDEX idx_active_price ON products (price);
B. CREATE INDEX idx_active_price ON products (price) WHERE active = true AND price < 100;
C. CREATE INDEX idx_active_price ON products (price) WHERE active = true OR price < 100;
D. CREATE INDEX idx_active_price ON products WHERE active = true AND price < 100;
Solution
Step 1: Define index condition matching query filters
The query filters active = true AND price < 100, so index WHERE must match both.
Step 2: Check syntax and columns
Index must specify columns (price) and use AND in WHERE clause for correct filtering.
Final Answer:
CREATE INDEX idx_active_price ON products (price) WHERE active = true AND price < 100; -> Option B
Quick Check:
Partial index WHERE matches query filters exactly [OK]
Hint: Use AND in WHERE to match all query conditions [OK]