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 Where

SQLite Where

Summary: in this tutorial, you will learn how to use SQLite WHERE clause to specify the search condition for rows returned by the query.

Introduction to SQLite WHERE clause

The WHERE clause is an optional clause of the SELECT statement. It appears after the FROM clause as the following statement:

SELECT column_list FROM table WHERE search_condition;

In this example, you add a WHERE clause to the SELECT statement to filter rows returned by the query. When evaluating a SELECT statement with a WHERE clause, SQLite uses the following steps:

  1. First, check the table in the FROM clause.
  2. Second, evaluate the conditions in the WHERE clause to get the rows that met these conditions.
  3. Third, make the final result set based on the rows in the previous step with columns in the SELECT clause.

The search condition in the WHERE has the following form:

left_expression COMPARISON_OPERATOR right_expression

For example, you can form a search condition as follows:

WHERE column_1 = 100; WHERE column_2 IN (1,2,3); WHERE column_3 LIKE 'An%'; WHERE column_4 BETWEEN 10 AND 20;

Besides the SELECT statement, you can use the WHERE clause in the UPDATE and DELETE statements.

SQLite comparison operators

A comparison operator tests if two expressions are the same. The following table illustrates the comparison operators that you can use to construct expressions:

OperatorMeaning
=Equal to
<> or !=Not equal to
<Less than
>Greater than
<=Less than or equal to
>=Greater than or equal to

SQLite logical operators

Logical operators allow you to test the truth of some expressions. A logical operator returns 1, 0, or a NULL value.

Notice that SQLite does not provide Boolean data type therefore 1 means TRUE, and 0 means FALSE.

The following table illustrates the SQLite logical operators:

OperatorMeaning
ALLreturns 1 if all expressions are 1.
ANDreturns 1 if both expressions are 1, and 0 if one of the expressions is 0.
ANYreturns 1 if any one of a set of comparisons is 1.
BETWEENreturns 1 if a value is within a range.
EXISTSreturns 1 if a subquery contains any rows.
INreturns 1 if a value is in a list of values.
LIKEreturns 1 if a value matches a pattern
NOTreverses the value of other operators such as NOT EXISTS, NOT IN, NOT BETWEEN, etc.
ORreturns true if either expression is 1

SQLite WHERE clause examples

We will use the tracks table in the sample database to illustrate how to use the WHERE clause.

The equality operator (=) is the most commonly used operator. For example, the following query uses the WHERE clause the equality operator to find all the tracks in the album id 1:

SELECT name, milliseconds, bytes, albumid FROM tracks WHERE albumid = 1;

Try It

SQLite WHERE clause

SQLite compares the values stored in the AlbumId column with a literal value 1 to test if they are equal. Only the rows that satisfy the condition are returned.

When you compare two values, you must ensure that they are the same data type. You should compare numbers with numbers, string with strings, etc.

In case you compare values in different data types e.g., a string with a number, SQLite has to perform implicit data type conversions, but in general, you should avoid doing this.

You use the logical operator to combine expressions. For example, to get tracks of the album 1 that have the length greater than 200,000 milliseconds, you use the following statement:

SELECT name, milliseconds, bytes, albumid FROM tracks WHERE albumid = 1 AND milliseconds > 250000;

Try It

SQLite WHERE clause comparison operator

The statement used two expressions albumid = 1 and milliseconds > 250000. It uses the AND logical operator to combine these expressions.

SQLite WHERE multiple conditions


SQLite WHERE clause with LIKE operator example

Sometimes, you may not remember exactly the data that you want to search. In this case, you perform an inexact search using the LIKE operator.

For example, to find which tracks composed by Smith, you use the LIKE operator as follows:

SELECT name, albumid, composer FROM tracks WHERE composer LIKE '%Smith%' ORDER BY albumid;

Try It

SQLite WHERE with LIKE operator

You get tracks composed by R.A. Smith-Diesel, Adrian Smith, etc.

SQLite WHERE clause with the IN operator example

The IN operator allows you to check whether a value is in a list of a comma-separated list of values. For example, to find tracks that have media type id is 2 or 3, you use the IN operator as shown in the following statement:

SELECT name, albumid, mediatypeid FROM tracks WHERE mediatypeid IN (2, 3);

Try It

SQLite WHERE with IN operator

In this tutorial, you have learned how to use the SQLite WHERE clause to filter rows in the final result set using comparison and logical operators.

  • Was this tutorial helpful ?
  • YesNo
Previous SQLite Select Distinct
Next SQLite Limit

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.