Comparison Operators

Basic SQL
≈ 1 min read

A comparison turns values into a condition

Comparison operators let you compare a column value with another value. Such expressions often form the basis of a WHERE condition.

  • = — equal to;
  • <> — not equal to;
  • > and < — greater than and less than;
  • >= and <= — greater than or equal to and less than or equal to.
SELECT name, price
FROM products
WHERE price <= 500;

The query returns products priced at 500 or less. The boundary is included because <= is used instead of <.

Compare compatible values

The meaning of a comparison depends on the data type. Numbers are compared as numbers, dates as dates, and strings according to the rules and collation settings of the database system. So not only the operator matters, but also the types of the values on both sides.

The standard SQL operator for “not equal” is <>. Some database systems also support !=, but <> better represents portable SQL syntax.