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 < 100price between 100 and 500customer_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
whereclause right after thefromclause of theselectstatement. - The
whereclause uses the condition to filter the rows returned from theselectclause. - 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:
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:
| Operator | Description |
|---|---|
= | Equal |
> | Greater than |
< | Less than |
>= | Greater than or equal |
<= | Less than or equal |
<> or != | Not equal |
and | Logical operator AND |
or | Logical operator OR |
in | Return true if a value matches any in a list |
between | Return true if a value is between a range |
like | Return true if a value matches a pattern |
is null | Return true if a value is NULL |
not | Negate 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.
Find customers whose first name is Jamie:
Show solution
select first_name, last_name
from customer
where first_name = 'JAMIE'| first_name | last_name |
|---|---|
| JAMIE | RICE |
| JAMIE | WAUGH |
Find customers whose first name and last names are “JAMIE” and “RICE”:
Show solution
select first_name, last_name
from customer
where first_name = 'JAMIE' and last_name = 'RICE'| first_name | last_name |
|---|---|
| JAMIE | RICE |
Estàs llegint una vista prèvia.
Inicia sessió per llegir l'article complet. Qualsevol compte obre 4 articles gratuïts al mes; l'alumnat i el professorat llegeixen les pàgines del seu curs sense límit.
Inicia sessió