How to Use the WHERE Clause and Operator Symbols
In the majority of cases when you issue a SQL select statement you will want to return particular rows from a table. In other words, you generally do not want to select all rows from your table. The "where" clause allows you to indicate the criteria for what rows to display on your report, or output.
To learn how to use the where clause and its associated operator symbols on an 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.
- Imagine we wish to select the city names of cities whose state abbreviation is equal to 'CA' in the
Citiestable. Execute the following statement:
You have included the where clause on your select statement. The
=(equal) operator symbol is used to indicate we are only interested in cities with a state abbreviation of 'CA'.
- Let's flip this statement around to display cities that are NOT in the state abbreviation of 'CA':
<>(not equal) operator symbol is used to indicate we are interested in cities whose state abbreviation is not equal to 'CA'.
- We might want to display city name and population for those cities with a population greater than 1,000,000:
>(greater than) operator symbol is used in the where clause. As you might suspect, other operator symbols are available such as
<=(less than or equal to) and
>=(greater than or equal to).