SQL

SQL Indexes & Performance Interview Questions

6 questions with answers · SQL Interview Guide

Clustered vs non-clustered indexes, composite indexes, execution plans, and query optimization. Senior-level interviews focus heavily on this topic.

bar_chartQuick stats
Total questions6
High frequency3
With code examples3
1

What is an index

An index is a database object that speeds up data retrieval by creating a separate data structure that points to rows in a table, similar to an index at the back of a book. Without an index, SQL Server has to scan every row in a table to find matches, which gets very slow on large tables. Indexes store a sorted copy of one or more columns along with pointers to the actual rows, so the engine can jump directly to relevant data. The trade-off is that indexes consume disk space and slow down INSERT, UPDATE, and DELETE operations because the index must be maintained alongside the table.

sql
CREATE INDEX idx_employee_lastname ON employees (last_name);

-- With multiple columns (composite index)
CREATE INDEX idx_emp_dept ON employees (department_id, last_name);
2

What is the difference between Cluster and Non-Cluster Index

A clustered index determines the physical sort order of the data in the table itself, so a table can only have one clustered index. The leaf nodes of a clustered index are the actual data pages. A non-clustered index is a separate structure that stores a sorted copy of the indexed columns along with a pointer back to the actual row, and a table can have up to 999 non-clustered indexes in SQL Server. Lookups using a non-clustered index may require an extra step called a key lookup to fetch columns not included in the index, while a clustered index read goes straight to the data. Primary keys are clustered by default in SQL Server, but you can change that.

3

You have a composite index of three columns, and you only provide the value of two columns in WHERE clause of a select query? Will Index be used for this operation

It depends on which columns you provide. Composite indexes follow the leftmost prefix rule, meaning the index is used only if your WHERE clause includes the leading column(s) of the index. If your index is on (col_a, col_b, col_c) and you filter on col_a and col_b, the index will be used effectively. But if you skip col_a and only filter on col_b and col_c, the optimizer generally cannot use that index and will fall back to a full table scan. Always make sure your most selective and most commonly queried column is at the front of a composite index.

4

Why do I need an index in a database

An index speeds up data retrieval by allowing the database engine to find rows without scanning the entire table. Think of it like a book index: instead of reading every page, you jump directly to the relevant location. Without indexes, a query on a large table does a full table scan, which gets very slow as data grows. Indexes are especially valuable on columns used frequently in WHERE clauses, JOIN conditions, and ORDER BY operations.

sql
-- Without index: full table scan on 10M rows
SELECT * FROM orders WHERE customer_id = 5000;

-- With index: near-instant lookup
CREATE INDEX idx_orders_customer ON orders(customer_id);
5

What is fill factor ? or When does page split occurs

Fill factor is a SQL Server setting that controls how much free space is left on each index page when the index is created or rebuilt. A fill factor of 80 means pages are filled to 80% capacity, leaving 20% free for future inserts and updates. This free space helps delay page splits, which happen when a new row needs to go into a page that is already full and SQL Server has to split that page into two, which causes fragmentation and hurts performance. The right fill factor depends on your workload: high for read-heavy tables, lower for frequently updated ones.

6

How to create index in SQL Server

You create an index using the CREATE INDEX statement, specifying the table and the column or columns to index. In SQL Server you can create a clustered index (one per table, determines physical sort order) or a nonclustered index (multiple allowed, a separate structure with pointers back to the table). Choosing the right columns matters a lot since indexing columns used in WHERE, JOIN, or ORDER BY clauses typically gives the biggest performance gains.

sql
-- Nonclustered index on a single column
CREATE INDEX IX_Employees_LastName
ON Employees (LastName);

-- Unique clustered index
CREATE UNIQUE CLUSTERED INDEX IX_Employees_ID
ON Employees (EmployeeID);

-- Composite nonclustered index
CREATE INDEX IX_Orders_CustomerDate
ON Orders (CustomerID, OrderDate);

Knowing the answers is half the battle

The other half is explaining them clearly under pressure.

Try a free mock interviewarrow_forward

More SQL topics