SQLite Tutorial

  • Home
  • Start Here
  • Views
  • Indexes
  • Triggers
  • Functions
    • Aggregate Functions
    • Date Functions
    • String Functions
    • Window Functions
  • API
    • SQLite Python
    • SQLite Node.js
    • SQLite Java
    • SQLite PHP
  • Try It
Home / SQLite Tutorial / SQLite BETWEEN

SQLite BETWEEN

Summary: in this tutorial, you will learn how to use the SQLite BETWEEN operator to test whether a value is in a range of values.

Introduction to SQLite BETWEEN Operator

The BETWEEN operator is a logical operator that tests whether a value is in range of values. If the value is in the specified range, the BETWEEN operator returns true. The BETWEEN operator can be used in the WHERE clause of the SELECT, DELETE, UPDATE, and REPLACE statements.

The following illustrates the syntax of the SQLite BETWEEN operator:

test_expression BETWEEN low_expression AND high_expression

In this syntax:

  • test_expression is an expression to test for in the range defined by low_expression and high_expression.
  • low_expression and high_expression is any valid expression that specify the low and high values of the range. The low_expression should be less than or equal to high_expression, or the BETWEEN is always returns false.
  • The AND keyword is a placeholder which indicates the test_expression should be within the range specified by low_expression and high_expression.

Note that the BETWEEN operator is inclusive. It returns true when the test_expression is less than or equal to high_expression and greater than or equal to the value of low_expression:

test_expression >= low_expression AND test_expression <= high_expression

To specify an exclusive range, you use the greater than (>) and less than operators (<).

Note that if any input to the BETWEEN operator is NULL, the result is NULL, or unknown to be precise.

To negate the result of the BETWEEN operator, you use the NOT BETWEEN operator as follows:

test_expression NOT BETWEEN low_expression AND high_expression

The NOT BETWEEN returns true if the value of test_expression is less than the value of low_expression or greater than the value of high_expression:

test_expression < low_expression OR test_expression > high_expression

SQLite BETWEEN operator examples

We will use the invoices table from the sample database for the demonstration:

SQLite BETWEEN numeric values example

The following statement finds invoices whose total is between 14.96 and 18.86:

SELECT InvoiceId, BillingAddress, Total FROM invoices WHERE Total BETWEEN 14.91 and 18.86 ORDER BY Total;

Here is the output:

SQLite BETWEEN Numbers example

As you can see, the invoices whose total is 14.91 or 18.86 are included in the result set.

SQLite NOT BETWEEN numeric values example

To find the invoices whose total are not between 1 and 20, you use the NOT BETWEEN operator as shown in the following query:

SELECT InvoiceId, BillingAddress, Total FROM invoices WHERE Total NOT BETWEEN 1 and 20 ORDER BY Total;

The following picture shows the output:

SQLite NOT BETWEEN Numbers example

As clearly shown in the output, the result includes the invoices whose total is less than 1 and greater than 20.

SQLite BETWEEN dates example

The following example finds invoices whose invoice dates are from January 1 2010 and January 31 2010:

SELECT InvoiceId, BillingAddress, InvoiceDate, Total FROM invoices WHERE InvoiceDate BETWEEN '2010-01-01' AND '2010-01-31' ORDER BY InvoiceDate;

Here is the output:

SQLite BETWEEN Dates example

SQLite NOT BETWEEN dates example

The following statement finds invoices whose dates are not between January 03, 2009, and December 01, 2013:

SELECT InvoiceId, BillingAddress, date(InvoiceDate) InvoiceDate, Total FROM invoices WHERE InvoiceDate NOT BETWEEN '2009-01-03' AND '2013-12-01' ORDER BY InvoiceDate;

The output is as follows:

SQLite NOT BETWEEN Dates example

In this tutorial, you have learned how to use the SQLite BETWEEN operator to test whether a value is in a range of values

  • Was this tutorial helpful ?
  • YesNo
Previous SQLite Limit
Next SQLite IN

Getting Started

  • What Is SQLite
  • Download & Install SQLite
  • SQLite Sample Database
  • SQLite Commands

SQLite Tutorial

  • SQLite Select
  • SQLite Order By
  • SQLite Select Distinct
  • SQLite Where
  • SQLite Limit
  • SQLite BETWEEN
  • SQLite IN
  • SQLite Like
  • SQLite IS NULL
  • SQLite GLOB
  • SQLite Join
  • SQLite Inner Join
  • SQLite Left Join
  • SQLite Cross Join
  • SQLite Self-Join
  • SQLite Full Outer Join
  • SQLite Group By
  • SQLite Having
  • SQLite Union
  • SQLite Except
  • SQLite Intersect
  • SQLite Subquery
  • SQLite EXISTS
  • SQLite Case
  • SQLite Insert
  • SQLite Update
  • SQLite Delete
  • SQLite Replace
  • SQLite Transaction

SQLite Data Definition

  • SQLite Data Types
  • SQLite Date & Time
  • SQLite Create Table
  • SQLite Primary Key
  • SQLite Foreign Key
  • SQLite NOT NULL Constraint
  • SQLite UNIQUE Constraint
  • SQLite CHECK constraints
  • SQLite AUTOINCREMENT
  • SQLite Alter Table
  • SQLite Rename Column
  • SQLite Drop Table
  • SQLite Create View
  • SQLite Drop View
  • SQLite Index
  • SQLite Expression-based Index
  • SQLite Trigger
  • SQLite VACUUM
  • SQLite Transaction
  • SQLite Full-text Search

SQLite Tools

  • SQLite Commands
  • SQLite Show Tables
  • SQLite Describe Table
  • SQLite Dump
  • SQLite Import CSV
  • SQLite Export CSV

SQLite Functions

  • SQLite AVG
  • SQLite COUNT
  • SQLite MAX
  • SQLite MIN
  • SQLite SUM

SQLite Interfaces

  • SQLite PHP
  • SQLite Node.js
  • SQLite Java
  • SQLite Python

About SQLite Tutorial

SQLite Tutorial website helps you master SQLite quickly and easily. It explains the complex concepts in simple and easy-to-understand ways so that you can both understand SQLite fast and know how to apply it in your software development work more effectively.

Looking for a tutorial…

If you did not find the tutorial that you are looking for, you can use the following search box. In case the tutorial is not available, you can request for it using the request for a SQLite tutorial form.

Recent Tutorials

  • SQLite IIF
  • SQLite Generated Columns
  • SQLite Getting Started
  • SQLite Programming Interfaces
  • SQLite Concat
  • SQLite INSTEAD OF Triggers
  • SQLite Join
  • SQLite IS NULL

Site Links

  • Home
  • About
  • Contact
  • Resources
  • Privacy Policy

Copyright © 2021 SQLite Tutorial. All Rights Reserved.