SQL Indexes




πŸ’» Introduction

An Index in SQL improves the speed of data retrieval from a database table, much like an index in a book helps you locate information quickly. Indexes are especially important for large tables and frequently queried columns.

1️⃣ CREATE INDEX – Creating an Index

The CREATE INDEX statement builds an index on one or more columns of a table.

CREATE INDEX idx_employee_lastname
ON Employees (LastName);

Create a unique index to enforce uniqueness:

CREATE UNIQUE INDEX idx_employee_email
ON Employees (Email);
Tip: Use indexes on columns that are frequently used in WHERE, JOIN, or ORDER BY clauses.

2️⃣ DROP INDEX – Removing an Index

To delete an existing index, use DROP INDEX. Syntax may vary slightly depending on the database:

-- SQL Server / Oracle
DROP INDEX idx_employee_lastname;

-- MySQL
DROP INDEX idx_employee_lastname ON Employees;

3️⃣ Types of Indexes

  • Single-column Index – Built on one column.
  • Composite (Multi-column) Index – Built on two or more columns.
  • Unique Index – Ensures that all values in the indexed column are unique.
  • Clustered Index – Reorders the physical storage of data to match the index (SQL Server).
  • Non-clustered Index – Creates a separate structure for quick lookups without altering table order.
Tip: While indexes speed up SELECT queries, they can slow down INSERT, UPDATE, and DELETE operations. Use them wisely.

βœ… Key Takeaways

  • CREATE INDEX β†’ Speeds up query performance on large tables.
  • DROP INDEX β†’ Removes an index when no longer needed.
  • Use indexes on frequently searched or joined columns for optimal results.
  • Too many indexes can slow down data modifications.

πŸ“˜ Quick Reference – SQL Index Commands

Operation SQL Command
Create Index CREATE INDEX idx_name ON table(column);
Create Unique Index CREATE UNIQUE INDEX idx_name ON table(column);
Drop Index DROP INDEX idx_name;

πŸ“š Continue Learning

πŸ’¬ Got a question about SQL Indexes? Drop it in the comments below and let’s discuss!




Leave a Comment