SQL AND & OR
The SQL AND & SQL OR keywords are used together with the SQL WHERE clause to join two or more search conditions specified in the WHERE clause. Let's use the weather table to illustrate how to use SQL AND & SQL OR keywords:
City | AverageTemperature | Date |
New York | 22 C | 10/10/2005 |
Seattle | 21 C | 10/10/2005 |
Washington | 20 C | 10/10/2005 |
New York | 18 C | 10/09/2005 |
Seattle | 20 C | 10/09/2005 |
Washington | 17 C | 10/09/2005 |
Here is how to select all rows where the city is either Washington or New York:
SELECT *
FROM Weather
WHERE City = 'Washington' OR City = 'New York'
The result of this SQL OR expression will be:
City | AverageTemperature | Date |
New York | 22 C | 10/10/2005 |
Washington | 20 C | 10/10/2005 |
New York | 18 C | 10/09/2005 |
Washington | 17 C | 10/09/2005 |
The following SQL statement using the SQL AND keyword will select all dates on which the average temperature in Washington was greater than 18 C:
SELECT Date
FROM Weather
WHERE City = 'Washington' AND AverageTemperature > 18
Here is the result of this SQL AND expression:
Date |
10/10/2005 |