0
0
PostgreSQLquery~20 mins

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

Choose your learning style9 modes available
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.