PostgreSQL Not equal to (!=) Operator

In Postgres, an operator is nothing but a reserved keyword, character, or symbol that offers unique functionality. In Postgres, there are various categories of operators, such as comparison operators, logical operators, arithmetic operators, etc. All these operators are used for different purposes.

The NOT EQUAL operator, symbolized as “!=” or “<>”, is one of the comparison operators that check if the input values are equal or not.

This post demonstrates what is a “NOT EQUAL” operator and how it works in Postgres. For a profound understanding, the content of this Postgres blog is classified as follows:

How to Use the Postgres NOT EQUAL Operator:
- With Integers
- With Floating Points
- With Text Data
- On Postgres Table’s Data

How to Use the NOT EQUAL Operator in Postgres?

Postgres Not Equal to (!=) Operator compares the left operand with the right operand and retrieves true if the given operands are not equal, and false if the operand values are equal. For instance, the below syntax shows the basic syntax of the NOT EQUAL operator:

SELECT operand_1 != operand_2;

The operands must be valid (i.e., both operands must have implicitly convertible data types).

In Postgres, the comparison operators are most often used in the WHERE Clause of any statement. For instance, the below syntax shows the syntax of the NOT EQUAL “!=” operator with the WHERE clause:

SELECT column_list
FROM table_name
WHERE col_name != value;

Alternatively, the “<>” operator can be employed to commit the same functionality.

Example 1: Using Not Equal Operator “!=” With Integers

The following example considers a simple expression to show the usage of the Postgres NOT EQUAL “!=” operator:

SELECT 200 + 300 != 400 As result;
img

In the above snippet, the left operand is not equal to the right one; so, it retrieves a boolean “true”.

Note: users may also use the “<>” operator in place of the “!=” operator.

Example 2: Using Not Equal Operator “!=” With Floating Points

The below expression utilizes the Postgres NOT EQUAL “<>” operator with the floating point values:

SELECT 200.25 + 200.75 <> 401.0 As result;
img

In the stated expression, the left operand is equal to the right one; so, it returns “false”.

Example 3: Using Not Equal Operator “!=” With Text Data in Postgres

In the following snippet, the “!=” operator performs the string comparison:

SELECT 'Hello' != 'Hello' As result;
img

This operator performs a case-sensitive comparison on the given strings. For instance, in the below snippet, we utilize the “!=” operator to compare a “lowercase” string with “uppercase”:

SELECT 'hello' != 'HELLO' As result;
img

The output signifies that the “!=” operator performs a case-sensitive comparison.

Example 4: Using Not Equal Operator “!=” on Postgres Table’s Data

A sample table named “std_data” has already been created with the following records:

SELECT * FROM std_data;
img

The following query will fetch all those students whose age is not equal to “20”:

SELECT std_name, std_age
FROM std_data
WHERE std_age != 20;
img

The “!=” operator retrieves all the students except those who are 20 years old.

Conclusion

The NOT EQUAL operator is one of the comparison operators that check if the input values are equal or not. It is symbolized as “!=” or “<>”. Postgres “Not Equal to” Operator compares the left operand with the right operand and retrieves true if the given operands are not equal, and false if the operand values are equal. This post presented numerous examples to explain the usage of the not equal to operator.