Sqlite - Filter

You can filter your output so you can return only a few records that meet certain criteria.

Introduction

You already know the basic syntax of the select statement and how to retrieve columns from one table in your database, but very often you don’t need all records from a table.

The SQL where clause allows you to filter your results.

This clause introduces certain conditions, like:

  • quantity < 100
  • price between 100 and 500
  • customer_name = 'John Smith'

Sample Database

The Sakila sample database is a fictitious database designed to represent a DVD rental store. The tables of the database include film, film_category, actor, customer, rental, payment and inventory among others.

Download sakila database file from this link: sakila.db

where

The select statement returns all rows from one or more columns in a table.

To retrieve rows that satisfy a specified condition, you use a where clause.

select <columns>
from <table>
where <condition>
order by <column>;

In this syntax,

  • You place the where clause right after the from clause of the select statement.
  • The where clause uses the condition to filter the rows returned from the select clause.
  • The condition is a boolean expression that evaluates to true, false, or unknown.

The query returns only rows that satisfy the condition in the where clause.

In other words, the query will include only rows that cause the condition to evaluate to true in the result set.

SQLite evaluates the where clause after the from clause, but before the select and order by clause:

from

where

select

order by

If you use column aliases in the select clause, you cannot use them in the where clause.

Besides the select statement, you can use the where clause in the update and delete statement to specify rows to update and delete.

To form the condition in the where clause, you use comparison and logical operators:

OperatorDescription
=Equal
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal
<> or !=Not equal
andLogical operator AND
orLogical operator OR
inReturn true if a value matches any in a list
betweenReturn true if a value is between a range
likeReturn true if a value matches a pattern
is nullReturn true if a value is NULL
notNegate the result of other operators

Tasks

Let’s practice with some examples of using the where clause.

We will use the customer table from the sample database for demonstration.

equal (=) operator

Find customers whose first name is Jamie:

first_namelast_name
JAMIERICE
JAMIEWAUGH
and operator

Find customers whose first name and last names are “JAMIE” and “RICE”:

first_namelast_name
JAMIERICE
or operator

Find the customers whose last name is Rodriguez or first name is Adam:

first_namelast_name
LAURARODRIGUEZ
ADAMGOOCH
in operator

If you want to find a value in a list of values, you can use the in operator.

Find the customers with first names in the list ANN, ANNE, and ANNIE:

first_namelast_name
ANNEVANS
ANNEPOWELL
ANNIERUSSELL
like operator

To find a string that matches a specified pattern, you use the like operator.

Find customers whose first names start with the word ANN:

first_namelast_name
ANNAHILL
ANNEVANS
ANNEPOWELL
ANNIERUSSELL
ANNETTEOLSON

The % is called a wildcard that matches any string.

The 'ANN%' pattern matches any strings that start with 'ANN'.

between operator

Find customers whose first names start with the letter A and contains 3 to 5 characters by using the between operator.

The between operator returns true if a value is in a range of values.

namecount
AMY3
ANN3
ANA3
ANNA4
not equal operator (!=)

Finds customers whose first names start with Bra and last names are not Motley

first_namelast_name
BRANDYGRAVES
BRANDONHUEY
BRADMCCURDY

Note that you can use the != operator and <> operator interchangeably because they are equivalent.

and

A boolean value can have one of three values: true, false, and null.

SQLite uses true and 1 to represent true false and 0 to represent false.

A boolean expression is an expression that evaluates to a boolean value.

For example, the expression 1 = 1 is a boolean expression that evaluates to true:

select 1 = 1 as result
result
1

The 1 in the output indicates the value of true.

The and operator is a logical operator that combines two boolean expressions.

The and operator:

  • Returns true only if both expressions are true.
  • It returns false if one of the expressions is false.
  • Otherwise, it returns null.

The following table shows the results of the and operator when combining true, false, and null.

Note that the order of the expressions doesn’t matter, for example, both true and null and null and true will evaluate to null.

e1e2e1 and e2
TrueTrueTrue
TrueFalseFalse
TrueNullNull
FalseFalseFalse
FalseNullFalse
NullNullNull
Truth table for the `and` operator

Ensure the and truth table follows three-valued logic.

For example, check that true and null evaluates to null.

In practice, you often use the and operator in a where clause to ensure that all specified expressions must be true for a row to be included in the result set.

Tasks

We’ll use the film table from the sample database.

Find all films that are not rated PG

Find all films that are not rated PG:

Task

Find the films that have a length greater than 180 and a rental rate less than 1

or

The or operator:

  • Returns true only if any of the expressions is true.
  • It returns false if both expressions are false.
  • Otherwise, it returns null.

The following table shows the results of the or operator when combining true, false, and null.

Note that the order of the expressions doesn’t matter, for example both false or null and null or false will evaluate to null.

e1e2e1 or e2
TrueTrueTrue
TrueFalseTrue
TrueNullTrue
FalseFalseFalse
FalseNullNull
NullNullNull

In practice, you usually use the or operator in a where clause to ensure that either of the specified expressions must be true for a row to be included in the result set.

Task

Find the films that have a rental rate is 0.99 or 2.99

in

The in operator returns true if the value is equal to any value in the list such as value1 and value2.

The list of values can be a list of literal values, including numbers and strings.

Functionally, the in operator is equivalent to combining multiple boolean expressions with the or operators:

value = value1 or value = value2 or ...

Tasks

Task

Retrieve information about the film with id 1, 2, and 3:

Task

Find the actors on the actor table who have the last name in the list ‘Allen’, ‘Chase’, and ‘Davis’:

Task

Find payments on the payment table whose payment dates are in a list of dates: 2005-02-15 and 2005-02-16

Task

To negate the in operator, you use the not in operator.

Retrieve films whose id is not 1, 2, or 3:

between

The between operator allows you to check if a value falls within a range of values.

The basic syntax of the between operator is as follows:

value between low and high;

If the value is greater than or equal to the low value and less than or equal to the high value, the between operator returns true; otherwise, it returns false.

You can rewrite the between operator by using the greater than or equal (>=) and less than or equal to ( <= ) operators and the logical and operator:

value >= low and value <= high

If you want to check if a value is outside a specific range, you can use the not between operator as follows:

value not between low and high

The following expression is equivalent to the expression that uses the not between operators:

value < low or value > high

Tasks

Task

Find payments with payment_id is between 14503 and 14505:

Task

Find payments with payment_id is not between 14503 and 14505:

Task

If you want to check a value against a date range, you use the literal date in ISO 8601 format, which is YYYY-MM-DD.

The following example uses the between operator to find payments whose payment dates are between 2007-02-15 and 2007-02-20 and amount more than 10:

like

You can use the like operator to match the first names of customers with a string using the following query:

select first_name, last_name
from customer
where first_name like 'Jen%'

The expression consists of the first_name, the like operator and a literal string that contains a percent sign (%). The string 'Jen%' is called a pattern.

The query returns rows whose values in the first_name column begin with Jen and are followed by any sequence of characters. This technique is called pattern matching.

You construct a pattern by combining literal values with wildcard characters and using the like or not like operator to find the matches.

SQLite offers two wildcards:

  • Percent sign (%) matches any sequence of zero or more characters.
  • Underscore sign (_) matches any single character.

The like operator returns true if the value matches the pattern.

To negate the like operator, you use the not operator.

If the pattern does not contain any wildcard character, the like operator behaves like the equal (=) operator.

Tasks

Task

Find customers whose first names contain the string er:

Task

Find customers whose first names starts with some letter followed by er:

Task

Find customers whose first names do not begin with Jen:

`is null

To check if a value is null or not, you cannot use the equal to (=) or not equal to (<>) operators.

Instead, you use the is null operator.

The is null operator returns true if the value is null or false otherwise.

The is not null operator returns true if the value is not null or false otherwise.

SQLite offers some useful functions to handle null effectively such as nullif and coalesce.

Task: Sales People

We’ll use the following table that includes information on salespeople:

CREATE TABLE employees (
id INTEGER UNIQUE NOT NULL,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
salary INTEGER NOT NULL,
commission_rate REAL NOT NULL,
commission REAL,
branch_id INTEGER NOT NULL
);
INSERT INTO employees VALUES
(11, 'Katarina', 'Rostova', 45000, 0.15, 47345.60, 1),
(12, 'Alina', 'Park', 43000, 0.15, 45678.90, 2),
(13, 'Meera', 'Malik', 50000, 0.15, 39045.63, 2),
(17, 'Samar', 'Navabi', 52000, 0.14, 23023.45, 2),
(18, 'Donald', 'Ressler', 40000, 0.14, 41345.75, 2),
(20, 'Elisabeth', 'Keen', 59000, 0.14, 45350.00, 2),
(21, 'Tom', 'Keen', 41000, 0.12, 41560.75, 1),
(22, 'Dembe', 'Zuma', 40000, 0.12, 31540.70, 5),
(23, 'Aram', 'Mojtabai', 50000, 0.12, 29050.65, 2),
(30, 'Kate', 'Kaplan', 54000, 0.10, 25760.45, 5),
(32, 'Marvin', 'Gerard', 55000, 0.10, 22500.00, 5),
(34, 'Raymond', 'Reddington', 60000, 0.10, 17570.80, 5),
(35, 'Harold', 'Cooper', 57000, 0.10, 15450.50, 2),
(37, 'Ian', 'Garvey', 43000, 0.08, NULL, 1),
(38, 'Ivan', 'Stepanov', 41000, 0.08, NULL, 1)
Task

Get the records of all employees whose annual salary is equal or greater than 50000, and order by salary:

Task

List all employees who, thanks to their long experience with the company, have a commission rate above 0.12:

Task

List the employees whose commission earnings were greater than their annual salary:

Task

List all employees whose commission rate is between 0.12 and 0.14:

Task

Retrieve information on all employees whose last name (when sorted alphabetically) is before ‘Keen’:

Task

List all employees whose last name starts with K:

Task

Retrieve all records that have NULL in the commision column:

Task

Retrieve the records of ‘Kaplan’, ‘Gerard’ and ‘Zuma’:

Task

List all employees that work in branch 5 and have salaries equal to or greater than 5000:

Task

Get information on all employees except the ones who work in branch 2:

Task

Retrieve all records where their commission earnings were higher than their salary, and they are not working in branch #2

Task: Population

Download population.csv

Task

Retrieve countries of Asia that have a population greater than 100 million:

Task

Retrieve countries of Europe that have a population between 10 million and 15 million:

Task

Pending