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

Creating & Dropping Indexes

Learn how to create, view and remove indexes in SQL Server.

Introduction

Indexes are database objects used to improve the performance of data retrieval operations.

SQL Server provides the CREATE INDEX statement to create indexes and the DROP INDEX statement to remove indexes that are no longer required.

Example Table

First, create a Students table:

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

Creating a Non-Clustered Index

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 name of the index and City is the indexed column.

CREATE INDEX Syntax

CREATE INDEX index_name
ON table_name(column_name);

If the index type is not specified, SQL Server creates a non-clustered index by default.

Creating a Clustered Index

A clustered index can be created using:

CREATE CLUSTERED INDEX IX_Students_StudentId
ON Students(StudentId);
Note: A table can have only one clustered index.

Creating a Unique Index

A unique index prevents duplicate values in the indexed column or combination of columns.

CREATE UNIQUE INDEX IX_Students_UniqueId
ON Students(StudentId);

This ensures that duplicate StudentId values are not allowed through this unique index.

Creating an Index on Multiple Columns

We can create an index using more than one column.

CREATE INDEX IX_Students_City_Course
ON Students(City, Course);

This is called a composite or multi-column index.

Viewing Indexes

SQL Server provides system catalog views that can be used to view information about indexes.

SELECT
    name AS IndexName,
    type_desc AS IndexType
FROM sys.indexes
WHERE object_id = OBJECT_ID('Students');

This query displays the indexes associated with the Students table.

Dropping an Index

When an index is no longer required, it can be removed using the DROP INDEX statement.

DROP INDEX IX_Students_City
ON Students;

Here, IX_Students_City is the index that will be removed from the Students table.

Removing Unused Indexes

Unused indexes can consume storage and require maintenance when table data changes.

Therefore, indexes that are no longer useful should be reviewed and removed carefully.

DROP INDEX IX_Students_City
ON Students;

Checking Before Dropping

Before dropping an index, it is useful to verify that the index exists.

IF EXISTS
(
    SELECT 1
    FROM sys.indexes
    WHERE name = 'IX_Students_City'
      AND object_id = OBJECT_ID('Students')
)
BEGIN

    DROP INDEX IX_Students_City
    ON Students;

END;

Why Should Indexes Be Managed Carefully?

Although indexes improve SELECT query performance, every index also requires storage and maintenance.

  • Indexes consume additional storage.
  • INSERT operations may require index updates.
  • UPDATE operations may require index maintenance.
  • DELETE operations may require index maintenance.
  • Too many unnecessary indexes can affect write performance.

Complete Example

Create the table:

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

Create an index:

CREATE INDEX IX_Students_City
ON Students(City);

Use the table:

SELECT *
FROM Students
WHERE City = 'Pune';

Remove the index when it is no longer required:

DROP INDEX IX_Students_City
ON Students;

Important Points

  • Use CREATE INDEX to create an index.
  • Use CREATE CLUSTERED INDEX for a clustered index.
  • Use CREATE NONCLUSTERED INDEX for a non-clustered index.
  • A table can have only one clustered index.
  • A table can have multiple non-clustered indexes.
  • Use DROP INDEX to remove an index.
  • Unnecessary indexes should be removed carefully.

Summary

SQL Server provides commands for creating and removing indexes. The CREATE INDEX statement is used to create an index, while the DROP INDEX statement is used to remove it.

Indexes can significantly improve data retrieval performance, but they also require storage and maintenance.

Therefore, indexes should be created based on actual query requirements and unnecessary indexes should be avoided.