Comparison predicates

Comparison predicate is two expressions separated by a comparison operator. There are six conventional comparison operators: =, >, <, >=, <=, <>.

The data of NUMERIC type (numbers) are compared in accordance with their algebraic values.

The data of CHARACTER STRING type are compared in accordance with their alphabetic sequences. If a1a2an and b1b2bn are two character sequences, the first of these is "less" than the second if 1 < b1 or 1 = b1 and 2 < b2 and so on. Also, it is believed to be 12n < b1b2bm if n < m and 12n = b1b2bn, i.e. if the first string is the prefix of second one. For example, 'folder' < 'for' because the two first letters of these strings coincide, while the third letter of the string 'folder' precedes the third letter in the string 'for'. Inequality 'bar' < 'barber' is also correct because its first string is the prefix of the second string.

The data of DATETIME type is compared in a chronological order.

The data of INTERVAL type (time range) are converted into corresponding types and then compared as ordinary numeric values (of NUMERIC type).

Example 5.2.1

Get information on computers with processor speed not less than 500 MHz and price below $800:

  1. SELECT *
  2. FROM PC
  3. WHERE speed >= 500 AND
  4. price < 800;

The query returns the following data:

code model speed ram hd cd price
1 1232 500 64 5 12x 600
3 1233 500 64 5 12x 600
7 1232 500 32 10 12x 400
10 1260 500 32 10 12x 350

Example 5.2.2

Get information on all those printers that are not matrix and priced below $300:

  1. SELECT *
  2. FROM printer
  3. WHERE NOT (type = 'matrix') AND
  4. price < 300;

Here is the result of that query:

code model color type price
2 1433 y Jet 270
3 1434 y Jet 290

Suggested exercises: 108

Bookmark and Share
aggregate functions Airport ALL AND AS keyword ASCII AVG Battles Bezhaev Bismarck C.J.Date calculated columns Cartesian product CASE cast CHAR CHARINDEX Chebykin check constraint classes COALESCE common table expressions comparison predicates Computer firm CONSTRAINT CONVERT correlated subqueries COUNT CROSS APPLY CTE data type conversion data types database schema DATEADD DATEDIFF DATENAME DATEPART DATETIME date_time functions DDL DEFAULT DEFAULT VALUES DELETE DISTINCT DML duplicates edge equi-join EXCEPT exercise (-2) More tags
The book was updated
month ago
©SQL-EX,2008 [Evolution] [Feedback] [About] [Links] [Team]
All right reserved.