LogoInterview Master

SQL Learning Hub

SQL NOT Operator

Understanding the SQL NOT Operator

The NOT operator in SQL is used to negate a condition in a WHERE clause or HAVING clause. It reverses the result of the condition that follows it, making TRUE conditions FALSE and FALSE conditions TRUE.

Basic Syntax

Common SQL NOT Operator Interview Questions

  • How does the NOT operator work with complex conditions?
  • How does NOT interact with NULL values?
  • What is De Morgan's Law in SQL?
  • When would you use NOT versus using the opposite comparison operator?

How the SQL NOT Operator Works

Basic NOT Usage

The NOT operator reverses the result of the condition it precedes. It changes TRUE to FALSE and FALSE to TRUE.

NOT with Other Comparison Operators

You can often replace NOT with the opposite comparison operator for clearer code.

Truth Table for NOT

The NOT operator follows this simple truth table for a condition A:

ANOT A
TRUEFALSE
FALSETRUE
NULLNULL

Note: NOT NULL remains NULL. The NOT operator does not convert NULL values to TRUE or FALSE.

NOT with Other SQL Operators

NOT with Other Logical Operators

When combining NOT with AND or OR, you can apply De Morgan's Laws to simplify the logic.

De Morgan's Laws:

  • NOT (A AND B) is equivalent to (NOT A) OR (NOT B)
  • NOT (A OR B) is equivalent to (NOT A) AND (NOT B)

NOT IN Operator

NOT can be combined with IN to exclude rows where a column matches any value in a list.

NOT LIKE Operator

Use NOT with LIKE to find strings that don't match a pattern.

NOT NULL Handling

Use IS NOT NULL to find non-null values in a column.

Important: Always use IS NOT NULL rather than NOT IS NULL or NOT column = NULL. The latter are incorrect syntax or will not work as expected.

Practical SQL NOT Examples

Example 1: Excluding Specific Content

This example shows how to find all videos except those with specific keywords:

Example 2: Finding Incomplete User Records

This example shows how to find users with incomplete information:

Example 3: Complex Filtering with NOT

This example demonstrates how to use NOT with complex conditions:

Translation: This query finds recent videos that are NOT (popular tutorials OR popular challenges OR very popular videos).

Best Practices for SQL NOT Operator

1. Simplify When Possible

Where appropriate, use negative comparison operators (!=, <>, etc.) instead of NOT for better readability.

2. Use Parentheses for Complex NOT Expressions

When using NOT with complex expressions, use parentheses to make it clear what part of the expression is being negated.

3. Be Careful with NULL Values

Remember that NOT NULL is still NULL. Always use IS NOT NULL for checking non-null values, not NOT column IS NULL.

4. Consider Performance

Some databases may optimize queries better when written without NOT. If performance is critical, test both forms.

Test Your Knowledge

Take a quick quiz to reinforce what you've learned about SQL NOT Operator

Ready for hands-on SQL practice?

We have 200+ questions on real companies and real products.

Find a question to practice
Dawn AI