Where
September 13, 1998
Suppose you don't want all of the data
for all of the rows in a table. Instead, perhaps you only
want data for rows that match some criteria. For example,
perhaps I want to see the data for the employees, but I
only am interested in seeing the data for the employees who
make over $45,000 per year.
Well, SQL provides the "where" clause
for just these circumstances. The "where" clause allows you
to specify conditions that a column cell must meet if it
is to be considered a match and be returned in the
results. In SQL syntax terminology, the WHERE clause is called
the predicate.
The generic syntax of the WHERE clause looks
something like the following:
SELECT column_name
FROM table_name
WHERE where_clause;
Consider the following case in which
we ask the database to return only the rows in the
SALES
table in which the Employee number is equal to "101"
SELECT *
FROM SALES
WHERE E_NUM = 101;
In this case, the database would return
the following:
---------------------------------------------
001 001 1 99.99 101 102
003 002 1 865.99 101 103
---------------------------------------------
The WHERE clause can be used in conjunction with
various testing operators besides the "=" sign. Specifically, you
can use the ">", "<", "<=", or ">=" operators to select ranges. Thus,
to get a report of all the employees making more than 45,000 per year,
you might use the following:
SELECT *
FROM EMPLOYEES
WHERE EMP_SALARY > 45000;
If you are comparing a column of the CHARACTER data type, you can
place the match string in single quotes ('').
For example, to find out Rick Tan's phone
number from the
CLIENTS table,
you might use:
SELECT C_PHONE, C_NAME
FROM CLIENTS
WHERE C_NAME = 'Rick Tan';
In this case, the database would
return the following:
C_PHONE C_NAME
------------------------
649-2038 Rick Tan
------------------------
|
Note that although SQL is generally
case insensitive, when you are matching CHARACTERS using
the single quotes, you must be aware of case. Thus,
WHERE name = 'CHRIS' would not return the same as
WHERE name = 'Chris'.
|
Wildcards
Introduction to Databases for the Web | Table of Contents
And, Or, and Not
|