Hello! Welcome to the fourth lesson in our module on SQL fundamentals.
In our last lesson, you learned how to filter data using the WHERE clause with a single condition. This is a great starting point, but most real-world data retrieval tasks require you to be more specific by combining several criteria.
Today's lesson focuses on precisely that. Our learning outcome is to combine multiple conditions in a WHERE clause using AND and OR operators. These logical operators are the foundation for building complex and powerful queries. Given your background in programming and interest in logic, you'll likely recognize these concepts, as they are fundamental building blocks in almost any language (like && and || in C).
Logical Operators: AND and OR
When filtering data, we often need to check if multiple conditions are met simultaneously or if any one of several conditions is met. This is where the AND and OR operators come into play.
AND: Use this when you need rows that satisfy all of the specified conditions.OR: Use this when you need rows that satisfy at least one of the specified conditions.
The following image shows the truth tables for these operators, which provide a clear, logical map of their behavior. In these tables, 1 represents TRUE and 0 represents FALSE.

Let's break them down one by one.
The AND Operator: Combining "Must-Haves"
The AND operator is used to filter results based on two or more conditions that must all be true. Think of it as a series of gates; a row of data must pass through every gate to be included in the final result.
The syntax is straightforward:
SELECT column_name(s)
FROM table_name
WHERE condition1 AND condition2 AND ...;
Let's watch a quick demonstration.
How to Filter with the WHERE clause in SQL
This short clip from Becoming a Data Scientist clearly demonstrates how to use AND to apply multiple filters to a query.
Watch from 04:35 to 05:00. Notice how the query finds products that belong to category ID 1 AND have a unit price greater than 20.
For example, if you wanted to find all employees in the 'Sales' department who were hired after January 1, 2023, you would write:
SELECT first_name, last_name, hire_date
FROM employees
WHERE department = 'Sales' AND hire_date > '2023-01-01';
Only employees who meet both criteria will be returned.
Test your understanding!
You have a products table with category (text) and stock_quantity (integer) columns. Write a query to find all products in the 'Electronics' category that have fewer than 10 units in stock.
Show answer
SELECT *
FROM products
WHERE category = 'Electronics' AND stock_quantity < 10;
The OR Operator: Providing Alternatives
The OR operator is used when you want to retrieve rows that meet at least one of several conditions. It's useful when you're looking for data that could fall into one of a few different buckets.
The syntax is similar to AND:
SELECT column_name(s)
FROM table_name
WHERE condition1 OR condition2 OR ...;
Here is another quick clip showing the OR operator in action.
How to Filter with the WHERE clause in SQL
This clip, also from Becoming a Data Scientist, shows how to use OR to retrieve rows that match any one of several conditions.
Watch from 05:00 to 05:26. The query is looking for products where the category ID is 1, OR 2, OR 3.
For instance, to find all customers who live in either 'New York' or 'London', you would write:
SELECT customer_name, city
FROM customers
WHERE city = 'New York' OR city = 'London';
This query will return customers from both cities.
Combining AND and OR: The Importance of Precedence
This is where things get interesting and where a precise, logical approach is critical. What happens when you mix AND and OR in the same WHERE clause?
SQL evaluates AND operators before OR operators. This is a fundamental rule of operator precedence, similar to how multiplication is done before addition in mathematics.
Let's say you want to find all products that are in Category 1 or Category 2, and you only want to see products with a price greater than $15. A common mistake would be to write:
-- This query is logically flawed!
SELECT *
FROM products
WHERE category_id = 1 OR category_id = 2 AND unit_price > 15;
Because AND is evaluated first, the database interprets this as:
"Find all products where category_id = 1... OR... find all products where (category_id = 2 AND unit_price > 15)."
This is not what we wanted! We would get all products from Category 1, regardless of price.
The following video perfectly illustrates this common pitfall and how to correct it.
How to Filter with the WHERE clause in SQL
Pay close attention to this clip. It highlights why understanding operator precedence is crucial and shows how to use parentheses to enforce the correct logical grouping.
Watch from 05:26 to 06:40. The presenter first runs the flawed query and points out the incorrect results. Then, they add parentheses () to fix the logic and get the intended result.
The Solution: Use Parentheses ()
To ensure your conditions are evaluated in the order you intend, you should always use parentheses to group OR conditions together. Parentheses override the default operator precedence.
The correct query for our example would be:
-- This is the correct way
SELECT *
FROM products
WHERE (category_id = 1 OR category_id = 2) AND unit_price > 15;
Now, the database understands the logic as:
"Find all products where (category_id is 1 OR 2)... AND... the unit_price is greater than 15."
This connection to mathematical order of operations is a powerful mental model. To explore this idea further, I recommend reading this short article.
SQL AND / OR Order of Operations
This article from Ben Nadel's blog, titled 'SQL AND / OR Order of Operations', reinforces the concept of operator precedence. The key insight is in the first reader comment, which draws a brilliant analogy between logical operators and arithmetic.
Read the main post and then pay special attention to the first comment by 'unicode'. The author explains that you can think of AND as multiplication and OR as addition, which makes the order of operations (AND before OR) intuitive.
Best Practice: Even when your query logic might work without them, use parentheses. It makes your queries unambiguous and far easier for you and others to read and maintain.
Test your understanding!
You have a support_tickets table with the columns priority (text: 'High', 'Medium', 'Low'), status (text: 'Open', 'Closed'), and agent_id (integer).
Write a query to find all tickets that are either 'High' or 'Medium' priority, AND are currently assigned to agent_id 5.
Show answer
SELECT *
FROM support_tickets
WHERE (priority = 'High' OR priority = 'Medium') AND agent_id = 5;
Without the parentheses, the query WHERE priority = 'High' OR priority = 'Medium' AND agent_id = 5 would incorrectly return all 'High' priority tickets (regardless of agent) plus all 'Medium' priority tickets assigned to agent 5.
For a comprehensive review of these concepts, including syntax and best practices, the following resource is very helpful.
SQL AND Operator | Combine Conditions
The article 'SQL AND Operator' from InterviewMaster.ai provides a structured overview of using AND, its interaction with OR, and general best practices. It's a great summary to solidify your understanding.
Please read two sections: Start at the heading 'AND with Other Logical Operators' and read down to the end of that section. Then, read the section 'Best Practices for SQL AND Operator'. Focus on the points about operator precedence, using parentheses, and performance considerations.
Conclusion
You've now learned how to create much more sophisticated filters in your queries. By combining conditions with AND and OR, you can pinpoint data with much greater precision.
Key Takeaways:
- The
ANDoperator requires all conditions to be true. - The
ORoperator requires at least one condition to be true. - SQL has an operator precedence rule:
ANDis evaluated beforeOR. - Using parentheses
()is the best practice to control the order of evaluation and make your query logic clear and bug-free.
Preview of the Next Lesson:
While AND and OR are powerful, they can sometimes be clumsy. For example, checking if a category is one of five different values would require four OR statements. In the next lesson, we'll explore more elegant and efficient ways to filter data using operators like IN (for checking against a list of values), BETWEEN (for checking a range), and LIKE (for flexible text pattern matching).