0
0
MySQLquery~20 mins

Composite indexes in MySQL - Practice Problems & Coding Challenges

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

Given a table orders with columns customer_id, order_date, and status, and a composite index on (customer_id, order_date), which query will most efficiently use the index?

ASELECT * FROM orders WHERE order_date = '2024-01-01' AND status = 'shipped';
BSELECT * FROM orders WHERE order_date = '2024-01-01';
CSELECT * FROM orders WHERE status = 'shipped' AND customer_id = 123;
DSELECT * FROM orders WHERE customer_id = 123 AND order_date = '2024-01-01';
Attempts:
2 left
💡 Hint

Think about the order of columns in the composite index and which columns are filtered in the query.

🧠 Conceptual
intermediate
2:00remaining
Understanding leftmost prefix rule in composite indexes

Which of the following statements best describes the leftmost prefix rule for composite indexes?

AA composite index can only be used if the query filters on the first column of the index or a leftmost subset of columns in order.
BA composite index is only used if the query filters on the last column of the index.
CA composite index can be used by queries filtering on any combination of columns in the index, regardless of order.
DA composite index is ignored if the query filters on all columns in the index.
Attempts:
2 left
💡 Hint

Think about how the database uses the index starting from the first column.

📝 Syntax
advanced
2:00remaining
Creating a composite index in MySQL

Which of the following SQL statements correctly creates a composite index named idx_customer_date on the customer_id and order_date columns of the orders table?

ACREATE INDEX idx_customer_date ON orders (order_date, customer_id);
BCREATE COMPOSITE INDEX idx_customer_date ON orders (customer_id, order_date);
CCREATE INDEX idx_customer_date ON orders (customer_id, order_date);
DCREATE INDEX idx_customer_date ON orders customer_id, order_date;
Attempts:
2 left
💡 Hint

Remember the correct syntax for creating indexes in MySQL.

optimization
advanced
2:00remaining
Choosing the best composite index for query performance

You have a table sales with columns region, product_id, and sale_date. You often run this query:

SELECT * FROM sales WHERE product_id = 101 AND region = 'North';

Which composite index will optimize this query best?

ACREATE INDEX idx_region_product ON sales (region, product_id);
BCREATE INDEX idx_product_region ON sales (product_id, region);
CCREATE INDEX idx_sale_date_product ON sales (sale_date, product_id);
DCREATE INDEX idx_region_sale_date ON sales (region, sale_date);
Attempts:
2 left
💡 Hint

Consider which column is filtered first in the query and the order of columns in the index.

🔧 Debug
expert
2:00remaining
Diagnosing why a composite index is not used

You created a composite index on (category, price) in a products table. The query below does not use the index:

SELECT * FROM products WHERE price > 100;

Why is the composite index not used?

ABecause the query filters only on the second column <code>price</code>, not the first column <code>category</code> of the composite index.
BBecause the query uses a greater than operator, which is not supported by composite indexes.
CBecause the index was created with the wrong column order; it should be (price, category).
DBecause composite indexes cannot be used for range queries.
Attempts:
2 left
💡 Hint

Recall the leftmost prefix rule for composite indexes.