NOT Operator

SQL · Filtering Rows

Theory

NOT reverses a condition: TRUE becomes FALSE and FALSE becomes TRUE. WHERE NOT dept = 'IT' keeps every employee outside IT, the same as dept <> 'IT'. NOT combines with other operators to form NOT IN, NOT BETWEEN, NOT LIKE, NOT EXISTS and IS NOT NULL.

The catch is NULL: NOT UNKNOWN is still UNKNOWN, so a row whose dept is NULL is not returned by either dept = 'IT' or NOT dept = 'IT'. Add OR dept IS NULL if you want those rows too. When negating a combined condition, De Morgan's rules apply: NOT (a AND b) equals NOT a OR NOT b, and NOT (a OR b) equals NOT a AND NOT b. Parentheses make it clear what NOT applies to.

Why we use it

Sometimes it is easier to describe what you do not want: everyone except one department, names not matching a pattern. NOT lets you exclude rows without listing all the other values.

When to use it

Use NOT to exclude a known set of rows. For simple inequality, <> is often clearer. Remember that NULLs are excluded by both a condition and its NOT, so handle them with IS NULL if they matter.

How it works

  1. Read a row
  2. Evaluate the inner condition
  3. Inner condition TRUE? Yes: NOT makes it FALSE: discard. No: Check if FALSE or UNKNOWN.
  4. Was it FALSE? Yes: NOT makes it TRUE: keep the row. No: UNKNOWN stays UNKNOWN: discard the row.
  5. Move to the next row

Syntax

SELECT columns FROM table
WHERE NOT condition;

Example

SELECT * FROM employees WHERE NOT department = 'IT';

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic