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?
Think about how the index is structured and which columns are used first in the WHERE clause.
The composite index (customer_id, order_date) is ordered first by customer_id, then by order_date. Queries filtering on customer_id and optionally order_date can use the index efficiently. Queries B and C both filter on both columns, so they benefit most. The order of conditions in the WHERE clause does not affect index usage. Query A filters only on order_date, which is the second column, so the index is not used efficiently. Query D filters only on customer_id, which can use the index but less specifically than B and C.
Why does the order of columns in a composite index matter?
Think about how indexes help the database find data quickly.
The order of columns in a composite index matters because the database uses the first column to filter rows first, then uses the second column to further narrow down results. This makes searching efficient. The other options are incorrect because alphabetical order is not used, NULL handling is unrelated to order, and physical storage of rows is not affected by index column order.
Which of the following SQL statements correctly creates a composite index on columns last_name and first_name in a table Employees?
Remember the order of clauses in the CREATE INDEX statement.
The correct syntax is CREATE INDEX index_name ON table_name (column1, column2);. Option B follows this syntax. Options A, C, and D have syntax errors such as missing parentheses or incorrect clause order.
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?
Think about which column is filtered first in queries.
Since queries filter first by sales_rep and then by region, the composite index should start with sales_rep to efficiently narrow down rows, followed by region. Option C matches this order. Other options start with columns filtered later or not filtered at all, reducing index effectiveness.
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?
Think about how composite indexes work with leading columns.
Composite indexes work best when the query filters on the first column or a leftmost prefix of the index. Since the query filters only on price, which is the second column, the index on (category, price) cannot be used efficiently. The other options are incorrect because indexes support range queries, rebuilding is not required for usage, and primary key presence does not affect index usage here.