0
0
PostgreSQLquery~20 mins

Expression indexes in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Expression Index Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Effect of expression index on query performance

Given a table users with columns id (integer) and email (text), an expression index is created on LOWER(email). Which query will use this index efficiently?

PostgreSQL
CREATE INDEX idx_lower_email ON users (LOWER(email));
ASELECT * FROM users WHERE LOWER(email) = 'example@example.com';
BSELECT * FROM users WHERE email = 'example@example.com';
CSELECT * FROM users WHERE UPPER(email) = 'EXAMPLE@EXAMPLE.COM';
DSELECT * FROM users WHERE email LIKE '%example.com';
Attempts:
2 left
💡 Hint

Think about which query matches the expression used in the index.

📝 Syntax
intermediate
2:00remaining
Correct syntax to create an expression index

Which of the following is the correct syntax to create an expression index on the price * quantity expression in a sales table?

ACREATE INDEX idx_total ON sales (price, quantity);
BCREATE INDEX idx_total ON sales (price * quantity);
CCREATE INDEX idx_total ON sales USING btree (price * quantity);
DCREATE INDEX idx_total ON sales (price + quantity);
Attempts:
2 left
💡 Hint

Expression indexes require specifying the expression inside parentheses, optionally with an index method.

🧠 Conceptual
advanced
2:00remaining
Why use expression indexes?

What is the main advantage of using an expression index in a database?

ATo index the result of a function or expression for faster queries involving that expression.
BTo automatically update all columns in a table when an expression changes.
CTo store computed columns physically in the table for faster inserts.
DTo create a backup of the table's data based on an expression.
Attempts:
2 left
💡 Hint

Think about what expression indexes store and how they help queries.

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

A developer created this index:

CREATE INDEX idx_trim_name ON employees (TRIM(name));

But the query below does not use the index:

SELECT * FROM employees WHERE name = 'Alice';

Why?

AThe TRIM function is not supported in expression indexes.
BThe query does not use the TRIM function, so the index on TRIM(name) is not used.
CThe index was created on the wrong table.
DThe index needs to be created as UNIQUE to be used.
Attempts:
2 left
💡 Hint

Check if the query expression matches the index expression.

optimization
expert
3:00remaining
Optimizing queries with expression indexes on JSON data

Consider a products table with a JSONB column attributes. You want to speed up queries filtering products where attributes->>'color' = 'red'. Which index creation is best?

ACREATE INDEX idx_color ON products (attributes->'color');
BCREATE INDEX idx_color ON products (attributes);
CCREATE INDEX idx_color ON products USING gin (attributes);
DCREATE INDEX idx_color ON products ((attributes->>'color'));
Attempts:
2 left
💡 Hint

Think about indexing the exact expression used in the WHERE clause.