softwaretips4u Tips that Transform
← Back to Homepage ← Back to Articles

Indexes in SQL

🔹 What is an Index?

An index is a database object that improves the speed of data retrieval by creating a quick lookup structure, similar to a book index.

🔹 Types of Indexes

CREATE INDEX idx_employee_name ON employees(name);
CREATE INDEX idx_emp_dept_salary ON employees(dept_id, salary);
CREATE UNIQUE INDEX idx_email ON users(email);

🔹 Pros and Cons

Pros: Faster SELECT queries; useful for JOIN and WHERE conditions.
Cons: Slows INSERT/UPDATE/DELETE (index updates) and consumes extra storage.

🔹 Example Query Using Index

SELECT *
FROM employees
WHERE dept_id = 10 AND salary > 50000;
-- Faster with index on (dept_id, salary)
← Back to Articles | 🏠 Back to Homepage