BBampaa

Lessons › SQL › Chapter 2: Filtering

SQL lesson

NOT operator

NOT flips a rule. WHERE NOT status = 'cancelled' keeps every order that isn't cancelled. It's quicker to name what you don't want than to list everything you do.

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

Example

Everything except

SELECT *
FROM orders
WHERE NOT status = 'cancelled'

Result:

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

Step by step

  1. Flips a rule: keep every row that doesn't match.
    SELECT * FROM orders WHERE NOT status = 'cancelled'
  2. NOT goes right before the rule it flips.
    SELECT * FROM orders WHERE NOT status = 'cancelled'
  3. <> means "is not equal to". It does the same job.
    SELECT * FROM orders WHERE NOT status = 'cancelled'

Practice

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

1. Using orders, show every order that isn't Electronics.
SELECT * FROM orders WHERE NOT category = 'Electronics'
2. Using products, show every product except NovaTech's.
SELECT * FROM products WHERE NOT brand = 'NovaTech'
3. Using orders, show the orders that aren't cancelled.
SELECT * FROM orders WHERE NOT status = 'cancelled'
4. Using store_sales, show every sale except the online ones.
SELECT * FROM store_sales WHERE NOT channel = 'online'
5. Using shipments, show the shipment_id and status of shipments that aren't delivered yet.
SELECT shipment_id, status FROM shipments WHERE NOT status = 'delivered'

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