Use Sophia to knock out your gen-ed requirements quickly and affordably. Learn more
×

WHERE to Filter Data

Author: Sophia

what's covered
This tutorial explores using the WHERE clause within a SELECT statement to filter data in the result set in three parts:
  1. Getting Started
  2. Filtering Strings
  3. Comparison Operators

1. Getting Started

The WHERE clause is used to filter records in a SELECT statement. The WHERE clause is optional, and adds conditional restrictions to the SELECT statement that will help limit the result set. It only displays the records that fit the condition listed in the WHERE clause. By using the WHERE clause, you can easily answer questions like:

  • Which invoices have a total greater than 14?
  • Which customers live in Canada?
  • Which employees report to the General Manager?
For example, if we wanted to find the customer information of the customer_id that was equal to 5, we would run it as:


SELECT * 
FROM customer
WHERE customer_id = 5;

table

Notice that in the WHERE clause, we define the column (customer_id), the comparison operator (=), and the value that we wanted to compare it to (5).

If there are no rows that match the criteria in the WHERE clause, you should see a message similar to the following:


SELECT * 
FROM customer
WHERE customer_id = 1000;

results


2. Filtering Strings

Note that SQL requires single quotes around text values. Numeric values should not be enclosed in quotes. Here is an example of what would happen if we forgot to include quotes around the text value 'Helena':


SELECT * 
FROM customer
WHERE first_name = Helena;

We would get an error message:

error

This is because the database thinks the text value is a column. This could also present a problem if the text value is also an actual column. You would not get an error message; however, the results would not be what wanted either.

To properly use the WHERE clause, you would use the single quotes around the text values:


SELECT * 
FROM customer
WHERE first_name = 'Helena';

table


3. Comparison Operators

We looked at the = operator above, but there are many other operators that can be used in the WHERE clause. Other comparison operators include:

= means equal to
< means less than
<= means less than or equal to
> means greater than
>= means greater than or equal to
<> means not equal to

Let us find the invoices that have a total greater than 14.


SELECT * 
FROM invoice
WHERE total > 14;

table

The result set includes 12 rows. If we change the WHERE clause to >= 14, and include all invoices with the value of 14, the result set goes from 12 rows to 61 rows returned.


SELECT * 
FROM invoice
WHERE total >= 14;

table

When it comes to integer values being compared, there would be no difference between using these two statements:


SELECT * 
FROM invoice
WHERE total >= 15;

or


SELECT * 
FROM invoice
WHERE total > 14;

However, if the total contained decimal values, these two statements would not be equivalent. The second statement would return rows with a total value between 14 and 15, like 14.5, whereas the first one would not.

Video Transcript

try it
Your turn! Open the SQL tool by clicking on the LAUNCH DATABASE button below. Then enter in one of the examples above and see how it works. Next, try your own WHERE clauses.

summary
Filtering data using the WHERE clause in SELECT statements can help limit the amount of data being returned based on the conditions listed. There are multiple comparison operators that can be used to filter data.

Source: Authored by Vincent Tran