Sometimes we want to perform operations on results that satisfy more than one criteria. In addition to using the where clause we might need to use certain other keywords to make our desire more specific. The keywords are AND and OR.
We use AND when we are specifying more than one conditions which need to be matched for us to be interested in the data. We use OR when either of a set of conditions should be matched. So AND would be much more specific than OR.
For the examples below, assume that we have a table called "employees" which looks like this:
| id | first_name | last_name |
|---|---|---|
| 1 | Paul | Pitterson |
| 2 | Francine | Beecham |
| 3 | Raul | DiNozzo |
| 4 | Anthony | DiNozzo |
If we want to select all the employees which have a last name of "DiNozzo" and first name "Anthony" we would use the following SQL statement:
SELECT * FROM employees WHERE first_name = 'Anthony' AND last_name = 'DiNozzo';
This would return:
| id | first_name | last_name |
|---|---|---|
| 4 | Anthony | DiNozzo |
If we want to select all the employees which have a last name of "DiNozzo" or first name "Paul" we would use the following SQL statement:
SELECT * FROM employees WHERE first_name = 'Paul' OR last_name = 'DiNozzo';
This would return:
| id | first_name | last_name |
|---|---|---|
| 1 | Paul | Pitterson |
| 3 | Raul | DiNozzo |
| 4 | Anthony | DiNozzo |
As we can see, using the AND & OR keywords with the MySQL where clause when writing our SQL can make our queries more powerful and precise.
We hope this MySQL tutorial was useful.