A composite index is an index that includes more than one column from a table. It helps speed up queries that filter or sort by multiple columns together.
Click to reveal answer
intermediate
Why does the order of columns matter in a composite index?
The order matters because the database uses the index starting from the first column. Queries filtering on the first column(s) can use the index efficiently, but if the first column is missing in the query, the index might not be used.
Click to reveal answer
intermediate
Given a composite index on (A, B), which query can use the index efficiently?
A query filtering on column A alone or on both A and B can use the index efficiently. But a query filtering only on B cannot use the index effectively.
Click to reveal answer
beginner
How does a composite index improve query performance?
It allows the database to quickly find rows matching multiple column conditions without scanning the whole table, reducing search time.
Click to reveal answer
intermediate
What happens if you reverse the column order in a composite index?
Reversing the order changes which queries can use the index efficiently. For example, an index on (B, A) helps queries filtering on B first, but not those filtering only on A.
Click to reveal answer
What is a composite index?
AA type of table
BAn index on a single column
CAn index on multiple columns
DA database user
✗ Incorrect
A composite index includes more than one column to speed up queries involving those columns.
If you have a composite index on (A, B), which query can use it efficiently?
AWHERE C = 5
BWHERE B = 5
CWHERE B = 5 AND A = 10
DWHERE A = 5
✗ Incorrect
The index can be used efficiently when filtering on the first column A or both A and B, but not on B alone.
Why does column order matter in a composite index?
ABecause the database uses the index starting from the first column
BBecause the last column is always ignored
CBecause columns are sorted alphabetically
DBecause indexes only work on one column
✗ Incorrect
The database uses the index starting from the first column, so queries must filter on that column to use the index.
What is the main benefit of using a composite index?
AFaster queries on multiple columns
BMore storage space
CSlower inserts
DLess data in the table
✗ Incorrect
Composite indexes speed up queries that filter or sort by multiple columns.
If you have an index on (B, A), which query is less likely to use it efficiently?
AWHERE B = 10
BWHERE A = 10
CWHERE B = 10 AND A = 20
DWHERE B > 5
✗ Incorrect
Queries filtering only on A cannot use an index starting with B efficiently.
Explain what a composite index is and why the order of columns matters.
Think about how the database searches using the index.
You got /4 concepts.
Describe how a composite index can improve query performance and give an example of when it might not be used.
Consider which columns the query filters on compared to the index order.
You got /4 concepts.
Practice
(1/5)
1. What is the main benefit of using a composite index in a database?
easy
A. It encrypts the data for security.
B. It reduces the size of the database file.
C. It automatically backs up the database.
D. It speeds up queries filtering on multiple columns together.
Solution
Step 1: Understand what a composite index does
A composite index is created on multiple columns to help the database find rows faster when queries filter on those columns together.
Step 2: Compare options to the definition
Only It speeds up queries filtering on multiple columns together. correctly describes the benefit of speeding up queries using multiple columns. Other options describe unrelated features.
Final Answer:
It speeds up queries filtering on multiple columns together. -> Option D
Quick Check:
Composite index = speeds up multi-column queries [OK]
Hint: Composite index helps multi-column filters run faster [OK]
Common Mistakes:
Thinking it reduces database size
Confusing indexing with backup or encryption
Assuming it speeds up single-column queries only
2. Which of the following is the correct syntax to create a composite index on columns last_name and first_name in a table employees?
easy
A. CREATE INDEX idx_name ON employees last_name, first_name;
B. CREATE INDEX idx_name ON employees (last_name, first_name);
C. CREATE INDEX idx_name (last_name, first_name) ON employees;
D. CREATE INDEX idx_name ON employees WHERE last_name, first_name;
Solution
Step 1: Recall correct CREATE INDEX syntax
The correct syntax is: CREATE INDEX index_name ON table_name (column1, column2);
Step 2: Match options to syntax
CREATE INDEX idx_name ON employees (last_name, first_name); matches the correct syntax exactly. Options A, C, and D have syntax errors or misplaced keywords.
Final Answer:
CREATE INDEX idx_name ON employees (last_name, first_name); -> Option B
Quick Check:
CREATE INDEX ... ON table (columns) [OK]
Hint: Use parentheses around columns after ON table [OK]
Common Mistakes:
Omitting parentheses around columns
Placing columns before ON keyword
Using WHERE instead of column list
3. Given a composite index on (city, age) in a table users, which query will use the index efficiently?
medium
A. SELECT * FROM users WHERE age = 25 AND city = 'Boston';
B. SELECT * FROM users WHERE age > 30;
C. SELECT * FROM users WHERE city = 'Boston' AND age > 30;
D. SELECT * FROM users WHERE city = 'Boston' ORDER BY age DESC;
Solution
Step 1: Understand index column order usage
A composite index on (city, age) can be used efficiently if the query filters on city first, then age.
Step 2: Analyze each query
SELECT * FROM users WHERE city = 'Boston' AND age > 30; filters on city and age, matching index order. SELECT * FROM users WHERE age > 30; filters only on age (second column), so index is less useful. SELECT * FROM users WHERE age = 25 AND city = 'Boston'; filters age first, then city, which is less efficient. SELECT * FROM users WHERE city = 'Boston' ORDER BY age DESC; filters city but only orders by age, which may use index partially.
Final Answer:
SELECT * FROM users WHERE city = 'Boston' AND age > 30; -> Option C
Quick Check:
Index (city, age) best for city then age filters [OK]
Hint: Filter on first index column before others [OK]
Common Mistakes:
Assuming index works well if filtering only second column
Ignoring column order in composite index
Thinking ORDER BY always uses index fully
4. You created a composite index on (department, salary) but your query filtering only on salary is slow. What is the likely cause?
medium
A. The index column order starts with department, so filtering only salary can't use it well.
B. The index is corrupted and needs rebuilding.
C. The salary column data type is incompatible with indexing.
D. The database does not support composite indexes.
Solution
Step 1: Recall how composite index column order affects usage
Composite indexes work best when filtering starts with the first column. Filtering only on the second column (salary) won't use the index efficiently.
Step 2: Evaluate options
The index column order starts with department, so filtering only salary can't use it well. correctly explains the cause. Options A, B, and D are unlikely or incorrect because the index exists and salary can be indexed normally.
Final Answer:
The index column order starts with department, so filtering only salary can't use it well. -> Option A
Quick Check:
Filter must start with first index column [OK]
Hint: Filter on first index column to use composite index [OK]
Common Mistakes:
Assuming any column in index can be filtered efficiently alone
Blaming index corruption without checking usage
Ignoring column order importance
5. You have a table orders with columns customer_id, order_date, and status. You often run queries filtering by customer_id and sorting by order_date. Which composite index will optimize these queries best?
hard
A. CREATE INDEX idx_customer_date ON orders (customer_id, order_date);
B. CREATE INDEX idx_date_customer ON orders (order_date, customer_id);
C. CREATE INDEX idx_status_customer ON orders (status, customer_id);
D. CREATE INDEX idx_order ON orders (order_date);
Solution
Step 1: Identify query filter and sort columns
Queries filter by customer_id and sort by order_date, so index should start with customer_id and include order_date next.
Step 2: Match index column order to query pattern
CREATE INDEX idx_customer_date ON orders (customer_id, order_date); matches this order (customer_id, order_date), allowing efficient filtering and sorting. CREATE INDEX idx_date_customer ON orders (order_date, customer_id); reverses order, less efficient for filtering. Options A, B, and C do not match query needs.
Final Answer:
CREATE INDEX idx_customer_date ON orders (customer_id, order_date); -> Option A
Quick Check:
Index columns match filter then sort order [OK]
Hint: Index columns in filter then sort order [OK]
Common Mistakes:
Reversing column order in composite index
Indexing unrelated columns
Using single-column index when composite needed
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.