INDEX

An INDEX in SQL is used to speed up the retrieval of rows from a table. It works like an index in a book — allowing the database engine to find data more quickly without scanning the entire table.

Indexes are especially helpful on columns that are frequently used in WHERE, JOIN, ORDER BY, and GROUP BY clauses.


Syntax

CREATE INDEX index_name
ON table_name (column_name);

To drop an index:

DROP INDEX index_name ON table_name;

Example: Create an Index on Name

CREATE INDEX idx_name
ON employees (name);

This index helps speed up queries like:

SELECT * FROM employees
WHERE name = 'Alice';

Composite Index

You can also create indexes on multiple columns:

CREATE INDEX idx_name_gender
ON employees (name, gender);

This benefits queries that filter on both columns together.

Key Points

  • Indexes improve read/query performance
  • But they can slow down write operations like INSERT, UPDATE, and DELETE
  • Too many indexes can degrade performance — use them wisely