Bird
Raised Fist0
PostgreSQLquery~20 mins

Partial indexes with WHERE clause in PostgreSQL - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Partial Index Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of query using a partial index

Given a table orders with columns id, status, and amount, a partial index is created as:

CREATE INDEX idx_pending_orders ON orders(amount) WHERE status = 'pending';

What will be the result of this query?

EXPLAIN SELECT * FROM orders WHERE status = 'pending' AND amount > 100;
AThe query uses the index but scans all rows regardless of status.
BThe query performs a full table scan ignoring the partial index.
CThe query uses the partial index to scan only rows with status 'pending' and amount > 100.
DThe query raises an error because partial indexes cannot be used in WHERE clauses.
Attempts:
2 left
💡 Hint

Partial indexes only include rows matching the WHERE condition.

📝 Syntax
intermediate
1:30remaining
Correct syntax for creating a partial index

Which of the following is the correct syntax to create a partial index on the users table for rows where active = true?

ACREATE PARTIAL INDEX idx_active_users ON users(id) WHERE active = true;
BCREATE INDEX idx_active_users ON users(id) WHERE active = true;
CCREATE INDEX idx_active_users ON users(id) IF active = true;
DCREATE INDEX idx_active_users ON users(id) FILTER active = true;
Attempts:
2 left
💡 Hint

Partial indexes use the WHERE clause after the index definition.

optimization
advanced
2:30remaining
Choosing a partial index for query optimization

You have a transactions table with millions of rows. Most rows have status = 'completed', but you often query for status = 'failed' transactions. Which partial index would best optimize queries filtering WHERE status = 'failed'?

ACREATE INDEX idx_failed_transactions ON transactions(id) WHERE status = 'failed';
BCREATE INDEX idx_completed_transactions ON transactions(id) WHERE status = 'completed';
CCREATE INDEX idx_all_transactions ON transactions(id);
DCREATE INDEX idx_failed_transactions ON transactions(id) WHERE status != 'failed';
Attempts:
2 left
💡 Hint

Partial indexes should match the query filter condition to be effective.

🔧 Debug
advanced
2:00remaining
Why does this partial index not improve query performance?

A partial index was created as:

CREATE INDEX idx_active_users ON users(id) WHERE active = true;

But the query below does not use the index:

SELECT * FROM users WHERE active = false AND id = 123;

Why?

AThe query optimizer ignores all indexes on boolean columns.
BThe index is corrupted and needs to be rebuilt.
CPartial indexes cannot be used with equality filters.
DThe query filters for <code>active = false</code>, but the index only covers rows where <code>active = true</code>.
Attempts:
2 left
💡 Hint

Partial indexes only include rows matching their WHERE condition.

🧠 Conceptual
expert
3:00remaining
Effect of partial index on unique constraint enforcement

Consider a table employees with a partial unique index:

CREATE UNIQUE INDEX idx_unique_email_active ON employees(email) WHERE active = true;

What is the effect of this index on enforcing uniqueness of email values?

AIt enforces uniqueness of <code>email</code> only for rows where <code>active = true</code>. Duplicate emails can exist for inactive employees.
BIt enforces uniqueness of <code>email</code> for all rows regardless of <code>active</code> status.
CIt does not enforce uniqueness at all because partial indexes cannot be unique.
DIt enforces uniqueness only for inactive employees.
Attempts:
2 left
💡 Hint

Partial unique indexes apply uniqueness only to rows matching the WHERE condition.

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

  1. Step 1: Understand partial index concept

    A partial index indexes only a subset of rows based on a condition.
  2. Step 2: Role of WHERE clause

    The WHERE clause defines which rows to include in the index.
  3. Final Answer:

    To index only rows that meet a specific condition -> Option A
  4. 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

  1. Step 1: Check index creation syntax

    CREATE INDEX requires index name, table, columns, and optional WHERE clause.
  2. Step 2: Validate WHERE clause and operators

    Use single equals (=) for comparison, and specify columns to index.
  3. Final Answer:

    CREATE INDEX idx_pending ON orders (status) WHERE status = 'pending'; -> Option A
  4. 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

  1. Step 1: Understand partial index condition

    The index covers rows where active = true, indexing last_login.
  2. Step 2: Analyze query filter

    The query filters on active = true and last_login > '2024-01-01', matching the index condition.
  3. Final Answer:

    The query will use the partial index and return matching rows quickly -> Option D
  4. 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

  1. Step 1: Check partial index WHERE clause

    The WHERE clause uses CURRENT_DATE, which is non-immutable (changes daily).
  2. Step 2: Understand index usage limitation

    PostgreSQL partial indexes require immutable conditions to be used by queries.
  3. Final Answer:

    The partial index condition uses a non-immutable function, so it cannot be used -> Option C
  4. 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

  1. Step 1: Define index condition matching query filters

    The query filters active = true AND price < 100, so index WHERE must match both.
  2. Step 2: Check syntax and columns

    Index must specify columns (price) and use AND in WHERE clause for correct filtering.
  3. Final Answer:

    CREATE INDEX idx_active_price ON products (price) WHERE active = true AND price < 100; -> Option B
  4. Quick Check:

    Partial index WHERE matches query filters exactly [OK]
Hint: Use AND in WHERE to match all query conditions [OK]
Common Mistakes:
  • Using OR instead of AND in WHERE clause
  • Omitting columns in index definition
  • Trying to create index without columns