SQLite Tutorial

  • Home
  • Views
  • Indexes
  • Triggers
  • Functions
    • Aggregate Functions
    • Date Functions
    • String Functions
    • Window Functions
  • Interfaces
    • SQLite Java
    • SQLite Node.js
    • SQLite PHP
    • SQLite Python
  • 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:

1
2
3
4
5
6
SELECT
    column_list
FROM
    table
WHERE
    search_condition;

You add a WHERE clause to the SELECT statement to filter data returned by the query. The WHERE clause is also known as a set of conditions or a predicate list.

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 the 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:

1
left_expression COMPARISON_OPERATOR right_expression

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

1
2
3
4
5
6
7
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, to query all tracks in the album id 1, you use the equality operator as the following statement:

1
2
3
4
5
6
7
8
9
SELECT
   name,
   milliseconds,
   bytes,
   albumid
FROM
   tracks
WHERE
   albumid = 1;

Try It

SQLite WHERE clause

SQLite compares the value stored in the AlbumId column with a literal value 1 to test if the values are equal. 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 conversion, but in general, you should avoid doing this.

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

1
2
3
4
5
6
7
8
9
10
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:

1
2
3
4
5
6
7
8
9
10
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 1 or 2, you use the IN operator as the following statement:

1
2
3
4
5
6
7
8
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 Tutorial: SQLite Limit
Next Tutorial: 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 Constraint
  • 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 Concat
  • SQLite INSTEAD OF Triggers
  • SQLite Join
  • SQLite IS NULL
  • SQLite Rename Column
  • SQLite DROP VIEW
  • SQLite Window Frame
  • SQLite CUME_DIST

Site Links

  • Home
  • About
  • Contact
  • Resources
  • Privacy Policy

Copyright © 2020 SQLite Tutorial. All rights Reserved.

⤒