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
or
keyword. 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
and
keyword. Both conditions must be true in order for the row to be selected.
Related Articles
- Reset Root Password in MySQL on Windows
- How to Create an ER Diagram for a MySQL Database with Free Tools
- How to Round Up in SQL
- How to Concatenate Strings in SQL
- How to Select All Columns in a Row in SQL
- How to Check Multiple Conditions in SQL (this article)
- How Work with White Space and Semicolons in Simple SQL Selects
- How to Sort Records in SQL
- How to Write Subqueries (Simple and Correlated)