Creating Indexes in SQL
SQL Creating Indexes tutorial with examples for beginners
Indexes in SQL are special data structures that improve the speed of data retrieval operations on a table. They work like an index in a book—helping the database find rows faster without scanning the entire table.
In this beginner-friendly SQL tutorial, you will learn:
What SQL indexes are
How indexes work
Types of indexes
How to create, view, and delete indexes
Index best practices
Real-world examples
An index is a database object that speeds up data retrieval by creating a quick lookup structure based on one or more columns.
Indexes improve SELECT performance but may slow down:
INSERT
UPDATE
DELETE
Because the index must also be updated.
CREATE INDEX index_name
ON table_name (column_name);
| id | name | city | |
|---|---|---|---|
| 1 | Amit | amit@example.com | Delhi |
| 2 | Neha | neha@example.com | Mumbai |
| 3 | Rahul | rahul@example.com | Pune |
CREATE INDEX idx_users_city
ON users (city);
This makes city-based search faster:
SELECT * FROM users WHERE city = 'Delhi';
Ensures no duplicate values.
CREATE UNIQUE INDEX idx_unique_email
ON users (email);
Useful when queries filter by multiple columns.
CREATE INDEX idx_name_city
ON users (name, city);
Speeds up:
SELECT * FROM users WHERE name = 'Amit' AND city = 'Delhi';
| Index Type | Description |
|---|---|
| Single-column index | Index on one column |
| Composite index | Index on two or more columns |
| Unique index | Prevents duplicates |
| Full-text index | Speeds up text searches |
| Clustered index | Sorts table rows physically (SQL Server) |
| Non-clustered index | Logical pointer-based index |
CREATE FULLTEXT INDEX idx_article_content
ON articles (content);
Improves performance of:
SELECT * FROM articles WHERE MATCH(content) AGAINST('database');
SHOW INDEXES FROM users;
SELECT * FROM pg_indexes WHERE tablename = 'users';
EXEC sp_helpindex 'users';
MySQL:
DROP INDEX idx_users_city ON users;
PostgreSQL:
DROP INDEX idx_users_city;
SQL Server:
DROP INDEX users.idx_users_city;
CREATE INDEX idx_product_name
ON products (name);
CREATE UNIQUE INDEX idx_user_email
ON users (email);
CREATE INDEX idx_user_date
ON orders (user_id, order_date);
β Index columns used frequently in WHERE, JOIN, ORDER BY
β Use composite indexes when queries use multiple columns
β Avoid indexing small tables (no performance benefit)
β Do not index frequently updated columns
β Avoid too many indexes—slows down write operations
β Use UNIQUE index to enforce data integrity
In this tutorial on Creating Indexes in SQL, you learned:
What indexes are and how they work
How to create single-column, composite, and unique indexes
How to view and drop indexes
Best practices for efficient indexing
Indexes are essential for improving database performance—but must be used wisely.