Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How do I filter rows that fall within an inclusive numeric range in SQLite?

Quick answer

Use price BETWEEN 10 AND 20 to include prices from 10 through 20, including both endpoints. The equivalent conditions are price >= 10 AND price <= 20. For the example prices 8, 10, 15, 20, and 22, either query returns the rows priced 10, 15, and 20; 8 and 22 are excluded. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have a SQLite table of products and want prices from 10 through 20, including products priced exactly 10 or 20. What should the query look like?

Geez

Use BETWEEN for a compact inclusive range condition. Assuming your table is named products and the numeric column is named price, write:

SELECT name, price
FROM products
WHERE price BETWEEN 10 AND 20
ORDER BY price;

In SQLite, price BETWEEN 10 AND 20 is logically equivalent to price >= 10 AND price <= 20, so both endpoints are included. [S1] The names and sample values here are illustrative; replace them with your actual table and column names. ORDER BY price is included to make the example output easy to read.

Follow-up question

Can you show exactly which rows that returns, and how the two conditions compare?

Geez

Assume the illustrative products table contains these rows:

name       price
Notebook       8
Mug            10
Pen set        15
Desk lamp      20
Backpack       22

With the BETWEEN query, the result is:

name       price
Mug            10
Pen set        15
Desk lamp      20

The equivalent version using two comparisons is:

SELECT name, price
FROM products
WHERE price >= 10 AND price <= 20
ORDER BY price;

It selects the same three rows: 10 meets the lower boundary, 20 meets the upper boundary, and 15 is between them. The row priced 8 fails the lower comparison, while 22 fails the upper comparison. SQLite defines BETWEEN as equivalent to those inclusive comparisons. [S1]

Follow-up question

Which form should I use, and how can I avoid accidentally excluding an endpoint?

Geez

Either form works for this inclusive range. BETWEEN 10 AND 20 is concise and makes the intended range easy to spot. The two-comparison form spells out each boundary, which can be useful if you are building a more involved condition or want to inspect the lower and upper checks separately.

To keep the endpoints included, use BETWEEN lower_value AND upper_value, or write both >= lower_value and <= upper_value. Do not replace either comparison with a strict operator: > 10 excludes a value equal to 10, and < 20 excludes a value equal to 20. Those observations follow directly from the comparison symbols; SQLite’s documented equivalence confirms the inclusive version. [S1]

For example, if your intended bounds are 10 and 20, check that the condition is price >= 10 AND price <= 20, not price > 10 AND price < 20.

Follow-up question

What is a simple way to check my own query against the intended range?

Geez

Run the query and compare its returned prices with the values you expect to fall within your chosen bounds. For the illustration, the expected prices are 10, 15, and 20; 8 and 22 should not appear. [S1]

If the result differs from your expectation, check that the query filters the intended column and uses the intended numeric bounds. This example demonstrates SQLite’s inclusive range behavior for the illustrated values; it does not establish anything about your particular table’s schema or data.

A practical checklist

  • Confirm the table name and numeric column you want to filter.

  • Write down the intended lower and upper bounds.

  • Use BETWEEN lower AND upper or both >= lower and <= upper for an inclusive range.

  • Check that rows equal to both endpoints are included and out-of-range rows are excluded.

  • Use sample values from your own table to verify the query returns the intended rows.

A few more useful answers

Does `BETWEEN` include both endpoints in SQLite?

Yes. SQLite documents x BETWEEN y AND z as equivalent to x >= y AND x <= z, so values equal to either endpoint are included. [S1]

Is `price BETWEEN 10 AND 20` different from `price >= 10 AND price <= 20`?

For this range condition, SQLite documents them as logically equivalent. Both select the example rows priced 10, 15, and 20. [S1]

Sources

References used for this guide.

  1. [S1] SQLite SQL Language Expressions — BETWEEN and comparison behavior
    sqlite.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work