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 →Example
Excluding Retail Orders with NOT
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
- The asterisk symbol means all columns.
SELECT * - FROM tells SQL which table to pull the data from.
SELECT * FROM orders - WHERE keeps only the rows that match your condition.
SELECT * FROM orders WHERE NOT status = 'cancelled';
Practice
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 →