BBampaa

Lessons › SQL › Chapter 2: Filtering

SQL lesson

NOT operator

NOT reverses a condition, so instead of spelling out everything you want, you just name what you don't want and everything else comes back. Like telling a waiter "anything but fish" rather than listing every other item on the menu.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

Excluding Retail Orders with NOT

SELECT * FROM orders
WHERE NOT status = 'cancelled'

Result:

order_idcategorystatusrevenue
O2001Electronicsshipped129.99
O2003Homeshipped89.0
O2005Grocerydelivered34.2
O2006Clothingdelivered58.0

Step by step

  1. The asterisk symbol means all columns.
    SELECT *
  2. FROM tells SQL which table to pull the data from.
    SELECT *
    FROM orders
  3. WHERE keeps only the rows that match your condition.
    SELECT *
    FROM orders
    WHERE NOT status = 'cancelled';

Practice

Try each one in your head first, then open the answer.

1. Using orders, write a query to find rows where category is NOT equal to 'Electronics'.
SELECT * FROM orders WHERE NOT category = 'Electronics'
2. Using products, write a query to find rows where brand is NOT equal to 'NovaTech'.
SELECT * FROM products WHERE NOT brand = 'NovaTech'
3. Using customers, write a query to find rows where customer_id is NOT equal to 'C101'.
SELECT * FROM customers WHERE NOT customer_id = 'C101'
4. Using store_sales, write a query to find rows where store_id is NOT equal to 'ST01'.
SELECT * FROM store_sales WHERE NOT store_id = 'ST01'
5. Using returns, write a query to find rows where order_id is NOT equal to 'O2001'.
SELECT * FROM returns WHERE NOT order_id = 'O2001'

Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.

Try this lesson free →
About 5–10 minutes · no sign-up needed