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:

  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.
  3. Imagine we wish to select the city names of cities whose state abbreviation is equal to 'CA' in the Cities table. Execute the following statement:
    Cities in state abbrev of CA
    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'.
  4. Let's flip this statement around to display cities that are NOT in the state abbreviation of 'CA':
    Cities in state abbrev that is not CA
    The <> (not equal) operator symbol is used to indicate we are interested in cities whose state abbreviation is not equal to 'CA'.
  5. We might want to display city name and population for those cities with a population greater than 1,000,000:
    Sorted city names in descending sequence
    The > (greater than) operator symbol is used in the where clause. As you might suspect, other operator symbols are available such as < (less than), <= (less than or equal to) and >= (greater than or equal to).
Author: Stephen Withrow

Stephen has over 30 years of experience in training, development, and consulting in a variety of technology areas including Python, Java, C, C++, XML, JavaScript, Tomcat, JBoss, Oracle, and DB2. His background includes design and implementation of business solutions on client/server, Web, and enterprise platforms. Stephen has a degree in Computer Science and Physics from Florida State University.

About Webucator

Webucator provides instructor-led training to students throughout the US and Canada. We have trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft ASP.NET, Microsoft Office, Azure, Windows, Java, Adobe, Python, SQL, JavaScript, Angular and much more. Check out our complete course catalog.