Skip to main content
Log inGet a demo
Back to SQL Dictionary
Logical operators

SQL NOT

What is SQL NOT?

The SQL NOT operator is a logical operator used to negate a Boolean condition. It reverses the truth value of a condition, changing a true condition into false and vice versa.

When you would use it

You would use the SQL NOT operator when you need to reverse or negate a condition in a SQL query. It is often used to filter out rows that do not meet a specific condition, effectively excluding data that you want to exclude from your query.

Syntax

The SQL NOT operator is typically used in combination with other SQL clauses, such as the WHERE clause. The basic syntax is as follows:

SELECT columns
FROM table_name
WHERE NOT condition;
  • columns: The columns you want to retrieve in the query.
  • table_name: The name of the table containing the data.
  • condition: The condition to be negated with the NOT operator.

Parameter values

  • columns: The columns you want to retrieve in your query.
  • table_name: The name of the table where the data is stored.
  • condition: A Boolean condition that you want to reverse using the NOT operator.

Example query

Suppose we have a table named "employees" with columns "employee_id" and "salary." We want to retrieve the employees who do not earn more than $50,000:

SELECT employee_id, salary
FROM employees
WHERE NOT (salary > 50000);
-- Comment the line below to show it doesn't affect the query.
-- AND department = 'Finance';

In the above query, we use the SQL NOT operator to exclude employees who earn more than $50,000 from the result.

Example table response

Assuming the "employees" table contains the following data:

| employee_id | salary   | department  |
|------------ | -------- |------------ |
| 1          | 60000.00 | Sales       |
| 2          | 55000.00 | Marketing   |
| 3          | 45000.00 | Finance     |
| 4          | 48000.00 | HR          |
| 5          | 75000.00 | Finance     |

The query mentioned earlier would return the following result:

| employee_id | salary   |
|------------ | -------- |
| 3          | 45000.00 |
| 4          | 48000.00 |

This result includes employees who do not earn more than $50,000.

Use cases

  • Filtering rows based on conditions that should not be met.
  • Excluding data that does not fit specific criteria from query results.
  • Inverting or negating a Boolean condition to include or exclude specific data.

SQL languages this is available for

The SQL NOT operator is a standard feature in most relational database management systems (RDBMS) that support SQL. This includes popular RDBMS like MySQL, PostgreSQL, Oracle, SQL Server, and SQLite. The specific syntax and behavior may vary slightly between database systems, but the fundamental functionality remains the same.

Related

SQL OR

SQL NVL

SQL COALESCE

Ready to put your SQL knowledge to work?

Practice writing SQL to call data from the warehouse and sync it into Google Sheets in this 5 minute interactive demo.

Hightouch Audiences user interface.