In Mysql, The NOT operator is used to invert 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 excludes 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 MySQL:
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.
The NOT operator is used to negate a condition. For example, if we want to retrieve all rows where a certain condition is not true:
SELECT * FROM products WHERE NOT category = 'GAMES';
Note: The above query retrieves all columns from the products table where the category is not equal to 'GAMES'.
We can also use "NOT" in combination with other logical operators:
SELECT * FROM products WHERE NOT (category = 'GAMES' AND price > 1000);
Note: The above query retrieves all columns from the "products" table where the category is not equal to 'GAMES' and the `price` is not greater than `1000`.
Parentheses `()` syntax helps us to group conditions to specify the order of logical operations.
SELECT * FROM products WHERE NOT (category = 'HEALTH_CARE' OR category = 'GAMES');
Note: The above query retrieves all columns from the "products" table where the category is neither 'GAMES' nor 'HEALTH_CARE'.
Parentheses `()` syntax helps us to group conditions to specify the order of logical operations.
we can use `IS NULL` Clause with `NOT` Operator in MySQL.
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.