O'Reilly logo

Sams Teach Yourself MySQL by Chris Newman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 | ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required