Views, indexes, and query performance
Searchable predicates and SARGability
Write key ranges that can use an index search condition.
Lesson 75 of 84
Reading: about 2 min
Lesson + practice: about 15 min
Difficulty: Advanced
On this page4 sections
This lesson uses DDI_SQL_Training dataset v1.0.0 on SQL Server 2019 or later. Write key ranges that can use an index search condition.
Run the example
sql
SELECT OrderID, OrderDate
FROM dbo.Orders
WHERE OrderID >= 100 AND OrderID < 105
ORDER BY OrderID;Read it step by step
- The predicate keeps the indexed OrderID column unwrapped.
- The half-open range includes IDs 100–104 and excludes 105.
- This shape is searchable, but the optimizer still chooses its plan based on cost.
Check the result
| OrderID | OrderDate |
|---|---|
| 100 | 2023-04-11 |
| 101 | 2023-04-12 |
| 102 | 2023-04-13 |
| 103 | 2023-04-14 |
| 104 | 2023-04-15 |
The result order comes from ORDER BY, not from index storage.
Practice
Return Orders 200–202 using a half-open OrderID range.
Check your answer and expected result
Reveal answer
sql
SELECT OrderID, OrderDate
FROM dbo.Orders
WHERE OrderID >= 200 AND OrderID < 203
ORDER BY OrderID;| OrderID | OrderDate |
|---|---|
| 200 | 2023-07-20 |
| 201 | 2023-07-21 |
| 202 | 2023-07-22 |
The upper bound is excluded, so the three IDs are 200, 201, and 202.
Your progress
Reveal the check answer or reach the end to complete this lesson.