Index Range Scan Explained: BETWEEN, > and < on a B-Tree
A range condition finds the first matching leaf, then walks right until the range ends. Why columns after a range in a composite index stop helping.
In this chapter
A range is a slice of the leaf
More on Indexes: how queries use them
Keep learning
Overview
Oct 1, 00:00 Oct 15, 10:00 Oct 31, 00:00 Oct 31, 14:30 Nov 1, 00:00 chapters · indexes A range is a slice of the leaf Equality lands on one key. A range lands on the first key that qualifies and then walks neighbors until the condition fails. Those neighbors sit together because the leaf is sorted.
1. BETWEEN stays on one stretch of the leaf
= 15 && price in the slice WHERE price BETWEEN 15 AND 46
2. LIKE: only the text before the first % counts
PostgreSQL needs the right operator class for a LIKE prefix to use a normal index (text_pattern_ops) unless the database uses the C collation. Words in the middle of text are a job for full-text or trigram indexes, not B-trees.
3. “This month's orders”: the BETWEEN trap
created_at holds a date and a time. '2026-10-31' means Oct 31 at 00:00, so the orders placed later that day fall off the end. BETWEEN misses a day Start inclusive, end exclusive Which orders each query returns Order placed BETWEEN … AND '2026-10-31' >= Oct 1 AND < Nov 1 ✓ returned ✕ left out Oct 31 at 14:30 is a real October order, and only the second query returns it. Both versions can use a plain index on created_at. Wrapping the column in MONTH() or DATE_FORMAT() cannot.
4. A covering index never visits the table
An index on price already stores price in the leaf. SELECT price WHERE price BETWEEN 15 AND 46 can answer from that leaf. SELECT name, price needs the name. If name is not in the index, each hit becomes another lookup in the table page. SELECT price FROM ProductsWHERE price BETWEEN 15 AND 46 The engine calls this an index-only scan. It is a plan, not a property of the index, so adding one more column to the query can silently turn it off: Index (shop_id, amount) ✓ Everything needed is in the index. The table is never touched. Same index, one more condition ✕ created_at is not in the index, so every entry of shop 2 sends the engine to the table. The query asks for fewer rows and can run slower. Check EXPLAIN after changing a query. Do not add columns to an index on suspicion: each one makes every write a little heavier.
5. Two ranges in one query
≠ An index is a chain, not a chessboard In WHERE price < 50 AND created_at > ? only one range can define the slice. The other is checked on every entry the slice returns. Put the more selective one first. Two separate indexes instead force the engine to read both and merge them, which is usually slower than one good multi-column index. Two ranges that cannot be avoided, such as latitude and longitude, need a spatial index: see the special cases chapter. → Exception: skip scan
6. Turn a range into an equality
A range breaks the index in two ways: the columns after it cannot narrow the scan, and they stop being in order. Take the popular posts of a feed: Range first ✕ Scans the whole view_count range, then sorts by date. Flag first ✓ Seeks to (published, 1) and reads 20 entries in date order. Ways to get the flag Any engine A plain column that the application keeps up to date. It works everywhere, but you must keep it correct.
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.