0
0
SQLquery~20 mins

Composite index and column order in SQL - 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 column order in composite index on query performance

Consider a table Orders with columns customer_id and order_date. A composite index is created as (customer_id, order_date). Which query will benefit most from this index?

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

Think about how the index is structured and which columns are used first in the WHERE clause.

🧠 Conceptual
intermediate
1:30remaining
Understanding composite index column order

Why does the order of columns in a composite index matter?

ABecause the database uses the first column to quickly narrow down rows before checking the second column.
BBecause the database always searches columns in alphabetical order regardless of index order.
CBecause the order determines which columns can be NULL in the index.
DBecause the order affects the physical storage location of the table rows.
Attempts:
2 left
💡 Hint

Think about how indexes help the database find data quickly.

📝 Syntax
advanced
1:30remaining
Correct syntax to create a composite index

Which of the following SQL statements correctly creates a composite index on columns last_name and first_name in a table Employees?

ACREATE INDEX idx_name ON Employees last_name, first_name;
BCREATE INDEX idx_name ON Employees (last_name, first_name);
CCREATE INDEX idx_name (last_name, first_name) ON Employees;
DCREATE INDEX idx_name ON Employees WHERE last_name, first_name;
Attempts:
2 left
💡 Hint

Remember the order of clauses in the CREATE INDEX statement.

optimization
advanced
2:00remaining
Choosing column order for composite index to optimize queries

You have a table Sales with columns region, sales_rep, and sale_date. Most queries filter by sales_rep and then by region. Which composite index column order is best for performance?

A(region, sale_date)
B(region, sales_rep)
C(sales_rep, region)
D(sale_date, sales_rep)
Attempts:
2 left
💡 Hint

Think about which column is filtered first in queries.

🔧 Debug
expert
2:30remaining
Diagnose why a composite index is not used

A composite index exists on (category, price) in a Products table. The query SELECT * FROM Products WHERE price > 100; is slow and does not use the index. Why?

ABecause the query filters only on the second column of the composite index, the index cannot be used efficiently.
BBecause the index must be rebuilt before it can be used for queries.
CBecause the query uses a greater than operator which is not supported by indexes.
DBecause the table has no primary key defined.
Attempts:
2 left
💡 Hint

Think about how composite indexes work with leading columns.