Skip to content

Refine Database Retrievals with Comparison Predicates

July 19, 2017

domenico-loia-310197 (1)In a previous post, we saw that, using SQL, it’s easy to retrieve all the data contained in a database table with a simple SELECT statement, such as:

SELECT * FROM customers ;

We also saw that it’s almost as easy to retrieve only what we want from a table, leaving behind all the rest:

SELECT * FROM customers

                WHERE state = ‘CA’ ;

That returns only the customers located in California.

The equals operator (=) is an example of a comparison operator and when two operands are compared with the equals operator, it is called a comparison predicate. A comparison predicate either evaluates to a true value or a false value. A customer is either located in California or she is not.

In addition to the ‘is equal to’ operator, there are five additional comparison operators. They are:

Is not equal to (<>)

Is less than (<)

Is greater than (>)

Is less than or equal to (<=)

Is greater than or equal to (>=)

All six of these operators may be used with numerical operands. It makes sense to say that:

One number is equal to another number

One number is unequal to another number

One number is less than another number

One number is greater than another number

One number is less than or equal to another number

One number is greater than or equal to another number

Similarly, all six operators make sense when applied to dates and times. However, all six do not make sense when applied to text strings. One string may be equal to another string, or unequal to it. However if you try to apply any of the others, such as less than, you will probably not receive the result you are looking for.

Comparison predicates enable you to zero in on the exact information that you want to extract from a database.

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: