← Back to Questions
SQL

What is an index in SQL?

Learn What is an index in SQL? with simple explanations, real-time examples, interview tips and practical use cases.

What is an Index in SQL?

An Index in SQL is a database object used to improve the speed of data retrieval operations.

In simple words:

An index helps the database find data faster without scanning the entire table.


Why Index is Important

Databases may contain:

  • Thousands of rows
  • Millions of rows
  • Billions of rows

Without indexes:

  • Database performs full table scan

This becomes:

  • Slow
  • Expensive
  • Inefficient

Simple Real-Life Example

Think about a book.

Without index:

  • You search every page manually

With index:

  • You directly jump to required topic

Same Concept in SQL

Database indexes help:

  • Locate rows quickly

What Happens Without Index?

Suppose a table contains:

10 million users

Query:

SELECT *
FROM users
WHERE email = 'naresh@gmail.com';

Without Index

Database checks:

  • Row 1
  • Row 2
  • Row 3
  • ...
  • Until match found

This is Called

Full Table Scan

Problem

  • Very slow
  • High CPU usage
  • High disk reads

What Happens With Index?

Database uses:

Indexed structure

to directly locate required rows.


Result

  • Faster search
  • Lower disk access
  • Better performance

Index Internal Architecture

Search Query
      |
      v
Index Lookup
      |
      v
Locate Matching Row
      |
      v
Fetch Data

Real-Time Example

Suppose:

Students Table

Student ID Name Email
1 Naresh naresh@gmail.com
2 Rahul rahul@gmail.com
3 Arjun arjun@gmail.com

Query Example

SELECT *
FROM students
WHERE email = 'rahul@gmail.com';

Without Index

Database scans:

  • Every row

With Index

Database directly jumps to:

rahul@gmail.com

How to Create Index

CREATE INDEX index_name
ON table_name(column_name);

Example

CREATE INDEX idx_email
ON students(email);

What Happens Internally?

Database creates:

Special searchable structure

for:

email column

Index Visualization

Index Structure
-----------------------

arjun@gmail.com  -> Row Pointer
naresh@gmail.com -> Row Pointer
rahul@gmail.com  -> Row Pointer

-----------------------

Actual Table Data

Types of Indexes

  • Clustered Index
  • Non-Clustered Index
  • Unique Index
  • Composite Index
  • Full-Text Index

1. Clustered Index

Stores:

  • Actual table data in sorted order

Important Point

Only:

One clustered index

allowed per table.


2. Non-Clustered Index

Stores:

  • Indexed values
  • Pointers to rows

Important Point

Multiple non-clustered indexes allowed.


3. Unique Index

Ensures:

  • No duplicate values

Example

CREATE UNIQUE INDEX idx_email
ON students(email);

4. Composite Index

Index created on:

  • Multiple columns

Example

CREATE INDEX idx_name_city
ON students(name, city);

5. Full-Text Index

Used for:

  • Text searching
  • Search engines

Index Query Flow

User Query
      |
      v
Check Index
      |
      v
Locate Row Position
      |
      v
Fetch Actual Data

Why Indexes Improve Performance

Indexes reduce:

  • Number of rows scanned

Result

  • Faster query execution
  • Better scalability

Where Indexes are Commonly Used

  • Primary Keys
  • Foreign Keys
  • Search columns
  • Frequently filtered columns
  • JOIN conditions

Real-Time Banking Example

Banking systems use indexes on:

  • account_number
  • customer_id
  • transaction_id

Why?

To retrieve customer and transaction data quickly.


Real-Time E-Commerce Example

E-commerce platforms use indexes on:

  • product_id
  • product_name
  • customer_email

Example Query

SELECT *
FROM products
WHERE product_name = 'Laptop';

Real-Time Learning Platform Example

Learning platforms use indexes on:

  • course_id
  • student_email
  • topic_slug

Index in Microservices

Microservices heavily depend on indexes for:

  • Fast APIs
  • Scalable databases
  • High-performance queries

Example

GET /students/{id}

uses indexed:

student_id

Advantages of Indexes

  • Improves SELECT performance
  • Speeds up searches
  • Optimizes JOIN operations
  • Improves sorting and filtering

Disadvantages of Indexes

  • Consumes extra storage
  • Slows INSERT operations
  • Slows UPDATE operations
  • Slows DELETE operations

Why INSERT Becomes Slower

Because:

  • Indexes must also be updated

Example

INSERT INTO students
VALUES (...);

Database Must Update

  • Table data
  • All related indexes

Too Many Indexes Problem

Too many indexes may:

  • Reduce write performance
  • Increase storage usage

Best Practice

Create indexes only on:

  • Frequently searched columns

Index vs Primary Key

Feature Index Primary Key
Purpose Improve performance Uniquely identify rows
Duplicates Allowed Depends No
Number Allowed Multiple One

Index vs Full Table Scan

Feature Index Scan Full Table Scan
Speed Fast Slow
Rows Scanned Few All
Performance Better Poor

How Database Chooses Index

Database optimizer checks:

  • Query conditions
  • Table size
  • Available indexes

Then Chooses

Best execution plan


Performance Optimization Tips

  • Index frequently searched columns
  • Index JOIN columns
  • Avoid indexing unnecessary columns
  • Monitor slow queries regularly

Common Interview Mistake

Many developers think:

  • Indexes improve everything

Reality

Indexes mainly improve:

  • Read operations

But may reduce:

  • Write performance

Related Learning Topics


Professional Interview Answer

An Index in SQL is a database object used to improve the speed of data retrieval operations. Indexes help the database quickly locate rows without scanning the entire table. They are commonly created on frequently searched columns, primary keys, foreign keys, and JOIN conditions. Indexes significantly improve SELECT query performance but may slightly reduce INSERT, UPDATE, and DELETE performance because indexes must also be maintained. Common types of indexes include clustered indexes, non-clustered indexes, unique indexes, composite indexes, and full-text indexes.


Why Interviewers Like This Answer

  • Clearly explains indexing concept
  • Includes internal architecture
  • Shows performance understanding
  • Provides real-world enterprise examples
  • Explains advantages and trade-offs

Frequently Asked Questions

What is an index in SQL?

An index is a database object used to speed up data retrieval operations.

Why indexes are used?

Indexes improve query performance and reduce table scanning.

Do indexes improve INSERT performance?

No, indexes may slow INSERT operations because indexes must also be updated.

Can a table have multiple indexes?

Yes, a table can have multiple indexes.

What are common types of indexes?

Clustered, non-clustered, unique, composite, and full-text indexes.

Why this SQL question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.