What does <>
do in the following WHERE
clause,
WHERE posn_id <> rid
WHERE posn_id <> rid
Will return all rows where both posn_id
and rid
are not NULL
and where they are different.
It's the ANSI SQL-Compliant not equals operator
in a simple comparison predicate
(WHERE
statement). Colloquially, it's the "inequality operator". Though many databases accept an alternative !=
, the spec itself does not mention !=
and it should not be used if <>
is supported.
SQL uses three valued logic, with possible values being true
, false
or unknown
. The WHERE
clause filters out all rows except those where the predicate evaluates to true
.
null
, the operator returns unknown
.=
, the operator returns true.<>
returns false.On null treatment, a similar operator is IS DISTINCT FROM
which treats nulls as ordinary values, from the PostgreSQL docs
For non-null inputs,
IS DISTINCT FROM
is the same as the<>
operator. However, if both inputs arenull
it returns false, and if only one input isnull
it returns true.
For RDBMS specific documentation on comparison operators, see also
It's just another way of spelling the "not equals" operator, an alternative to !=
!=
is another way of spelling <>
" as <>
is the only operator of the two defined by the ansi standard.
Commented
Jan 29, 2018 at 21:43
!=
was added later to please C programmers and was not standard for years
<>
or!=
dba.stackexchange.com
into the site field. (I'd think it would at least come up with a few answers that contain it in their SQL or something, or at least this question.) But I still have a hard time believe it's never come up before on the site.