In this post, we will discuss how to work with indexes in SQL and explain the concepts with an example in a simple way. I hope this is very useful for beginners and intermediates to help them understand the basic concept.
We use indexes to increase the performance of the application and fetch the data very quickly from the table. Normally, after project deployment, we are facing some performance issues, usually in reporting if there are lakhs of records. We have to re-write the queries to increase the performance. The SQL server uses the index and views (selected and required columns) to find the data quickly. Indexes are applied to the column or view.
Whenever you create indexes in a table, internally, B-Tree is created like the below image.

Index in SQL is a similar index that we find in the book. Indexes are created on table and views and indexes can help the query to find the data quickly.
Table basically can contain four types of indexes.
- Clustered
- Composite Index
- Non- Clustered
- Unique Index
Advantages
- To improve the performance of the query.
- Indexes are used to fast retrieve the data result set from table
Disadvantages
- Non clustered index is stored separately from the table, so it requires additional disk space.
Cluster index
Hence, a clustered index should be applied to a column, having unique values. Also, much less modification is done on these values. Usually, it is applied to the primary key column of the table.
A table can have only one clustered index, however, the index can contain multiple columns. The way telephone directory is organized is by the last name and the first name.
Syntax
- CREATE CLUSTERED INDEX IndexName ON TableName(ColumnName)
Example
- CREATE TABLE Customer
- (
- Id int,
- Name varchar(50),
- Balance money
- )
- INSERT INTO Customer values(5,'A1',25000), (3,'B1',25000), (4,'C1',25000), (1,'D1',25000), (2,'E1',25000)
- Select * from Customer
Output
We are checking if there are any indexes created or not with the below ‘sp_helpindex’ procedure.
Explicitly create a cluster index using the command
We are going to explicitly create a clustered index on customer table. After executing the query, you will get the result in ascending order and see the output below.
- CREATE CLUSTERED INDEX PK_Customer_CLUSTERED_INDEX ON Customer(ID)
- Select * from Customer
- Exec sp_helpindex 'Customer'
Output
Primary Key with Cluster Index
Whenever we are creating a primary key, the clustered index is automatically created.
Let’s take another example. We are creating a customer table with primary key. Please ensure that you drop the created customer table first.
Example
- Drop table Customer
- CREATE TABLE Customer
- (
- Id int primary key,
- Name varchar(50),
- Balance money
- )
- INSERT INTO Customer values(5,'A1',25000), (3,'B1',25000), (4,'C1',25000), (1,'D1',25000), (2,'E1',25000)
Output
We are trying to create one more cluster index on customer table but we cannot create more than one cluster index in a table.
Example
- CREATE CLUSTERED INDEX PK_Customer_Balance_CLUSTERED_INDEX ON Customer(Balance)
Output

According to this error, we cannot create more than one clustered index in a table.
With Primary Key Table
Example
- CREATE TABLE Example
- (
- Id int primary key identity,
- Example varchar(50),
- Description varchar(50)
- )
- EXECSP_HELPINDEXExample









Pravas RanjanPosted Sep 12, 2018, 5:45 AM
Can you please explain how this index work internally with diagram so it will be very helpful for interview perspective thank you.
Pravas RanjanPosted Sep 12, 2018, 5:43 AM
Good one Jitendra bhai
Vineet DubeyPosted Sep 6, 2018, 5:49 AM
I think this article is very helpful for every persons
Jignesh KumarPosted Aug 30, 2018, 10:58 AM
Nice article ...