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 →Example
Everything except
SELECT *
FROM orders
WHERE NOT status = 'cancelled'| order_id | category | status | revenue |
|---|---|---|---|
| O2001 | Electronics | shipped | 129.99 |
| O2003 | Home | shipped | 89.0 |
| O2005 | Grocery | delivered | 34.2 |
| O2006 | Clothing | delivered | 58.0 |
Step by step
- Flips a rule: keep every row that doesn't match.
SELECT * FROM orders WHERE NOT status = 'cancelled' - NOT goes right before the rule it flips.
SELECT * FROM orders WHERE NOT status = 'cancelled' - <> means "is not equal to". It does the same job.
SELECT * FROM orders WHERE NOT status = 'cancelled'
Practice
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 →