The WHERE clause

 

The WHERE clause specifies which records from the tables listed in the FROM clause are included in the results of the SELECT statement. If you do not specify a WHERE clause, your query returns all rows from the table. If you specify more than one table in your query and you have not included a WHERE clause or a JOIN clause, your query generates a Cartesian product of the tables.

 

WHERE is optional, but when included, follows FROM. For example, you can select all employees in the sales department:

 

WHERE Dept = 'Sales'

 

or all customers between the ages of 18 and 30

 

WHERE Age Between 18 AND 30

 

WHERE is similar to HAVING. WHERE determines which records are selected. Similarly, once records are grouped with GROUP BY, HAVING determines which records are displayed. Use the WHERE clause to eliminate records you do not want grouped by a GROUP BY clause.

 

Use various expressions to determine which records the SQL statement returns. For example, the following SQL statement selects all employees whose salaries are more than $21,000:

 

SELECT LastName, Salary
FROM Employees
WHERE Salary > 21000