The WHERE Clause

You can add a WHERE clause to a SELECT statement to tell MySQL to filter the query results based on a given rule. Rules in a WHERE clause refer to data values returned by the query, and only rows in which the values meet the criteria in the rule are returned.

Filtering on an Exact Value

The simplest type of filter in a WHERE clause uses the equals operator (=) to specify that a data value must match a given value exactly.

The following query retrieves all the contacts for a given company:

mysql> SELECT id, first_name, last_name
    -> FROM customer_contacts
    -> WHERE customer_code = 'SCICORP'; +----+------------+-----------+ | id | first_name | last_name | +----+------------+-----------+ | 4 | Albert | Einstein | | 5 | Charles | ...

Get Sams Teach Yourself MySQL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.