0
0
PostgreSQLquery~20 mins

Covering indexes with INCLUDE in PostgreSQL - Practice Problems & Coding Challenges

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

Given a table employees(id INT, name TEXT, department TEXT, salary INT) with an index created as CREATE INDEX idx_dept_salary ON employees(department) INCLUDE (salary);, which query can fully use the index without accessing the table heap?

ASELECT department, salary FROM employees WHERE department = 'Sales';
BSELECT department, name FROM employees WHERE department = 'Sales';
CSELECT salary FROM employees WHERE salary > 50000;
DSELECT id, salary FROM employees WHERE department = 'Sales';
Attempts:
2 left
💡 Hint

Think about which columns are in the index key and which are included.

📝 Syntax
intermediate
2:00remaining
Correct syntax for creating an index with INCLUDE

Which of the following is the correct syntax to create a covering index on orders(order_date) including customer_id and total_amount columns in PostgreSQL?

ACREATE INDEX idx_orders_date ON orders(order_date, customer_id, total_amount);
BCREATE INDEX idx_orders_date ON orders(order_date) INCLUDE (customer_id, total_amount);
CCREATE INDEX idx_orders_date ON orders INCLUDE (customer_id, total_amount) (order_date);
DCREATE INDEX idx_orders_date ON orders(order_date) WITH INCLUDE (customer_id, total_amount);
Attempts:
2 left
💡 Hint

Remember the position of the INCLUDE clause in the syntax.

🧠 Conceptual
advanced
2:00remaining
Why use INCLUDE columns in indexes?

What is the main advantage of adding columns with INCLUDE in a PostgreSQL index?

AIt enforces uniqueness on the included columns.
BIt creates a composite primary key with included columns.
CIt allows storing additional columns in the index to support index-only scans without increasing index key size.
DIt compresses the included columns to save disk space.
Attempts:
2 left
💡 Hint

Think about how included columns affect index size and query performance.

🔧 Debug
advanced
2:00remaining
Why does this index not support index-only scans?

Consider the table products(id INT, category TEXT, price NUMERIC, stock INT) and the index:

CREATE INDEX idx_cat_price ON products(category, price);

Why might a query SELECT category, stock FROM products WHERE category = 'Books'; not use an index-only scan?

ABecause <code>stock</code> is not part of the index key or included columns, so the heap must be accessed.
BBecause the index is on <code>category</code> and <code>price</code>, but the query filters on <code>stock</code>.
CBecause the index is not unique, index-only scans are disabled.
DBecause the query does not select <code>price</code>, the index cannot be used.
Attempts:
2 left
💡 Hint

Think about which columns are available in the index and which are requested by the query.

optimization
expert
3:00remaining
Optimizing a query with covering indexes

You have a large table sales(order_id INT, customer_id INT, order_date DATE, total NUMERIC, status TEXT). You often run this query:

SELECT order_date, total FROM sales WHERE status = 'completed';

Which index will best optimize this query for index-only scans?

ACREATE INDEX idx_status_date ON sales(status, order_date, total);
BCREATE INDEX idx_total ON sales(total) INCLUDE (order_date, status);
CCREATE INDEX idx_order_date ON sales(order_date) INCLUDE (status, total);
DCREATE INDEX idx_status ON sales(status) INCLUDE (order_date, total);
Attempts:
2 left
💡 Hint

Consider which columns are filtered and which are selected, and how INCLUDE affects index size.