SQL queries allow basic conditional logic for refining the result set returned by the query. Conditions in SQL are built using the WHERE clause.

SELECT Title, FirstName, LastName FROM Employees

WHERE Title = 'Sales Representative';

In the preceding query I use a condition in the WHERE clause to eliminate rows returned by the query where the employee's title equals Sales Representative. Output from this query is seen in Figure 8.6.

Using conditions in the WHERE clause to refine the result set.

Using conditions in the WHERE clause to refine the result set.

Note that textual data such as 'Sales Representative' in the WHERE clause's expression must always be enclosed by single quotes.

SQL conditions work much like the conditions you've already learned about in Access VBA in that the WHERE clause's condition evaluates to either true or false. You can use the operators seen in Table 8.1 in SQL expressions.

Table 8.1 Conditional Operators Used in SQL Expressions






Not equal


Greater than


Less than


Greater than or equal to


Less than or equal to

To demonstrate conditional operators, the next query returns the rows in the Products table where the value for units in stock is less than or equal to 5. Output is seen in Figure 8.7.

0 0

Post a comment