Combining Conditions with AND & OR
A step-by-step, hands-on SQL practice lesson — with a live query playground
So far every filter you've written has checked one thing at a time. Real questions are rarely that simple — "Sales employees who are also active," or "anyone in Sales or Marketing." This lesson covers AND and OR, the two keywords that let you combine multiple conditions into one precise query — plus the single most common mistake beginners make when mixing them.
Introduction
What You'll Learn — and Why It Matters
🎯 Objective
By the end of this lesson, you will be able to:
- Use
ANDto require that every condition in a query is true. - Use
ORto match rows where at least one condition is true. - Combine
ANDandORcorrectly using parentheses. - Avoid the classic mistake of mixing them without grouping, which silently changes your results.
⚠️ What to Avoid
- Mixing
ANDandORin one condition without parentheses — MySQL evaluatesANDbeforeOR, which can quietly return the wrong rows. - Assuming
ORnarrows results likeANDdoes —ORactually widens them. - Writing
WHERE department = 'Sales' AND 'HR'instead of repeating the column name on both sides.
💡 Important to Know
Think of AND as narrowing a search — every extra condition makes the result set smaller or the same size, never bigger. OR does the opposite — every extra condition can only make the result set bigger or the same size. When you mix both in one query, wrap the OR part in parentheses so it's evaluated as one unit.
Requirements
What You'll Need Before Starting
SELECT, aliases, and basic WHERE filtering from Module 1.No installation needed for the live playground further down this page — it runs entirely in your browser.
Practice
Step-by-Step Practice
Follow along in order — each step builds on the one before it.
1 Open MySQL (CLI or phpMyAdmin)
Open a terminal and connect with the MySQL command-line client, or open phpMyAdmin at http://localhost/phpmyadmin/.
mysql -u root -p
The basic syntax patterns for this lesson look like this:
SELECT columns FROM table WHERE condition1 AND condition2; SELECT columns FROM table WHERE condition1 OR condition2;
2 Create a Database and Verify It
CREATE DATABASE module6_practice; SHOW DATABASES; USE module6_practice;
Now create a table and verify it was created:
CREATE TABLE employees ( id INT PRIMARY KEY, name VARCHAR(50), department VARCHAR(50), salary INT, status VARCHAR(20) ); SHOW TABLES; DESCRIBE employees;
3 Insert Sample Data
INSERT INTO employees (id, name, department, salary, status) VALUES (1, 'Sokha', 'Sales', 2800, 'Active'), (2, 'Maria', 'Marketing', 3200, 'Active'), (3, 'Wei', 'Sales', 3000, 'Inactive'), (4, 'Aiden', 'IT', 3500, 'Active'), (5, 'Nadia', 'HR', 2600, 'Active'), (6, 'Liam', 'Marketing', 2100, 'Inactive');
4 Filter with AND
Both conditions must be true — Sales and active.
SELECT name, department, status FROM employees WHERE department = 'Sales' AND status = 'Active';
5 Filter with OR
Either condition can be true — Sales or Marketing.
SELECT name, department FROM employees WHERE department = 'Sales' OR department = 'Marketing';
6 AND with a Numeric Condition
Combine a text condition with a numeric one.
SELECT name, salary FROM employees WHERE status = 'Active' AND salary > 2700;
7 Mixing AND and OR — the Right Way
Active employees who are in either Sales or Marketing. Without the parentheses, this query would return every active Sales employee, plus every Marketing employee regardless of status — not what we want.
SELECT name, department, status FROM employees WHERE status = 'Active' AND (department = 'Sales' OR department = 'Marketing');
8 Combine with Column Selection and an Alias
Bring in what you learned in Module 2.
SELECT name AS employee_name, salary AS monthly_pay FROM employees WHERE department = 'Sales' AND salary >= 2800;
9 Combine with Sorting
Filter first with AND/OR, then sort what's left.
SELECT name, department, salary FROM employees WHERE status = 'Active' AND (department = 'Sales' OR department = 'IT') ORDER BY salary DESC;
Troubleshooting
Common Problems and Solutions
Problem 1: Mixing AND and OR returns unexpected extra rows
You wrote WHERE status = 'Active' AND department = 'Sales' OR department = 'Marketing' and got inactive Marketing employees too.
AND before OR, so this reads as "(Active AND Sales) OR Marketing" — not what you meant. Add parentheses: WHERE status = 'Active' AND (department = 'Sales' OR department = 'Marketing').Problem 2: Trying to match multiple values with one column and AND
You wrote WHERE department = 'Sales' AND department = 'HR' and got zero rows.
OR instead — WHERE department = 'Sales' OR department = 'HR' — or the IN operator, covered in a future module.Problem 3: An alias from SELECT doesn't work inside WHERE
You aliased salary AS monthly_pay and then tried WHERE monthly_pay > 3000 AND department = 'IT', and got an "unknown column" error.
WHERE can't see aliases created in the same SELECT. Use the real column name, salary, inside WHERE.Problem 4: Too many AND conditions return zero rows
You stacked five AND conditions and nothing matched, even though you expected some results.
AND you add narrows the result further — it's easy to over-filter by accident. Comment out conditions one at a time to find which one is excluding everything, or double-check for typos in column values.Problem 5: A row you expected is silently missing
A row where department is NULL doesn't show up in either department = 'Sales' OR department = 'HR' or its opposite.
NULL never satisfies =, !=, AND, or OR comparisons directly. If you need to include those rows, add OR department IS NULL explicitly.Conclusion
What You Should Take Away
You should now be comfortable combining multiple conditions in a single query using AND to narrow results and OR to widen them — and, most importantly, you know to reach for parentheses the moment you mix the two together.
In Module 7, we'll build on this with shortcuts like IN and BETWEEN that make long chains of OR and comparison conditions much easier to write and read.
Homework
Homework & Quiz
📝 Homework Task
Using the employees table from this lesson, write your own query that:
- Selects
name,department, andsalary. - Finds employees who are Active AND earn 2600 or more.
- Sorts the results by salary, highest first.
Try it yourself first — then check it against the live playground below.
🧪 Quiz: Test Your Understanding (10 Questions)
Pick one answer per question, then click Submit to see your score instantly.
1. With AND, how many conditions must be true for a row to match?
2. With OR, how many conditions must be true for a row to match?
3. Which operator does MySQL evaluate first when AND and OR appear together?
4. What fixes unexpected results when mixing AND and OR?
5. WHERE department = 'Sales' AND department = 'HR' returns:
6. Which is the correct way to match Sales or HR employees?
7. Adding more AND conditions to a query tends to:
8. Does a row with a NULL department match "department = 'Sales' OR department = 'HR'"?
9. Can WHERE reference an alias created in the same SELECT statement?
10. Which clause would you add to sort filtered results by salary, highest first?
Try It Live
🧪 Live SQL Playground
A real SQLite database running entirely in your browser (via WebAssembly) — nothing is sent to a server. Practice combining AND/OR on the employees table below, or pick an example query to get started.
name TEXT
department TEXT
salary INTEGER
status TEXT
Related Lessons
Continue the Series
FAQ
Frequently Asked Questions
1. What's the core difference between AND and OR?
AND requires every listed condition to be true for a row to appear. OR only requires at least one of them to be true.
2. Why do AND and OR give different results when mixed without parentheses?
Because MySQL evaluates AND before OR by default, so an unparenthesized mix can group conditions differently than you intended. Parentheses make the grouping explicit and predictable.
3. Can I combine more than two conditions?
Yes — you can chain as many AND and OR conditions as you need, as long as you group them clearly with parentheses when mixing the two.
4. Is there a shortcut for many OR conditions on the same column?
Yes — the IN operator, for example WHERE department IN ('Sales', 'HR', 'IT'), replaces a long chain of OR conditions on the same column with something shorter and easier to read.
5. Does the order of AND/OR conditions affect the result?
No, as long as the grouping (parentheses) stays the same, reordering the individual conditions doesn't change which rows match.
6. Can I use AND/OR with numeric comparisons too?
Yes — you can freely mix equality, numeric comparisons like > and <, and text conditions within the same AND/OR expression.
7. What happens if none of my conditions match any rows?
MySQL simply returns an empty result set — no error, just zero rows. This is completely normal.
8. Does NULL work normally inside AND/OR conditions?
No — NULL represents an unknown value, so it can never satisfy a direct comparison inside AND or OR. You need an explicit IS NULL check to include those rows.
9. Can I use WHERE with a column that isn't in my SELECT list?
Yes. WHERE can reference any real column in the table, whether or not you're displaying it in your results.
10. Is there a NOT operator to reverse a condition?
Yes — NOT flips a condition's result, for example WHERE NOT department = 'Sales' matches every department except Sales. It combines with AND and OR the same way.