Academic Block

SQL BETWEEN Get Certified

Learn how to use the SQL BETWEEN operator to filter values within a specified range using SQLite-compatible syntax.

What is the BETWEEN Operator?

The BETWEEN operator selects values within a range. The range is inclusive, so both the lower and the upper boundary values are part of the result.

BETWEEN works with numbers, text and dates, and it is normally used with the WHERE clause.

Basic BETWEEN Syntax

The syntax checks whether a value lies between a lower value and an upper value: column BETWEEN low AND high.

What this code does: Keeps the products priced from 500 to 1500. Mouse (300) is too cheap, and Monitor (1800) and Laptop (60000) are too expensive. The query returns 2 rows: Keyboard (800) and Webcam (1200).

WITH products(name, price) AS (
  VALUES
    ('Mouse', 300),
    ('Keyboard', 800),
    ('Webcam', 1200),
    ('Monitor', 1800),
    ('Laptop', 60000)
)
SELECT name, price
FROM products
WHERE price BETWEEN 500 AND 1500;

BETWEEN Includes Boundary Values

BETWEEN is inclusive: values equal to the lower and upper limits are part of the result.

What this code does: Returns the scores from 60 to 80. Neha scored exactly 60 and Priya exactly 80, and both are included because the boundaries count. The query returns 3 rows: Neha (60), Rahul (70) and Priya (80).

WITH scores(student, score) AS (
  VALUES
    ('Aman', 50),
    ('Neha', 60),
    ('Rahul', 70),
    ('Priya', 80),
    ('Karan', 90)
)
SELECT student, score
FROM scores
WHERE score BETWEEN 60 AND 80;

BETWEEN with Numbers

Filtering numeric values such as prices, salaries, scores or quantities is the most common use of BETWEEN.

What this code does: Returns the employees earning from 55000 to 75000. Aarav (45000) and Vikram (90000) fall outside the range. The query returns 3 rows: Meera (55000), Rohan (65000) and Anika (75000).

WITH employees(name, salary) AS (
  VALUES
    ('Aarav', 45000),
    ('Meera', 55000),
    ('Rohan', 65000),
    ('Anika', 75000),
    ('Vikram', 90000)
)
SELECT name, salary
FROM employees
WHERE salary BETWEEN 55000 AND 75000;

BETWEEN with Prices

Use BETWEEN to find products whose price falls within a range.

What this code does: Keeps the products priced from 500 to 2000 and sorts them from the lowest price to the highest. The USB Cable (250) and Speaker (2500) are left out. The query returns 3 rows: Mouse (600), Keyboard (1200) and Headphones (1800).

WITH products(name, price) AS (
  VALUES
    ('USB Cable', 250),
    ('Mouse', 600),
    ('Keyboard', 1200),
    ('Headphones', 1800),
    ('Speaker', 2500)
)
SELECT name, price
FROM products
WHERE price BETWEEN 500 AND 2000
ORDER BY price;

NOT BETWEEN

NOT BETWEEN returns the values that fall outside the range.

What this code does: Returns the products priced below 800 or above 1800. Because the range is inclusive, prices of exactly 800 and 1800 are excluded from this result. The query returns 2 rows: Mouse (300) and Speaker (3000).

WITH products(name, price) AS (
  VALUES
    ('Mouse', 300),
    ('Keyboard', 800),
    ('Webcam', 1200),
    ('Monitor', 1800),
    ('Speaker', 3000)
)
SELECT name, price
FROM products
WHERE price NOT BETWEEN 800 AND 1800;

BETWEEN with AND

Combine BETWEEN with other conditions using AND. The AND that belongs to BETWEEN is part of its syntax, and a second AND adds another condition.

What this code does: A row must earn from 60000 to 80000 and work in IT or Finance. Anika is in IT but earns 82000, and Vikram earns 60000 but works in HR. The query returns 2 rows: Aarav (IT, 65000) and Meera (Finance, 72000).

WITH employees(name, department, salary) AS (
  VALUES
    ('Aarav', 'IT', 65000),
    ('Meera', 'Finance', 72000),
    ('Rohan', 'Sales', 55000),
    ('Anika', 'IT', 82000),
    ('Vikram', 'HR', 60000)
)
SELECT name, department, salary
FROM employees
WHERE salary BETWEEN 60000 AND 80000
AND department IN ('IT', 'Finance');

BETWEEN with OR

Several BETWEEN conditions can be combined with OR to match more than one range.

What this code does: Returns the products in the 500 to 2000 range or in the 20000 to 30000 range. Mouse (300) and Laptop (65000) match neither. The query returns 3 rows: Keyboard (900), Monitor (1800) and Tablet (25000).

WITH products(name, price) AS (
  VALUES
    ('Mouse', 300),
    ('Keyboard', 900),
    ('Monitor', 1800),
    ('Laptop', 65000),
    ('Tablet', 25000)
)
SELECT name, price
FROM products
WHERE price BETWEEN 500 AND 2000
   OR price BETWEEN 20000 AND 30000;

BETWEEN with Dates

SQLite commonly stores dates as text in YYYY-MM-DD format. Dates in this ISO format sort correctly, so they work with BETWEEN.

What this code does: Returns the events from 15 to 25 August 2026, sorted by date. The events on exactly 15 and 25 August are included, and those on the 10th and 30th are not. The query returns 3 rows: Seminar (2026-08-15), Conference (2026-08-20) and Meeting (2026-08-25).

WITH events(event_name, event_date) AS (
  VALUES
    ('Workshop', '2026-08-10'),
    ('Seminar', '2026-08-15'),
    ('Conference', '2026-08-20'),
    ('Meeting', '2026-08-25'),
    ('Webinar', '2026-08-30')
)
SELECT event_name, event_date
FROM events
WHERE event_date BETWEEN '2026-08-15' AND '2026-08-25'
ORDER BY event_date;

BETWEEN with Timestamps (A Common Trap)

If a date column also stores a time, such as 2026-08-25 10:30, the value is longer than '2026-08-25' and sorts after it.

What this code does: You might expect all events from 24 and 25 August. But “2026-08-25 10:30” sorts after the upper limit “2026-08-25”, so the two events on the 25th are missed. Only the 24th is returned. The query returns 1 row: Workshop (2026-08-24 09:00).

WITH events(event_name, event_date) AS (
  VALUES
    ('Workshop', '2026-08-24 09:00'),
    ('Seminar', '2026-08-25 10:30'),
    ('Meeting', '2026-08-25 16:00'),
    ('Webinar', '2026-08-26 11:00')
)
SELECT event_name, event_date
FROM events
WHERE event_date BETWEEN '2026-08-24' AND '2026-08-25';

Fixing the Timestamp Problem

For timestamps, use >= for the start and < the day after the end.

What this code does: Reads as “from the start of 24 August up to, but not including, 26 August”. This catches every event on the 24th and 25th, whatever the time of day. The query returns 3 rows: Workshop (2026-08-24 09:00), Seminar (2026-08-25 10:30) and Meeting (2026-08-25 16:00).

WITH events(event_name, event_date) AS (
  VALUES
    ('Workshop', '2026-08-24 09:00'),
    ('Seminar', '2026-08-25 10:30'),
    ('Meeting', '2026-08-25 16:00'),
    ('Webinar', '2026-08-26 11:00')
)
SELECT event_name, event_date
FROM events
WHERE event_date >= '2026-08-24'
  AND event_date <  '2026-08-26';

BETWEEN with Text Values

BETWEEN can compare text according to the database’s sorting rules, which for plain text is alphabetical order.

What this code does: Returns the names that sort from “Bhavna” to “Meera” alphabetically, including both ends. Aarav sorts before Bhavna and Rohan sorts after Meera. The query returns 4 rows: Bhavna, Chetan, Deepak and Meera.

WITH names(name) AS (
  VALUES
    ('Aarav'),
    ('Bhavna'),
    ('Chetan'),
    ('Deepak'),
    ('Meera'),
    ('Rohan')
)
SELECT name
FROM names
WHERE name BETWEEN 'Bhavna' AND 'Meera'
ORDER BY name;

BETWEEN with ORDER BY

Use ORDER BY to sort the rows that pass a BETWEEN condition.

What this code does: Keeps the scores from 70 to 90 and sorts them from the highest to the lowest. Neha (91) and Rahul (65) are outside the range. The query returns 3 rows: Priya (84), Karan (78) and Aman (72).

WITH students(name, score) AS (
  VALUES
    ('Aman', 72),
    ('Neha', 91),
    ('Rahul', 65),
    ('Priya', 84),
    ('Karan', 78)
)
SELECT name, score
FROM students
WHERE score BETWEEN 70 AND 90
ORDER BY score DESC;

BETWEEN with LIMIT

In SQLite, BETWEEN can be combined with ORDER BY and LIMIT to return only a selected number of matching rows.

What this code does: All five products are within 500 to 1600, so they all match. They are sorted from the highest price to the lowest and LIMIT 3 keeps the first three. The query returns 3 rows: Speaker (1600), Headphones (1400) and Webcam (1000).

WITH products(name, price) AS (
  VALUES
    ('Mouse', 500),
    ('Keyboard', 800),
    ('Webcam', 1000),
    ('Headphones', 1400),
    ('Speaker', 1600)
)
SELECT name, price
FROM products
WHERE price BETWEEN 500 AND 1600
ORDER BY price DESC
LIMIT 3;

BETWEEN with Calculations

The expression being tested can contain a calculation. Here the total value is calculated first, and then the range is checked.

What this code does: Multiplies price by quantity to get each row’s total, and keeps the totals from 1000 to 2000. Mouse totals 1000, Monitor totals 3000 and Webcam totals 2100. The query returns 1 row: Mouse (500, 2, 1000).

WITH products(name, price, quantity) AS (
  VALUES
    ('Mouse', 500, 2),
    ('Keyboard', 800, 1),
    ('Monitor', 1500, 2),
    ('Webcam', 700, 3)
)
SELECT name, price, quantity,
       price * quantity AS total
FROM products
WHERE price * quantity BETWEEN 1000 AND 2000;

BETWEEN vs Comparison Operators

BETWEEN is a shorter way to write “greater than or equal to the lower value AND less than or equal to the upper value”. This is the long form.

What this code does: Uses two comparisons joined by AND to keep the scores from 60 to 80. The query returns 2 rows: Neha (65) and Rahul (75).

WITH scores(student, score) AS (
  VALUES
    ('Aman', 55),
    ('Neha', 65),
    ('Rahul', 75),
    ('Priya', 85)
)
SELECT student, score
FROM scores
WHERE score >= 60
  AND score <= 80;

The Same Condition Using BETWEEN

Here is the same condition written with BETWEEN. It is shorter and the column name is written only once.

What this code does: Gives exactly the same result as the two-comparison version above. The query returns 2 rows: Neha (65) and Rahul (75).

WITH scores(student, score) AS (
  VALUES
    ('Aman', 55),
    ('Neha', 65),
    ('Rahul', 75),
    ('Priya', 85)
)
SELECT student, score
FROM scores
WHERE score BETWEEN 60 AND 80;

BETWEEN with Multiple Columns

Use a separate BETWEEN condition for each column you want to filter.

What this code does: A row must be aged 25 to 35 and earn 50000 to 75000. Anika is 40, which is outside the age range, so she is skipped. The query returns 4 rows: Aarav (25, 50000), Meera (30, 65000), Rohan (35, 72000) and Vikram (28, 60000).

WITH employees(name, age, salary) AS (
  VALUES
    ('Aarav', 25, 50000),
    ('Meera', 30, 65000),
    ('Rohan', 35, 72000),
    ('Anika', 40, 85000),
    ('Vikram', 28, 60000)
)
SELECT name, age, salary
FROM employees
WHERE age BETWEEN 25 AND 35
  AND salary BETWEEN 50000 AND 75000;

The Order of the Boundaries Matters

The lower value must come first. BETWEEN 1500 AND 500 is read as “at least 1500 and at most 500”, which no value can satisfy.

What this code does: The boundaries are reversed, so the condition can never be true and the query returns no rows, even though three products cost between 500 and 1500. Swap the two numbers to fix it. The query returns no rows.

WITH products(name, price) AS (
  VALUES
    ('Mouse', 600),
    ('Keyboard', 1200),
    ('Webcam', 1400),
    ('Monitor', 1800)
)
SELECT name, price
FROM products
WHERE price BETWEEN 1500 AND 500;

Common BETWEEN Uses

Use
Example
Purpose
Numbers
price BETWEEN 500 AND 1500
Find values within a numeric range
Dates
date BETWEEN '2026-01-01' AND '2026-12-31'
Find dates within a period
Text
name BETWEEN 'A' AND 'M'
Filter according to text ordering
NOT BETWEEN
score NOT BETWEEN 40 AND 60
Find values outside a range

Advantages of BETWEEN

  • Makes range-based filtering easy to read.
  • Includes both the lower and upper boundary values.
  • Works with numbers and ISO-formatted date strings in SQLite.
  • Can be combined with AND and OR conditions.
  • Can be used with calculations and expressions.

Best Practices

  • Remember that BETWEEN includes both boundary values.
  • Always write the lower value first and the higher value second.
  • Use NOT BETWEEN when you need values outside a range.
  • Use ISO YYYY-MM-DD date strings, and use >= with < when the column also stores a time.
  • Use parentheses when combining several range conditions with AND and OR.
  • Use IS NULL separately to handle NULL values, because BETWEEN never matches NULL.

The BETWEEN operator filters values within an inclusive range. It is especially useful for prices, scores, dates, salaries and quantities. Click Test My Code on any example above to run it live, or Code Explanation to walk through the query block by block.

Ctrl + Enter to run code in the editor Esc to close the editor

🧪 Test Your SQL Code

Edit the SQL code on the left and click “Run Code” to see the result on the right.

📝 SQL Code
👁️ Preview (query result)

Click “Run Code” to see the result here.