Advanced Filtering: Multiple Conditions and Dates
The Power of Multiple Filters
The Shopping Filter Analogy
Think of AND and OR like filters on an e-commerce site.
- AND: If you filter for shoes that are "Size 10" AND "Red", you get a very specific result because both must be true.
- OR: If you filter for "Nike" OR "Adidas", your results expand because you are happy with either brand.
Welcome to Module 1. In this lesson, we move beyond simple queries to handle complex, real-world business questions using multiple filters. Think of these like the sidebar on your favorite shopping site. When you select 'Red' AND 'Size 10', you're being very specific. But when you select 'Nike' OR 'Adidas', you're widening your search to include more options.
- AND narrows results (both conditions true).
- OR broadens results (at least one condition true).
Our Data: The Orders Table
We will use the Orders table to practice these concepts. This table tracks every transaction in our store.
| order_id | customer_id | order_date | total_amount | status |
|---|---|---|---|---|
| 101 | 5 | 2023-10-01 | 150.00 | Shipped |
| 102 | 8 | 2023-10-15 | 45.00 | Pending |
| 103 | 5 | 2023-11-02 | 210.00 | Shipped |
To master these filters, we'll look at our Orders table. Notice the 'order_date' column uses a standard format, and 'total_amount' is a number we can compare. We'll use these specific columns to find high-value orders or specific timeframes.
- order_date is stored in YYYY-MM-DD format.
- total_amount is numeric, allowing for mathematical comparisons.
Combining Conditions with AND & OR
Narrowing vs. Broadening
The AND operator returns records where all conditions are true. The OR operator returns records where at least one condition is true.
SELECT * FROM Orders WHERE total_amount > 100 AND status = 'Shipped';
Let's see the syntax in action. In this query, we are looking for orders that are BOTH over 100 dollars AND have already shipped. This combined logic helps us isolate high-value, completed transactions specifically.
- AND requires all conditions to be met.
- OR requires only one condition to be met.
Filtering by Date Ranges
Dates in SQL
SQL uses the ISO format: 'YYYY-MM-DD'. You can use comparison operators or the BETWEEN operator for ranges.
SELECT * FROM Orders WHERE order_date BETWEEN '2023-10-01' AND '2023-10-31';
When working with dates, SQL expects a specific string format: Year, Month, Day. The BETWEEN operator is a clean way to find everything in a specific month, like October 2023. Remember, BETWEEN includes both the start and end dates you provide.
- Dates must be wrapped in single quotes.
- BETWEEN is inclusive of both the start and end dates.
The Parentheses Pitfall
Operator Precedence
SQL processes AND before OR. Without parentheses, your logic might break!
Goal: Find 'Pending' or 'Shipped' orders over $100.
Correct:WHERE (status = 'Pending' OR status = 'Shipped') AND total_amount > 100
Here is a common beginner pitfall. If you mix AND and OR without parentheses, SQL will process the AND first. This might result in showing 'Shipped' orders over 100, plus EVERY 'Pending' order regardless of price! Always wrap your OR conditions in parentheses to tell SQL exactly how to group your logic.
- SQL evaluates AND before OR by default.
- Use parentheses to group OR conditions when mixing with AND.
Exercise 1: Basic AND Filtering
Task: Find all orders where the total_amount is less than 50 AND the status is 'Pending'.
It's time for your first challenge. Write a query to find small, unpaid orders. We need the total amount to be under 50 and the status to be 'Pending'. Type your query and click 'Run'.
- Use the < operator for 'less than'.
- Combine conditions using AND.
Exercise 2: Filtering by Date Range
Task: Retrieve all orders placed between '2023-11-01' and '2023-11-30'.
Now, let's look at the month of November. Write a query to find all orders placed between November 1st and November 30th, 2023.
- Use the BETWEEN operator or comparison operators.
- Ensure dates are in 'YYYY-MM-DD' format.
Exercise 3: The Advanced Logic Challenge
Task: Find orders that are either 'Shipped' or 'Cancelled', but ONLY if the total_amount was greater than 200.
Final challenge! This one requires careful logic. We want high-value orders (over 200) that have reached a final state: either 'Shipped' or 'Cancelled'. Don't forget your parentheses!
- Use parentheses for the OR condition.
- Use > for 'greater than'.