PostgreSQL – NOT Operator
The NOT operator is used to negate a condition or expression. The NOT operator can be used with various other operators and functions to create negative conditions. The syntax for using the NOT operator is quite straightforward:
NOT condition
Here are a couple of examples to illustrate its usage:
Using the NOT operator with a simple comparison:
Suppose you have a table named employees
with columns employee_id
and is_active
, and you want to retrieve all records where the employees are not active. You would use the following SQL query:
SELECT * FROM employees WHERE NOT is_active;
This query would fetch all rows from the employees
table where the is_active
column has a value of false
.
Using the NOT operator with other conditions:
SELECT * FROM products WHERE NOT (price > 100);
This query would retrieve all rows from the products
table where the price
is not greater than 100.
Remember that the NOT operator can be used in combination with various logical operators (AND
, OR
) and comparison operators (=
, >
, <
, >=
, <=
, etc.) to create complex conditions that check for negations or the absence of certain conditions.