MySQL

The WHERE Clause Operators

The first WHERE clause we looked at tests for equality determining if a column contains a specific value. MySQL supports a whole range of conditional operators, some of which are listed in table below.

WHERE Clause Operators

Operator Description

=

Equal to

<>

Not equal to

!=

Not equal to

<

Less than

<=

Less than or equal to

>

Greater than

>=

Greater than or equal to

BETWEEN

Between two specified values


Checking Against a Single Value

We have already seen an example of testing for equality. Here's one more:

SELECT prod_name, prod_price
FROM products
WHERE prod_name = 'product 9';

The above statement displayed the following output:

+-----------+------------+
| prod_name | prod_price |
+-----------+------------+
| product 9 |      20.00 |
+-----------+------------+

Checking for WHERE prod_name = 'product 9' returned a single row with a value of product 9. By default, MySQL is not case sensitive when performing matches, and so product 9 and Product 9 matched.

Now look at a few examples to demonstrate the use of other operators.

This first example lists all products that cost less than 8.99:

SELECT prod_name, prod_price
FROM products
WHERE prod_price < 8.99;

The above statement displayed the following output:

+------------+------------+
| prod_name  | prod_price |
+------------+------------+
| product 1  |       5.99 |
| product 2  |       3.99 |
| product 7  |       7.00 |
| product 8  |       1.80 |
| product 10 |       4.75 |
+------------+------------+

The following statement retrieves all products costing 8.99 or less resulting in two additional matches:

SELECT prod_name, prod_price
FROM products
WHERE prod_price <= 8.99;

The above statement displayed the following output:

+------------+------------+
| prod_name  | prod_price |
+------------+------------+
| product 1  |       5.99 |
| product 2  |       3.99 |
| product 3  |       8.99 |
| product 6  |       8.99 |
| product 7  |       7.00 |
| product 8  |       1.80 |
| product 10 |       4.75 |
+------------+------------+

Checking for Nonmatches

This next example displays all products except that has price 8.99:

SELECT prod_name, prod_price
FROM products
WHERE prod_price <> 8.99;

The above statement displayed the following output:

+------------+------------+
| prod_name  | prod_price |
+------------+------------+
| product 1  |       5.99 |
| product 2  |       3.99 |
| product 4  |      13.99 |
| product 5  |       9.09 |
| product 7  |       7.00 |
| product 8  |       1.80 |
| product 9  |      20.00 |
| product 10 |       4.75 |
| product 11 |      10.25 |
+------------+------------+

The following is the same example, except this one uses the != operator instead of <>:

SELECT prod_name, prod_price
FROM products
WHERE prod_price != 8.99;

Checking for a Range of Values

To check for a range of values, you can use the BETWEEN operator. Its syntax is a little different from other WHERE clause operators because it requires two values: the beginning and end of the range. The BETWEEN operator can be used, for example, to check for all products that cost between 5 and 10 or for all dates that fall between specified start and end dates.

The following example demonstrates the use of the BETWEEN operator by retrieving all products with a price between 5 and 10:

SELECT prod_name, prod_price
FROM products
WHERE prod_price BETWEEN 5 AND 10;

The above statement displayed the following output:

+------------+------------+
| prod_name  | prod_price |
+------------+------------+
| product 1  |       5.99 |
| product 2  |       3.99 |
| product 3  |       8.99 |
| product 5  |       9.09 |
| product 6  |       8.99 |
| product 7  |       7.00 |
+------------+------------+

As seen in this example, when BETWEEN is used, two values must be specified the low end and high end of the desired range. The two values must also be separated by the AND keyword. BETWEEN matches all the values in the range, including the specified range start and end values.

Checking for No Value

When a table is created, the table designer can specify whether individual columns can contain no value. When a column contains no value, it is said to contain a NULL value.

What is NULL?

No value, as opposed to a field containing 0, or an empty string, or just spaces.

NULL and Nonmatches You might expect that when you filter to select all rows that do not have a particular value, rows with a NULL will be returned. But they will not. Because of the special meaning of unknown, the database does not know whether they match, and so they are not returned when filtering for matches or when filtering for nonmatches.

When filtering data, make sure to verify that the rows with a NULL in the filtered column are really present in the returned data.

The SELECT statement has a special WHERE clause that can be used to check for columns with NULL values the IS NULL clause. The syntax looks like this:

SELECT prod_name
FROM products
WHERE prod_price IS NULL;

This statement returns a list of all products that have no price (an empty prod_price field, not a price of 0), and because there are none, no data is returned. The customers table, however, does contain columns with NULL values the cust_email column contains NULL if a customer has no email address on file:

SELECT cust_id
FROM customers
WHERE cust_email IS NULL;

The above statement displayed the following output:

+---------+
| cust_id |
+---------+
|       2 |
|       5 |
+---------+

When to Use Quotes

If you look closely at the conditions used in the examples' WHERE clauses, you will notice that some values are enclosed within single quotes such as 'product 9', and others are not. The single quotes are used to delimit strings. If you are comparing a value against a column that is a string datatype, the delimiting quotes are required. Quotes are not used to delimit values used with numeric columns.

by BrainBellupdated
Advertisement: