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

Non-Clustered Index

A Non-Clustered Index is a separate structure that helps SQL Server find data quickly without changing the physical order of the table.

What is a Non-Clustered Index?

A Non-Clustered Index is an index structure that is stored separately from the actual table data.

It contains the indexed column values along with a reference that allows SQL Server to locate the actual rows in the table.

Unlike a clustered index, a non-clustered index does not determine the physical order of the table data.

Important Difference

A clustered index determines the physical order of table data, whereas a non-clustered index is a separate structure that contains indexed values and row references.

Syntax

CREATE NONCLUSTERED INDEX index_name
ON table_name(column_name);

Example Table

Consider the following Students table:

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

Creating a Non-Clustered Index

Suppose we frequently search students based on the City column.

We can create a non-clustered index on the City column:

CREATE NONCLUSTERED INDEX IX_Students_City
ON Students(City);

Here:

  • IX_Students_City is the index name.
  • Students is the table name.
  • City is the indexed column.

Sample Data

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

Using the Non-Clustered Index

Now suppose we want to find all students from Pune:

SELECT *
FROM Students
WHERE City = 'Pune';

If SQL Server determines that the non-clustered index is useful for the query, it can use the index to locate the required rows efficiently.

Multiple Non-Clustered Indexes

Unlike a clustered index, a table can have multiple non-clustered indexes.

For example, we can create indexes on both City and Course.

CREATE NONCLUSTERED INDEX IX_Students_City
ON Students(City);

CREATE NONCLUSTERED INDEX IX_Students_Course
ON Students(Course);

Both indexes can exist on the same table.

Non-Clustered Index on Multiple Columns

A non-clustered index can also contain more than one column. Such an index is called a composite or multi-column index.

CREATE NONCLUSTERED INDEX IX_Students_City_Course
ON Students(City, Course);

This index contains both City and Course columns.

Including Additional Columns

SQL Server also allows additional non-key columns to be included in a non-clustered index.

CREATE NONCLUSTERED INDEX IX_Students_City
ON Students(City)
INCLUDE(StudentName, Course);

Here, City is the index key and StudentName and Course are included columns.

Advantages of Non-Clustered Index

  • A table can have multiple non-clustered indexes.
  • They can improve search performance.
  • They are useful for frequently searched columns.
  • They can improve queries using WHERE conditions.
  • They can be useful for JOIN and ORDER BY operations.
  • Included columns can help cover frequently executed queries.

Disadvantages of Non-Clustered Index

  • Non-clustered indexes require additional storage.
  • Too many indexes can slow down INSERT operations.
  • UPDATE and DELETE operations may require index maintenance.
  • Indexes need to be maintained as table data changes.

Clustered Index vs Non-Clustered Index

Clustered Index Non-Clustered Index
Determines the physical order of table data. Does not determine the physical order of table data.
Only one clustered index can exist on a table. Multiple non-clustered indexes can exist on a table.
Data rows are associated directly with the clustered index order. The index contains keys and references to the actual rows.
Useful for range-based retrieval. Useful for targeted searches and filtering.

Important Points

  • A table can have multiple non-clustered indexes.
  • A non-clustered index is stored separately from the actual table data.
  • Non-clustered indexes can improve SELECT query performance.
  • Indexes require additional storage space.
  • Too many indexes can negatively affect INSERT, UPDATE and DELETE operations.

Summary

A Non-Clustered Index is a separate index structure that helps SQL Server locate rows efficiently. It does not change the physical order of data in the table.

Unlike a clustered index, a table can contain multiple non-clustered indexes.

Non-clustered indexes are useful for frequently searched, filtered, joined and sorted columns, but they should be created carefully because they require additional storage and maintenance.