Difference between Clustered and Non-clustered index

07 Jul, 2022
Prerequisite – SQL queries on clustered and non-clustered Indexes 
1. Clustered Index : Clustered index is created only when both the following conditions satisfy – 
The data or file, that you are moving into secondary memory should be in sequential or sorted order.There should be a key value, meaning it can not have repeated values.  
The data or file, that you are moving into secondary memory should be in sequential or sorted order.
There should be a key value, meaning it can not have repeated values.  
Whenever you apply clustered indexing in a table, it will perform sorting in that table only. You can create only one clustered index in a table like primary key. Clustered index is as same as dictionary where the data is arranged by alphabetical order. 
In clustered index, index contains pointer to block but not direct data.  
Example of Clustered Index – If you apply primary key to any column, then automatically it will become clustered index.  
create table Student
( Roll_No int primary key, 
Name varchar(50), 
Gender varchar(30), 
Mob_No bigint );

insert into Student
values (4, 'ankita', 'female', 9876543210 );

insert into Student 
values (3, 'anita', 'female', 9675432890 );

insert into Student 
values (5, 'mahima', 'female', 8976453201 ); 
In this example, Roll no is a primary key, it will automatically act as a clustered index. The output of this code will produce in increasing order of roll no. 
 
You can have only one clustered index in one table, but you can have one clustered index on multiple columns, and that type of index is called composite index. 
2. Non-clustered Index : Non-Clustered Index is similar to the index of a book. The index of a book consists of a chapter name and page number, if you want to read any topic or chapter then you can directly go to that page by using index of that book. No need to go through each and every page of a book. 
The data is stored in one place, and index is stored in another place. Since, the data and non-clustered index is stored separately, then you can have multiple non-clustered index in a table. 
In non-clustered index, index contains the pointer to data. 
 
Example of Non-clustered Index – 
create table Student
( Roll_No int primary key, 
Name varchar(50), 
Gender varchar(30), 
Mob_No bigint );

insert into Student 
values (4, 'afzal', 'male', 9876543210 );

insert into Student 
values (3, 'sudhir', 'male', 9675432890 );

insert into Student 
values (5, 'zoya', 'female', 8976453201 );

create nonclustered index NIX_FTE_Name
on Student (Name ASC); 
Here, roll no is a primary key, hence there is automatically a clustered index. If we want to apply non-clustered index in NAME column (in ascending order), then the new table will be created for that column. 
Output before applying non-clustered index :  
Output after applying non-clustered index : 
 
Row address is used because, if someone wants to search the data for sudhir, then by using the row address he/she will directly go to that row address and can fetch the data directly. 
Difference between Clustered and Non-clustered index : 
ayushupapro
shivnagarsoge
raj2002
Picked
DBMS
Difference Between
GATE CS
DBMS
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.