Types of Indexes in SQL: Clustered, Unique, Composite
Every index type sorted by structure, storage and rule: B-tree, hash, clustered, secondary, unique, composite, covering, partial and full-text.
In this chapter
Index types: structure, storage, rule
Types of indexes: common questions
What are the main types of indexes in SQL?
By structure: B-tree, hash, full-text. By storage: clustered and secondary (non-clustered). By rule: unique, composite (multi-column), covering and partial or filtered.
More on Indexes: how they are built
Keep learning
Overview
A tree whose leaf pages hold the full rows in key order A separate tree whose leaves hold the key and a pointer to the row ann → 4 bob → 1 dee → 2 Values stored row by row Values stored column by column A unique index rejects a repeated key INSERT b@x → Error 1062 Duplicate entry A filtered index only holds rows that pass a predicate index WHERE 3 of 6 rows Where do the rows live? Either the index holds the rows themselves, or it holds pointers to rows stored somewhere else. the table itself The rows are stored inside the index, sorted by its key. A lookup lands on the leaf and the whole row is already there. Only one per table, because rows can be sorted only one way. InnoDB: always the PRIMARY KEY. A separate tree sorted by another column, holding key → pointer. A lookup finds the pointer, then makes a second trip to fetch the row. You can have many. Every extra INDEX (name), INDEX (email)… In InnoDB the pointer is the primary key. How are the bytes laid out? The disk is read a page at a time, so what sits together on a page decides how much is read. apps (OLTP) All columns of one row sit together on a page. Reading or writing one whole record touches one page. The default in MySQL, PostgreSQL and SQL Server. reports (OLAP) All values of one column sit together. SUM or AVG over millions of rows reads only that column, and it compresses well. Which rows may it hold? On top of structure and layout, an index can enforce a rule or cover only part of the table. No two rows may share the key. The INSERT or UPDATE that would create a duplicate fails with an error. Emails, usernames, order numbers. A PRIMARY KEY is always unique. Only rows that pass a WHERE condition are indexed, so the index is smaller and quicker when queries ask for that subset. PRIMARY KEY (always) CLUSTERED INDEX (optional) CLUSTER command, one-off sort every other index NONCLUSTERED INDEX every index not in InnoDB COLUMNSTORE INDEX extensions only not supported (index a generated column instead) CREATE INDEX … WHERE partial index: CREATE INDEX … WHERE chapters · indexes Clustered, unique, columnstore: three questions behind every index name Structure — where do the rows live? Storage — how are the bytes laid out on disk? Rule — which rows may the index hold? So one index can be several at once. CREATE UNIQUE INDEX ON users(email) in MySQL is non-clustered, rowstore and unique. New to indexes? Read what an index is, and why a database needs one first. This chapter builds on it.
4. Background: how indexes grew into B+ trees
Picture a data file sorted by key and split into blocks. An index can point at it in three ways, each smaller than the last. The third, an index of an index, is exactly what a B+ tree is, built so it can grow and shrink as rows come and go. Three ways to point at sorted data Finding one key One per key Find the key in the index and jump straight to its record The biggest One per block, holding the first key of the block Take the largest entry that is not bigger than the key, read that block, scan inside it. Looking for 121 with entries 100, 120, 140 lands in the block of 120. Smaller. The data must be sorted by the key. A sparse index over the sparse index, repeated until one block is left One read per layer, then the data block Only the small top layers need to stay in memory. The B+ tree is this idea.
5. What each database calls it
Support by engine MySQL (InnoDB) SQL Server ? Older textbooks use these words differently Some course notes call an index primary when the data file is sorted by the primary key, clustered when rows that share a non-key value, such as a department id, are stored together in one block, and secondary for an index on any other field. In InnoDB and SQL Server, clustered means the table itself is stored in index order. This course uses the database meaning. → Where each one is explained Clustered and non-clustered walks a lookup through a clustered and a secondary index, reads SHOW INDEXES, and tests the leftmost prefix rule. Rowstore, columnstore, unique, and filtered compares row and column layout, and the unique and filtered rules.
More courses
- Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
- Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
- React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
- System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
- NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
- Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
- Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
- AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
- Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
- TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
- CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
- Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
- Docker — What an image is, why layers cache, and how containers find each other on a network.
- Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
- Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
- AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
- Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.