Using the Greater-Than (>) and Less-Than or Equal-To (<=) operators
Which cities and districts are identified by Mktkey values that are greater than 4 and less than or equal to 12?
SELECT mktkey, hq_city, hq_state, district FROM aroma.market WHERE mktkey > 4 AND mktkey <= 12;
|5||New York||NY||New York|
Conditions evaluate to true or false and can be expressed with comparison operators or comparison predicates. SQL contains the following comparison operators:
|>=||greater than or equal|
|<=||less than or equal|
The example query retrieves and displays all cities and districts whose Mktkey is greater than 4 but less than or equal to 12.
The Mktkey column contains integer values, which are comparable to other numeric values. If you compare an integer to a character, however, the server returns an error message:
SELECT mktkey, hq_city, hq_state, district FROM aroma.market WHERE mktkey > '4'; [IBM][CLI Driver][DB2/LINUX] SQL0401N The data types of the operands for the operation ">" are not compatible. SQLSTATE=42818
You can obtain help interpreting error messages, and identifying corrective action that can be taken, by using the DB2 Information Center. The Center was introduced in Part 1 of this series.
Conditions must compare values of comparable data types. If you attempt to compare unlike data types, the server returns either an error message or an incorrect result. Comparison operators can be used to compare one character string with another, as the following legal condition illustrates:
(city > 'L')
For more information about comparable data types, refer to the SQL Reference Guide .