The Wayback Machine - https://web.archive.org/web/20261121142912/https://www.geeksforgeeks.org/sql/sql-queries-on-clustered-and-non-clustered-indexes/

SQL Queries on Clustered and Non-Clustered Indexes

Last Updated : 13 Nov, 2026

Clustered and non-clustered indexes use different structures to organize and access table data. They help the database locate records faster and execute queries more efficiently. The choice between these indexes depends on how the table is structured and how the data is accessed.

1. Clustered Index

A clustered index stores table rows in the order of the indexed column. Each table can have only one clustered index.

Example: First, we create an employees table with the required columns. Then, we create a clustered index on the emp_id column by defining it as the primary key and insert some sample records.

Query:

CREATE TABLE employees (
emp_id INT,
emp_name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10, 2),
CONSTRAINT pk_employees PRIMARY KEY CLUSTERED (emp_id)
);

INSERT INTO employees VALUES
(101, 'Alice', 'HR', 45000),
(102, 'John', 'IT', 55000),
(103, 'Michael', 'Finance', 60000),
(104, 'Jane', 'IT', 50000);

SELECT * FROM employees;

Output:

Clustered_and_Non_Clustered_Indexes

Dropping and Creating a Custom Clustered Index

A table can have only one clustered index. If the primary key has created the existing clustered index, we must first remove the primary key constraint before creating a clustered index on another column.

Example: Suppose we want to create a clustered index on the emp_name column instead of emp_id. First, drop the primary key constraint. Then, create a clustered index on emp_name

ALTER TABLE employees
DROP CONSTRAINT pk_employees;

CREATE CLUSTERED INDEX idx_employees_emp_name
ON employees (emp_name ASC);

EXEC sp_helpindex 'employees';

Output:

Clustered_and_Non_Clustered_Indexes12

2. Non-Clustered Index

A non-clustered index is a separate structure that stores indexed column values and row locators to find records in a table. It helps improve data retrieval without changing the physical order of table rows.

Example: Creating a Non-Clustered Index. First, we create an employees table and insert some sample records.

Query:

CREATE TABLE employees (
emp_id INT,
emp_name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10, 2)
);

INSERT INTO employees
(emp_id, emp_name, department, salary)
VALUES
(104, 'Jane', 'IT', 50000),
(102, 'John', 'IT', 55000),
(103, 'Michael', 'Finance', 60000),
(101, 'Alice', 'HR', 45000);

SELECT * FROM employees;

Output:

Clustered_and_Non_Clustered_Indexes1

Now, We will create a Non-Clustered Index on the emp_name column to improve query performance when searching by name.

Query:

CREATE NONCLUSTERED INDEX idx_employees_emp_name
ON employees(emp_name ASC);

EXEC sp_helpindex 'employees';

Output:

Clustered_and_Non_Clustered_Indexes13

Optimizing Queries with Clustered & Non-Clustered Indexes

1. SELECT Queries with WHERE Clause

Clustered Index: When a SELECT query uses a WHERE clause on the indexed column, the database can use the clustered index to quickly locate matching records.

Query:

SELECT * FROM employees
WHERE emp_name = 'John';

Output:

Clustered_and_Non_Clustered_Indexes3

Non-Clustered Index: When a non-clustered index is created on the emp_name column, SQL can use it to locate matching records efficiently

Query:

SELECT * FROM employees
WHERE emp_name = 'Alice';

Output:

Clustered_and_Non_Clustered_Indexes4

2. UPDATE Queries

Clustered Index: When updating a row in a table with a Clustered Index, the database can quickly locate the row to modify based on the indexed column.

Example: Suppose we want to update the department of an employee named Michael.

Query:

UPDATE employees
SET department = 'HR'
WHERE emp_name = 'Michael';

SELECT * FROM employees
WHERE emp_name = 'Michael';

Output:

Clustered_and_Non_Clustered_Indexes5

Non-Clustered Index: When updating a row using the indexed column in the WHERE clause, SQL Server can use the non-clustered index to locate the matching record.

Example: Suppose we want to update the department of an employee named Alice from 'HR' to 'Finance'.

Query:

UPDATE employees
SET department = 'Finance'
WHERE emp_name = 'Alice';

SELECT * FROM employees
WHERE emp_name = 'Alice';

Output:

Clustered_and_Non_Clustered_Indexes6

3. JOIN Queries

Clustered Index: When performing a JOIN operation between two tables, SQL can use a clustered index on the join column to efficiently locate and match records.

Query:

-- Create another table for JOIN
CREATE TABLE projects (
project_id INT,
emp_id INT,
project_name VARCHAR(50)
);

INSERT INTO projects VALUES
(1, 101, 'Website Development'),
(2, 102, 'Database Management'),
(3, 103, 'Cloud Computing'),
(4, 104, 'Software Testing');

-- Join query
SELECT
e.emp_id,
e.emp_name,
p.project_name
FROM employees e
JOIN projects p
ON e.emp_id = p.emp_id;

Output:

Clustered_and_Non_Clustered_Indexes7

Non-Clustered Index: When performing a JOIN operation on the emp_name column, SQL Server can use the non-clustered index to locate matching employee records efficiently. This can improve query performance by reducing the time required to find matching rows.

Query:

CREATE TABLE projects (
project_id INT,
emp_name VARCHAR(50),
project_name VARCHAR(50)
);

INSERT INTO projects VALUES
(1, 'Jane', 'Software Testing'),
(2, 'John', 'Database Management'),
(3, 'Michael', 'Cloud Computing'),
(4, 'Alice', 'Website Development');

-- Join query
SELECT
e.emp_id,
e.emp_name,
p.project_name
FROM employees e
JOIN projects p
ON e.emp_name = p.emp_name;

Output:

Clustered_and_Non_Clustered_Indexes8

4. View

Clustered Index: When a view retrieves data from a table with a clustered index, the database can use the underlying index to locate matching records efficiently.

Query:

CREATE VIEW employee_view AS
SELECT emp_id, emp_name, department, salary
FROM employees;

-- Retrieve records from the view
SELECT * FROM employee_view
WHERE emp_name = 'John';

Output:

Clustered_and_Non_Clustered_Indexes9

Non-Clustered Index: SQL can also use a non-clustered index on the underlying table when a view retrieves matching records.

Query:

SELECT * FROM employee_view
WHERE emp_name = 'Jane';

Output:

Clustered_and_Non_Clustered_Indexes10
Comment