SQL tutorial

SQL Where

Learn more about SQL, a standard language for interacting with databases and storing, manipulating, and retrieving data from databases.

Go hands-on with SQL in our free interactive SQL tutorial.

The SQL WHERE clause is used to alter the behavior of a SELECT statement or other SQL commands that are applied to rows of data, in order to either restrict the rows returned by a query or the records affected by other statement types (DELETE, UPDATE, etc.) The SQL WHERE clause is the simplest clause to understand and without question one of the most frequently used.

Let’s return to looking at the employees table. In the previous section, we used a SELECT statement to return all rows from the table, as below:

Code Example
SELECT * FROM employees;

However, this returns all rows from the table – and could be a very large result, as some databases have tables with millions (or even hundreds of millions!) of records. Furthermore, we would like to “drill-down” and look for more specific employees within the employees table matching specific criteria. 

So we will apply a WHERE clause here to alter the behavior of the SELECT statement. A WHERE clause comes after the SELECT statement and takes a form as below:

Code Example
SELECT <columns> FROM <table> WHERE <condition>;

Where, in this form, <condition> must be a SQL expression which evaluates to either True or False for each record in the table – that is, a predicate.

The simplest expression we can write is to check whether a given column is equal to a value we specify using the equality operator (=). This will act as a filter and return only the rows which match the predicate we have written.

For example, let’s look at the employees table again, but only return employees with the first name ‘Georgi’:

Code Example
SELECT * FROM employees WHERE first_name = "Georgi";
emp_nobirth_datefirst_namelast_namegenderhire_date
100011953-09-02GeorgiFacelloM1986-06-26
109091954-11-11GeorgiAtchleyM1985-04-21
110291962-07-12GeorgiItzfeldtM1992-12-27
114301957-01-23GeorgiKlassenM1996-02-27
121571960-03-30GeorgiBarinkaM1985-06-04

We can see above that our SQL query modified with a WHERE clause has only returned rows matching the predicate, and filtered the result set that is returned from the table specified.

Predicates need not be specified using only the equality operator nor using only text fields; we could also look for rows in the employees table where the hire date is greater than a certain value, using the greater than operator (>) and a date value:

Code Example
SELECT * FROM employees WHERE hire_date > "1995-01-01";
emp_nobirth_datefirst_namelast_namegenderhire_date
100161961-05-02KazuhitoCappellettiM1995-01-27
100191953-01-23LillianHaddadiM1999-04-30
100221952-07-08ShahafFamiliM1995-08-22
100241958-09-05SuzettePetteyF1997-05-19
100261953-04-03YongqiaoBerztissM1995-03-20

It should also be noted that predicates need not be applied to fields which are returned in the SELECT statement and operate independently of them; for example, the below is a perfectly valid query with a WHERE clause and will return the result, filtering on the unseen first_name field which is not returned in the result:

Code Example
SELECT last_name, gender, hire_date FROM employees WHERE first_name = "Georgi";

That being said, it is considered a best practice to include the fields used in the predicate for the WHERE clause in the SELECT which makes the logic of the query easier to read, and also to ensure that the desired result is being returned correctly. As such, the below would be preferred to the query above:

Code Example
SELECT first_name, last_name, gender, hire_date FROM employees WHERE first_name = "Georgi";

In this case we would be able to examine the first_name field returned to verify that our predicate is returning the correct result, which would not be possible with the previous query.

Finally, the predicate for a WHERE clause can be any expression (or combination of expressions) as long it evaluates to True or False; this allows specifying detailed criteria for the result returned by a query through usage of logical operators such as AND and OR (covered in greater detail in a later section):

Code Example
SELECT * FROM employees WHERE first_name = "Georgi" AND hire_date > "1995-01-01";
emp_nobirth_datefirst_namelast_namegenderhire_date
114301957-01-23GeorgiKlassenM1996-02-27
152201957-08-03GeorgiPanienskiF1995-07-23
156891962-09-14GeorgiCapobianchiM1995-03-11
351081955-03-17GeorgiDratvaF1998-02-05
453481962-08-22GeorgiNivatF1998-08-28

We can see how even simple SELECT statements combined with a single WHERE clause some predicates begin to unlock the true power of SQL to dive into data!

Learn SQL Today

Get hands-on experience writing code with interactive tutorials in our free online learning platform.

  • Free and fun
  • Designed for beginners
  • No downloads or setup required