The SQL WHERE statement, also known as the SELECT WHERE statement, provides the ability to select rows conditionally. This lesson discusses how to use the SQL WHERE statement.
Note: This lesson uses the MySQL Sakila demo database. If you haven't already, follow the steps to install the Sakila DB, then head back here.
What Does the SQL Select Where Statement Do?
While a normal SELECT will select all data from the columns and table provided, SQL WHERE will filter the data selected.
How to Use Where in SQL
Now that you understand how to SELECT all data from a given table, it's time to understand how to filter that data.
Example 1: SQL Select Where First Name
Say that you want to select all actors from the actor table with the first name "Julia".
But, you ONLY want to select actors with the first name "Julia". In this case, and in so many others, you use the WHERE clause. The WHERE keyword is used to filter results to those who pass a certain boolean condition. For instance:
SELECT * FROM sakila.actor WHERE first_name = "JULIA";
With the result:
Example 2: Select Where ID
Now, let's say you only want to retrieve the actor who has the actor_id of 123.
SELECT * FROM sakila.actor WHERE actor_id = 123;
With the result:
Example 3: Select Where Length
And you can go on and on like this, only selecting records WHERE a certain condition is true. For instance:
SELECT * FROM sakila.film WHERE length > 184;
Summary: the SQL Where Statement
In this lesson, you learned about the SQL SELECT WHERE statement, and how to use WHERE in SQL. The SQL WHERE statement allows you to filter the rows selected with a boolean condition.
SQL WHERE Syntax
Here's the syntax for the SQL WHERE statement, where you can substitute the variables in < > with your values.
SELECT <YOUR_COLUMN_NAMES> FROM <YOUR_TABLE> WHERE <YOUR_CONDITION>;
Next up, you'll explore the AND, OR, and NOT operators!