By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Zero-fluff, hyper-practical guide for real projects and certifications (PL-300, Google Data Analytics, etc.)
You’re analyzing customer data for an e-commerce company. Your boss asks: "Show me all high-value customers (spending > $1,000) who haven’t made a purchase in the last 6 months, live in California or New York, and whose email isn’t from a free provider (e.g., Gmail, Yahoo)."
Without filtering operators, you’d manually scroll through thousands of rows. With them, you write one SQL query that returns the exact list in seconds.
Why this matters in production:- Performance: Poor filtering = full table scans = slow queries = angry users.- Accuracy: Missing a NOT or misplacing parentheses in AND/OR = wrong results = bad business decisions.- Maintainability: Complex filters in legacy code become "spaghetti SQL" if not structured clearly.
NOT
AND/OR
Real-world scenario:You inherit a dashboard that’s timing out. The query fetches all orders, then filters in Python. Fix it by pushing filters to SQL (where they belong) using WHERE, IN, BETWEEN, etc. Instant 10x speedup.
WHERE
IN
BETWEEN
AND
WHERE age > 30 AND country = 'USA'
WHERE (age > 30 AND country = 'USA') OR status = 'VIP'
WHERE age > 30 AND (country = 'USA' OR status = 'VIP')
OR
WHERE country = 'USA' OR country = 'Canada'
WHERE country IN ('USA', 'Canada')
WHERE NOT country = 'USA'
WHERE country != 'USA'
NULL
WHERE NOT (age > 30)
WHERE age <= 30
WHERE country IN ('USA', 'Canada', 'Mexico')
EXISTS
WHERE order_date BETWEEN '2023-01-01' AND '2023-12-31'
BETWEEN 1 AND 10
> 1 AND < 10
LIKE
%
_
WHERE email LIKE '%@gmail.com'
%gmail
IS NULL
IS NOT NULL
WHERE email IS NULL
''
WHERE email = ''
WHERE age > 30 OR country = 'USA' AND status = 'VIP'
WHERE age > 30 OR (country = 'USA' AND status = 'VIP')
Task: Write a query to find: - Customers from California or New York.- Who spent between $500 and $5,000 in the last year.- Whose email isn’t from a free provider (e.g., Gmail, Yahoo).- Who haven’t made a purchase in the last 3 months.
Prerequisites:- A SQL environment (e.g., PostgreSQL, MySQL, BigQuery, or SQLite).- Sample data (use this schema): ```sql CREATE TABLE customers ( customer_id INT, name VARCHAR(100), email VARCHAR(100), state VARCHAR(2), total_spent DECIMAL(10,2), last_purchase_date DATE );
INSERT INTO customers VALUES (1, 'Alice', '[email protected]', 'CA', 1200.50, '2023-10-15'), (2, 'Bob', '[email protected]', 'NY', 3000.00, '2023-12-01'), (3, 'Charlie', '[email protected]', 'TX', 200.00, '2023-11-20'), (4, 'Dana', '[email protected]', 'CA', 750.00, '2023-08-10'), (5, 'Eve', NULL, 'NY', 5000.00, '2023-07-01'); ```
SELECT * FROM customers WHERE state IN ('CA', 'NY');
Output:| customer_id | name | email | state | total_spent | last_purchase_date | |-------------|-------|----------------|-------|-------------|--------------------| | 1 | Alice | [email protected]| CA | 1200.50 | 2023-10-15 | | 2 | Bob | [email protected]| NY | 3000.00 | 2023-12-01 | | 4 | Dana | [email protected] | CA | 750.00 | 2023-08-10 | | 5 | Eve | NULL | NY | 5000.00 | 2023-07-01 |
SELECT * FROM customers WHERE state IN ('CA', 'NY') AND total_spent BETWEEN 500 AND 5000;
Output: Same as above (all rows meet the condition).
NOT LIKE
SELECT * FROM customers WHERE state IN ('CA', 'NY') AND total_spent BETWEEN 500 AND 5000 AND email NOT LIKE '%@gmail.com' AND email NOT LIKE '%@yahoo.com';
Output:| customer_id | name | email | state | total_spent | last_purchase_date | |-------------|------|---------------|-------|-------------|--------------------| | 2 | Bob | [email protected]| NY | 3000.00 | 2023-12-01 | | 4 | Dana | [email protected] | CA | 750.00 | 2023-08-10 |
SELECT * FROM customers WHERE state IN ('CA', 'NY') AND total_spent BETWEEN 500 AND 5000 AND email NOT LIKE '%@gmail.com' AND email NOT LIKE '%@yahoo.com' AND last_purchase_date < '2023-10-01'; -- No purchases in last 3 months (assuming today is 2024-01-01)
Output:| customer_id | name | email | state | total_spent | last_purchase_date | |-------------|------|---------------|-------|-------------|--------------------| | 4 | Dana | [email protected] | CA | 750.00 | 2023-08-10 |
SELECT * FROM customers WHERE state IN ('CA', 'NY') AND total_spent BETWEEN 500 AND 5000 AND (email NOT LIKE '%@gmail.com' OR email IS NULL) AND (email NOT LIKE '%@yahoo.com' OR email IS NULL) AND last_purchase_date < '2023-10-01';
Output: Now includes Eve (who has a NULL email).| customer_id | name | email | state | total_spent | last_purchase_date | |-------------|------|-------|-------|-------------|--------------------| | 4 | Dana | [email protected] | CA | 750.00 | 2023-08-10 | | 5 | Eve | NULL | NY | 5000.00 | 2023-07-01 |
LIKE '%gmail'
LIKE 'gmail%'
WHERE age > 30 AND country = 'USA' OR status = 'VIP'
sql WHERE state IN ('CA', 'NY') AND total_spent BETWEEN 500 AND 5000 AND ( email NOT LIKE '%@gmail.com' OR email IS NULL )
= NULL
NOT IN
WHERE id NOT IN (1, 2, NULL)
NOT EXISTS
# GOOD: Parameterized cursor.execute("SELECT * FROM users WHERE email = %s", (user_input,)) ```
=
WHERE email = NULL
Typical question patterns:1. AND/OR precedence: - Question: What does WHERE age > 30 OR country = 'USA' AND status = 'VIP' return? - Trap: Without parentheses, AND takes precedence. Correct answer: WHERE age > 30 OR (country = 'USA' AND status = 'VIP'). - Key: Always use parentheses to avoid ambiguity.
email
WHERE email != '%@gmail.com'
WHERE email NOT LIKE '%@gmail.com'
WHERE email NOT LIKE '%@gmail.com' OR email IS NULL
Correct answer: C (because NULL emails won’t match NOT LIKE).
IN vs OR:
WHERE country = 'USA' OR country = 'Canada' OR ...
WHERE country IN ('USA', 'Canada', ...)
Correct answer: B (same logic, better performance).
BETWEEN inclusivity:
Challenge:Write a query to find all products that: - Are in the "Electronics" or "Appliances" category.- Have a price between $100 and $1,000.- Have a name containing "Pro" (case-insensitive).- Are not discontinued (discontinued = FALSE).
discontinued = FALSE
Solution:
SELECT * FROM products WHERE category IN ('Electronics', 'Appliances') AND price BETWEEN 100 AND 1000 AND LOWER(name) LIKE '%pro%' AND discontinued = FALSE;
Why it works:- IN efficiently checks multiple categories.- BETWEEN includes the boundary prices.- LOWER(name) LIKE '%pro%' handles case insensitivity.- discontinued = FALSE filters out discontinued products.
LOWER(name) LIKE '%pro%'
WHERE age > 30
WHERE country = 'USA' OR country = 'CA'
WHERE country IN ('USA', 'CA')
WHERE price BETWEEN 100 AND 1000
WHERE name LIKE '%Pro%'
WHERE email IS NOT NULL
!= NULL
AND/OR/NOT
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.