Hello Learners. Thank you so much for the support for my first article “Learn About Sql Server Architecture”. I hope you all are doing well.
This article is all about Indexes in SQL Server. Some of us have heard about the term indexes and some of us have not. Well not to worry for those who have not heard about it and are in the learning phase. Let’s just start now.
In simple terms, we can think of dictionary as a SQL Server Engine, the partition by each alphabet (A to Z) as tables and the content page (First Page) as Indexes. Now suppose your first page (Content or Index Page) is missing, and you want to search something starting with ‘G’ in Dictionary (Sql server), you will have to search almost all the pages that are filled with letter ‘G’ in the dictionary. It obviously takes time to find that particular word if there is no first page (Content or Index page). Now suppose you have the First Page (Content or Index Page), this task would be easier as you would know where the page is, where the words that start with letter ‘G’ are, and hence it would take less time to find the word (Record) that you want. Hence, it clearly defines that indexes help in better searches and take less time to fetch the records.
Similarly in SQL Server we have indexes for the tables which help us to fetch the data in less time and help increase the performance as well. There are many types of indexes for different purposes. In this article I will try to include all the indexes with examples as well. But mainly they are categorized as :
- Clustered Indexes
- NonClustered Indexes
Let’s learn about them one by one.
CLUSTERED INDEXES
Clustered indexes are used to sort the data in a particular order. When you create a Primary Key on any Table, it will create a clustered index on that column automatically which helps in sorting the data in the table. In case there is no primary key, you can still make the Clustered Indexes. There can be only ONE Clustered Index per table as the data can only be sorted in any one order on the basis of columns; for instance, if there is an employee table the data can be sorted either by EmpID or by EmpName.
All the indexes are stored in a B Tree Structure format which has a root, intermediate level and leaf node. At all the levels there is a key (Index Column) which helps to sort the data or rows in a table. When the pointer reaches the leaf nodes it has both the key value and the entire row.
A table without a clustered index is called as HEAP and with cluster, it is known as Clustered Table.
Syntax to create Clustered Index
- Create Clustered Index IndexName ON TableName(ColumnName)
Here, I will be using an example of table and then will create a clustered index on it and see the execution plan of the same as well.
- Create Table Test
- (
- ID int,
- Name varchar(20),
- Salary int
- )
Now we will insert the records into the table
- Insert into Test values (6,’f’,23000), (2,’b’,12000), (5,’e’,29000), (3,’c’,56000), (1,’a’,31000), (4,’d’,16000), (8,’h’,21000)
Now we will select the data from the table and see how it appears.
- Select * from Test
When we run the above query we get the given results.
ID Name Salary
6 f 23000
2 b 12000
5 e 29000
3 c 56000
1 a 31000
4 d 16000
8 h 21000
You will see that the result will be in the order in which they were inserted. Now we will create a clustered index with Name (Key Column) on Test Table.
- Create Clustered Index Ix_CL_Test On Test(Name)
- Select * from Test

Join the conversation! Your thoughts help the community grow.