SQL Indexes and Query Performance for Beginners
Codevity Admin · 10/25/2026
Step 18 of 25 · SQL Interview Preparation for Beginners · Stage 5: Indexes, design and transactions
Codevity Admin · 10/25/2026
An index is a separate structure that lets the database find rows without reading the whole table, like the index at the back of a book. Fresher interviews rarely go deep, but they always ask what indexes are, the clustered versus non-clustered difference, and why some queries cannot use an index.
| Clustered index | Non-clustered index | |
|---|---|---|
| What it is | The table's rows are stored in the index order | A separate structure with the key values and a pointer back to each row |
| How many per table | One: the data can only be sorted one way | Many |
| Created by default for | PRIMARY KEY | UNIQUE constraints |
| Reading extra columns | Not needed: the leaf level already holds the full row | May need a key lookup back to the table |
A table with no clustered index is called a heap. A primary key is a logical rule, and a clustered index is a physical structure: SQL Server happens to implement a primary key with a clustered index by default (unless one already exists, or you ask for NONCLUSTERED). They are not the same thing.
-- Supports lookups by customer and avoids a key lookup for the total
CREATE INDEX ix_orders_customer
ON orders (customer_id)
INCLUDE (total);A scan is not automatically bad. On a small table, or when the query needs most of the rows anyway, scanning can be the cheapest plan.
A covering index contains every column a query needs, either as key columns or as INCLUDE columns, so SQL Server never has to look up the table. That is the INCLUDE (total) above.
A composite index has several key columns, and their order matters. An index on (department_id, salary) helps WHERE department_id = 1 and WHERE department_id = 1 AND salary > 60000, but it does not help a query that filters on salary alone, because the index is sorted by department_id first.
SARG stands for Search ARGument. A predicate is SARGable when the engine can use an index seek for it. The usual way to break that is to wrap the column in a function or expression: the engine would have to compute the function for every row before it could compare anything.
| Not SARGable | SARGable rewrite |
|---|---|
| A leading wildcard cannot seek. Consider full-text search for this | |
When you compare two different data types, SQL Server converts one side by its data type precedence. If it has to convert the column, the index cannot be used to seek. A common case is comparing a column with an value, because has higher precedence. Keep column types and parameter types identical.
Every index must be maintained when data changes. More indexes mean slower , and , and more storage. Index the columns you filter, join and sort on frequently (foreign keys are good candidates), not everything. An index on a column with very few distinct values, such as a yes/no flag, rarely helps.
An execution plan shows how the database will run (or did run) your query.
WHERE YEAR(order_date) = 2026WHERE order_date >= '20260101' AND order_date < '20270101'WHERE LEFT(name, 1) = 'A'WHERE name LIKE 'A%'WHERE salary * 12 > 600000WHERE salary > 50000WHERE name LIKE '%sha'WHERE ISNULL(manager_id, 0) = 0WHERE manager_id IS NULLVARCHARNVARCHARNVARCHARINSERTUPDATEDELETESET STATISTICS IO ON;EXPLAIN ANALYZEWHERE YEAR(order_date) = 2026