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:

  1. 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.
  2. Now let's explore the where clause and its conditions.
  3. 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: Cities with population between 1 and 5 million or NV Two conditions are present in the where clause. The conditions are separated by the or keyword. Either condition can be true in order for the row to be selected.
  4. 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: Cities in state abbrev CA, FL, NVAgain, two conditions are present in the where clause but now the conditions are separated by the and keyword. Both conditions must be true in order for the row to be selected.