Indexes

SQL · Extra Concepts

Theory

An index is a separate, sorted lookup structure that lets the database find rows without reading the whole table, just like the index at the back of a book. Most databases use a B-tree: a balanced tree of sorted keys where each key points to the matching rows. To find dept = 'IT' among a million rows, the engine walks a few tree levels instead of scanning every row, turning a full scan into a handful of page reads.

Indexes help WHERE filters, JOIN conditions, ORDER BY and MIN/MAX on the indexed columns. A composite index on (dept, salary) also helps queries filtering on dept alone, but usually not on salary alone, because the tree is sorted by the first column first (the leftmost-prefix rule). The cost is that every INSERT, UPDATE and DELETE must also update each index, and indexes take extra storage. Columns with very few distinct values, like a yes/no flag, rarely benefit.

Why we use it

Without an index the database must scan every row to answer a filter, which gets slower as the table grows. An index makes lookups on the indexed columns fast, often reducing seconds to milliseconds.

When to use it

Index columns used often in WHERE, JOIN and ORDER BY on large tables, especially selective ones like ids, emails and dates. Avoid indexing tiny tables, low-variety columns and write-heavy tables where the update cost outweighs faster reads.

How it works

  1. Query arrives: WHERE dept = 'IT'
  2. Is there an index on dept? Yes: Walk the B-tree to the 'IT' keys. No: Scan every row of the table.
  3. Follow pointers to fetch only the matching rows
  4. Index holds all needed columns? Yes: Answer from the index alone. No: Read the rest from the table.
  5. Return rows with far fewer reads

Syntax

CREATE INDEX idx_name ON table (column);

Example

CREATE INDEX idx_employees_department ON employees (department);

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic