NOT Operator In Sql

In SQL, The NOT operator is used to reject a condition in a WHERE clause. It is often used in combination with other logical operators such as AND, OR and IS NULL Operator to deal with complex conditions.

NOT Operator used to exclude rows that meet a certain condition from the result set.

NOT Operator returns true if the condition is false and false if the condition is true.

Here's how the NOT operator is typically used in SQL:

NOT Syntax In SQL:

SELECT columnName1, columnName2
FROM tableName
WHERE NOT condition;
  • NOT condition: The NOT Operator used to invert the value.

  • tableName: The name of the table from which you want to retrieve data.

  • columnName1, columnName2: The columns you want to retrieve from the table.

NOT Operator In SQL:

The NOT operator is used to invert a condition. For example, if we want to retrieve all rows where a certain condition is not true.

Example:

SELECT *
FROM products
WHERE NOT category = 'GAMES';

Note: This query retrieves all columns from the products table where the category is not equal to 'GAMES'.

NOT Operator with AND Operator and OR Operator:

We can also use NOT in combination with other logical operators:

NOT with AND Operator Example:

SELECT *
FROM products
WHERE NOT (category = 'GAMES' AND price > 1000);

The above query retrieves all columns from the products table where the category is not 'GAMES' or the price is not greater than 1000.

Parentheses `()` syntax help us to group conditions to specify the order of logical operations.

NOT With OR Operator Example:

SELECT *
FROM products
WHERE NOT (category = 'HEALTH_CARE' OR category = 'GAMES');

The above query retrieves all columns from the products table where the category is neither 'GAMES' nor 'HEALTH_CARE'.

Using NOT with IS NULL:

we can use `IS NULL` Clause with `NOT` Operator in SQL.

Example:

SELECT *
FROM products
WHERE NOT product_price IS NULL;

The above query retrieves all records from the products table and checking `product_price` should not be `NULL` value.