Relational Operators

Relational operators produce Boolean values based on the comparison of two operands of the same operand type.

Operator | Description | Usage | First Operand (op1) | Second Operand (op2) | Result Type1 |

= == | Equality | op1 = op2 op1 == op2 | Any type | Same as op1 | Boolean |

!= | Inequality | op1 != op2 | |||

< | Less than | op1 < op2 | Numeric, Boolean, String, Date, Time, DateTime, Interval | Same as op1 | Boolean |

<= | Less than or equal to | op1 <= op2 | |||

> | Greater than | op1 > op2 | Numeric, Boolean, String, Date, Time, DateTime, Interval | Same as op1 | Boolean |

>= | Greater than or equal to | op1 >= op2 | |||

1. The relational operators return null if either operand is a null value. |

Quick Look

Find vehicle models with more than 4 doors:

FROM VehicleModel WHERE doors > 4 RETURN brand, modelName;

Find the availability of the vehicle with the specified license string:

From Vehicle WHERE license == 'ABC1997' RETURN available;

Return the rental events with timestamps that are less than 5 and a half days old:

FROM RentalEvents WHERE ($$NOW - timestamp) <= INTERVAL{DAYS:5, HOURS:12} RETURN *;

Discussion

For general information about operator formats, operands, and precedence, see About Operator Expressions.

Rules for Comparison

Relational operators compare operand values to determine whether they are equal or to determine their relative ordering (less-than, greater-than, and so on). The rules for comparing operands are determined based on the operand type.

The rules in the following table apply to operand types that can be compared either for equality or relative ordering:

Operand Type | Rules for Comparison |

Numeric | Operands may be of different numeric types (integer, floating-point, hexadecimal). If operand types are mixed, implicit type coercion is performed as described in Implicit Type Coercion of Numeric Operands. Equality and relative ordering are determined by the numeric values of the operands. |

Boolean | Operands must of the same type. The Boolean value true is greater than the Boolean value false. If either operand is null, the comparison result is null. |

Date, Time, DateTime | Operands must be of the same type—that is, both dates, both times, or both datetimes. Datetime values may be expressed in different time zones; the operator automatically converts them to the same time zone. Equality and relative ordering are based on the internal numeric representations (ticks) of the operand values. Operands are equal if they represent the same date, time, or datetime. An operand representing an earlier date, time, or datetime is less than an operand representing a later one. |

Interval | Operands must be of the same type. Equality and relative ordering are based on the internal numeric representations (ticks) of the operand values. Operands are equal if they represent intervals of the same duration. An operand representing a shorter interval is less than an operand representing a longer one. |

String | Operands may be of different string types. Equality and relative ordering are based on string length and the comparison of corresponding characters from left to right. Operands are equal if they represent strings of the same length, in which the corresponding characters match. When operands represent strings of the same length but with different characters, the relative ordering is determined by the value of the first nonmatching character. The string containing the character with the lower value is less than the string containing the character with the higher value. An operand representing a shorter string is less than an operand representing a longer string. |

The rules in the following table apply to operand types that can be compared only for equality (or inequality):

Operand Type | Rules for Comparison (Equality/Inequality Only) |

Embedded object | Operands are equal if they represent objects of the same class, and the corresponding attributes from each operand are equal based on the comparison rules for their types. |

Reference | Operands are equal if they have the same object identifier (OID). |

Collection | An operand with fewer elements is less than an operand with more elements, and conversely. Operands are equal if they represent collections with the same number of elements, and if the element values in corresponding positions are equal (based on the comparison rules for the element type). For unordered collections, the order of the elements is determined by an internal iterator. |