Basics · Lesson 14 of 19

Excluding Rows with NOT

Sometimes it's easier to say what you don't want.

Use != for "not equal" (<> means the same thing):

SELECT first_name, department_id
FROM employees
WHERE department_id != 1;

NOT flips any condition you already know:

SELECT first_name FROM employees
WHERE department_id NOT IN (1, 2);
SELECT first_name FROM employees
WHERE first_name NOT LIKE 'J%';
SELECT first_name, salary FROM employees
WHERE salary NOT BETWEEN 100000 AND 150000;

You've already seen one of these: IS NOT NULL.

Watch out: != never matches NULL. Alice and Robert have no manager, so this quietly leaves them out:

SELECT first_name FROM employees
WHERE manager_id != 1;

To keep them, check for NULL as well:

SELECT first_name FROM employees
WHERE manager_id != 1
   OR manager_id IS NULL;
Check your understanding

List the first name of every employee who is not in department 1.

Examples run on the employees sample database. Open any of them in the playground to experiment.