Master SQL Server From Scratch

Clear, interactive, and structured SQL Server lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to SQL Server
Introduction to SQL Server SQL Server Installation SQL Server Fundamentals
2. Data Types
Data Types Numeric Data Types Character & String Data Types Date & Time Data Types Other SQL Server Data Types
3. Database & Tables
Database & Tables Introduction Creating a Database Creating Tables Table Structure Altering Tables Dropping Tables
4. Constraints
Constraints Introduction PRIMARY KEY FOREIGN KEY UNIQUE Constraint NOT NULL & DEFAULT CHECK Constraint
5. SQL Basics
SQL Basics Introduction SQL Commands SQL Command Categories SELECT Statement INSERT, UPDATE & DELETE
6. CRUD Operations
CRUD Operations Introduction INSERT Operation SELECT Operation UPDATE Operation DELETE Operation
7. SQL Queries
Queries Introduction WHERE Clause ORDER BY Clause DISTINCT LIKE Operator IN, BETWEEN & IS NULL
8. Joins
Joins Introduction INNER JOIN LEFT JOIN RIGHT JOIN FULL OUTER JOIN CROSS JOIN
9. SQL Functions
SQL Functions Introduction String Functions Numeric Functions Date & Time Functions Conversion Functions NULL Functions
10. Grouping & Aggregate Functions
GROUP BY Aggregate Functions COUNT() SUM() AVG() MIN() & MAX()
11. Subqueries
Subqueries Introduction Single-Row Subquery Multiple-Row Subquery Correlated Subquery Subquery with DML
12. Views
SQL Views Introduction Creating a View Updating a View Altering a View Dropping a View
13. Stored Procedures
Stored Procedures Introduction Creating a Stored Procedure Input Parameters Output Parameters Executing & Modifying Stored Procedures
14. Triggers
Triggers Introduction AFTER INSERT Trigger AFTER UPDATE Trigger AFTER DELETE Trigger INSTEAD OF Trigger
15. Transactions
Transactions Introduction COMMIT ROLLBACK SAVEPOINT Transaction with TRY...CATCH
16. Indexes
Indexes Introduction Clustered Index Non-Clustered Index Creating & Dropping Indexes Index Performance
17. Common Table Expressions
CTE Introduction Simple CTE CTE with JOIN CTE with Aggregate Functions Recursive CTE
18. Window Functions
Window Functions Introduction ROW_NUMBER() RANK() and DENSE_RANK() LEAD() and LAG() PARTITION BY
19. Advanced SQL
Advanced SQL Introduction Temporary Tables Table Variables Dynamic SQL Error Handling Performance Optimization

Clustered Index

What is a Clustered Index?

A Clustered Index determines the physical order in which data rows are stored in a table.

The data rows of the table are stored according to the clustered index key. Because the actual table data is arranged according to the clustered index, a table can have only one clustered index.

How Does a Clustered Index Work?

Suppose we have a Students table containing the following records:

StudentId    StudentName    City
--------------------------------
103          Rahul          Pune
101          Amit           Mumbai
105          Neha           Pune
102          Priya          Delhi
104          Rohit          Nashik

If we create a clustered index on StudentId, SQL Server organizes the table data according to the StudentId values.

StudentId    StudentName    City
--------------------------------
101          Amit           Mumbai
102          Priya          Delhi
103          Rahul          Pune
104          Rohit          Nashik
105          Neha           Pune

Creating a Clustered Index

A clustered index can be created using the CREATE CLUSTERED INDEX statement.

CREATE CLUSTERED INDEX IX_Students_StudentId
ON Students(StudentId);

Here:

  • IX_Students_StudentId is the index name.
  • Students is the table name.
  • StudentId is the indexed column.

Clustered Index with Primary Key

When a primary key is created, SQL Server generally creates a clustered index for the primary key by default, unless another index option is specified.

CREATE TABLE Students
(
    StudentId INT PRIMARY KEY,
    StudentName VARCHAR(100),
    City VARCHAR(50),
    Age INT
);

In this example, the primary key on StudentId can have a clustered index associated with it.

Only One Clustered Index

A table can have only one clustered index because the actual data rows can be physically ordered in only one way.

For example, we can create a clustered index on StudentId:

CREATE CLUSTERED INDEX IX_Students_StudentId
ON Students(StudentId);

We cannot create another clustered index on the same table while the existing clustered index is present.

Example Query

Consider the following query:

SELECT *
FROM Students
WHERE StudentId = 103;

If StudentId is the clustered index key, SQL Server can efficiently locate the required row using the index structure.

Advantages of Clustered Index

  • Improves data retrieval performance.
  • Data is physically organized according to the clustered index key.
  • Useful for range-based queries.
  • Can improve ORDER BY operations on the indexed key.
  • Useful when records are frequently searched using the indexed column.

Disadvantages of Clustered Index

  • A table can have only one clustered index.
  • Insert and update operations may require data reorganization.
  • Creating and maintaining indexes requires additional database resources.

Complete Example

Create a Students table:

CREATE TABLE Students
(
    StudentId INT,
    StudentName VARCHAR(100),
    City VARCHAR(50),
    Age INT
);

Insert sample records:

INSERT INTO Students
VALUES
(103, 'Rahul', 'Pune', 22),
(101, 'Amit', 'Mumbai', 21),
(105, 'Neha', 'Pune', 23),
(102, 'Priya', 'Delhi', 22),
(104, 'Rohit', 'Nashik', 24);

Create a clustered index:

CREATE CLUSTERED INDEX IX_Students_StudentId
ON Students(StudentId);

Now SQL Server can organize the table data according to StudentId.

Important

A clustered index is closely related to the physical organization of table data. Since the actual data rows can have only one physical order, a table can have only one clustered index.

Important Points

  • A clustered index determines the physical order of data rows.
  • A table can have only one clustered index.
  • Clustered indexes are useful for fast data retrieval.
  • Primary keys are commonly associated with clustered indexes by default.
  • Clustered indexes are useful for range queries.

Summary

A Clustered Index determines the physical order of data rows in a SQL Server table. The data is organized according to the clustered index key.

A table can have only one clustered index because the table data can have only one physical order.

Clustered indexes are useful for improving data retrieval performance, especially when searching and retrieving data using the indexed key.