Composite Index — Complete Guide
Composite Index — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of SQL Server Tutorial on Toolliyo Academy.
On this page
SQL Server Tutorial · Lesson 34 of 100
Composite Index
SQL basics ✓ → Queries → Advanced
Queries · 2 — JOINs · ~6 min · SQL — Indexing & Performance
What is this?
A composite index has multiple key columns. Order matters: leftmost prefixes are usable (A), (A,B), not (B) alone unless the engine scans.
Why should you care?
Filtering TenantId then OrderDate matches how multi-tenant apps query — put the equality column first.
See it live — copy this example
Run in SQL Server Management Studio (SSMS) or Azure Data Studio.
USE DataVerse;
IF COL_LENGTH('dbo.Orders', 'OrderDate') IS NULL
ALTER TABLE dbo.Orders ADD OrderDate DATE NOT NULL CONSTRAINT DF_Orders_OrderDate DEFAULT (CAST(SYSUTCDATETIME() AS DATE));
CREATE NONCLUSTERED INDEX IX_Orders_City_OrderDate
ON dbo.Orders (City, OrderDate)
INCLUDE (Amount);
SELECT OrderId, Amount
FROM dbo.Orders
WHERE City = N'Mumbai' AND OrderDate >= '2026-01-01';
What happened?
- Key order City then OrderDate supports equality on City plus range on OrderDate.
- INCLUDE keeps Amount covered.
Practice next
- Create the composite index.
- Run the Mumbai date-range query and check for Seek.
- Filter only OrderDate and see a less ideal plan.
- Query WHERE City = N'Pune' only — still uses left prefix.
- Add OrderId to INCLUDE for full covering.
Remember
Multi-column keys are ordered. Equality columns usually lead. Design indexes from real WHERE clauses.
City operations queue
Warehouse app lists DataVerse orders for a city by date.
Outcome: Composite index matches the screen filter exactly.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!