Composite INDEX
- SQL course
- Intermediate
- Lesson 79/115
- 4 min
Create a multi-column index.
Composite INDEX
A composite INDEX covers multiple columns. Ideal for columns queried together.
CREATE INDEX idx_city_status ON customers (city, status);This query indexes city and status together.
The index idx_city_status is created.
Composite INDEX — details
Column order matters in a composite index. The first column is searched first.
For example, a (city, status) index helps WHERE city = 'İzmir' and WHERE city = 'İzmir' AND status = 'active', but not WHERE status = 'active' alone.
Practice Composite INDEX
Add a composite index on city and status.
Starter code
-- composite index
CREATE INDEX idx_city_status ON customers (city)Sample solution
CREATE INDEX idx_city_status ON customers (city, status);Expected output
İndeks oluşturuldu
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Composite INDEX: match the terms
| Term | Meaning |
|---|---|
composite |
multi-column |
order |
matters |
first column |
priority |
together |
query |
2 Add the second column
CREATE INDEX idx_city_status ON customers (city, ____);
CREATE INDEX idx_city_status ON customers (city, status);
Explanation: Column order matters in a composite index; (city, status) is ordered by city first.
3 What matters in a composite index?
- The column name
- The column order Correct answer
- The table name
- The data type
Explanation: Column order matters in a composite index.