How to Check Multiple Conditions in SQL
See SQL: Tips and Tricks for similar articles.
A select statement in SQL may contain one or more conditions (also known as predicates) in the where clause. A condition evaluates to true or false or unknown. The where clause must return a true value for a particular row to be selected.
To learn how to check multiple conditions in a SQL select statement, follow these steps:
- You'll need to setup the MySQL database tables. The instructions for the setup can be found in How to add comments in simple SQL selects. Follow steps 1 through 7 in this topic before proceeding to the next step.
- Now let's explore the where clause and its conditions.
- Suppose we want to display cities whose population is between 1,000,000 and 5,000,000 people or that are located in
Nevada (state abbreviation = 'NV'). Execute the following statement:
Two conditions are present in the where clause. The conditions are separated by the orkeyword. Either condition can be true in order for the row to be selected. - Our next requirement is to display cities in California (state abbreviation = 'CA') only where the population is between 1,000,000 and 5,000,000 people:
Again, two conditions are present in the where clause but
now the conditions are separated by the andkeyword. Both conditions must be true in order for the row to be selected.
