CHAPTER 09
Beginner
SQL Operators and Conditions
Updated: May 16, 2026
5 min read
# CHAPTER 9
SQL Operators and Conditions
1. Introduction
Business rules are rarely simple. An HR manager doesn't ask for "all employees in Sales." They ask for "all employees in Sales OR Marketing, who were hired BETWEEN 2020 and 2023, AND who are NOT currently managers." To translate these complex, multi-layered human requests into database logic, we must master Boolean Operators and Conditional SQL clauses. In this chapter, we will learn how to chain conditions together to create laser-focused queries.2. Learning Objectives
By the end of this chapter, you will be able to:-
Combine multiple
WHEREconditions usingANDandOR.
-
Use the
INoperator to replace massiveORchains.
-
Select ranges of data using
BETWEEN.
-
Reverse logic using the
NOToperator.
-
Safely query empty data using
IS NULL.
3. The AND / OR Operators
These operators allow you to string multiple conditions together.
-
AND: Every single condition must be True.
-
OR: Only one of the conditions needs to be True.
sql
4. The IN Operator (The OR Shortcut)
If you want to find employees in Sales, HR, IT, and Exec, typing OR department = '...' four times is tedious and prone to typos. The IN operator allows you to check against a list of values instantly.
sql
5. The BETWEEN Operator
When searching for dates or numerical ranges, you could write: WHERE salary >= 50000 AND salary <= 100000. However, the BETWEEN operator makes this much more readable.
sql
6. The NOT Operator
Sometimes it is easier to tell MySQL what you *don't* want. The NOT operator reverses the logic of any condition.
sql
7. The Danger of NULL
In database theory, NULL does not mean "Zero" or "Blank text". NULL means Unknown or Missing.
Because NULL is a concept of nothingness, you cannot do math on it.
If John's salary is NULL, and you query WHERE salary = NULL, MySQL will return zero results. You cannot say "Does $50,000 equal Unknown?"
To find missing data, you must use the special IS NULL or IS NOT NULL operators!
sql
8. Mini Project: Complex HR Query
The CEO wants a list of all non-management employees who were hired in 2022 and work in either IT or Marketing. We must chain everything together!
sql
9. Common Mistakes
-
Order of Operations (PEMDAS): Just like in math,
ANDtakes precedence overOR. If you mix them without using parentheses, the query will return chaotic results.
WHERE dept = 'IT' OR dept = 'HR' AND salary > 50000 (This finds all IT people regardless of salary, plus HR people making over 50k).
*Correct:* WHERE (dept = 'IT' OR dept = 'HR') AND salary > 50000 (This correctly applies the salary rule to both departments!).
10. Best Practices
-
Always use
INoverORfor exact matches: It is cleaner to read, and modern MySQL engines are highly optimized to process anINlist much faster than a long chain ofORconditions.
11. Exercises
-
1.
Write a query to find all products that are
in_stock = 1and have a price between $10 and $50.
-
2.
Why does the query
SELECT * FROM users WHERE phone_number = NULLfail to return users with missing phone numbers?
12. MCQ Quiz with Answers
Question 1
To ensure that the OR logic is evaluated before the AND logic in a complex WHERE clause, what must you use?
Question 2
Which operator should be used as a cleaner, faster alternative to stringing together multiple OR statements checking the exact same column?
13. Interview Questions
-
Q: Explain the mathematical concept of
NULLin SQL. Why must you useIS NULLinstead of the=operator?
-
Q: Describe how you would troubleshoot a query containing both
ANDandORoperators that is returning more rows than expected.
14. FAQs
Q: Can I useBETWEEN with letters instead of numbers?
A: Yes! WHERE last_name BETWEEN 'A' AND 'M' will return everyone whose last name starts with the first half of the alphabet!
15. Summary
By mastering operators, you transition from asking simple questions to conducting deep, multi-variable investigations. You can safely navigate missing data withIS NULL, condense logic with IN and BETWEEN, and strictly control evaluation order using parentheses.