CHAPTER 09
Intermediate
SQL Operators and Conditional Statements
Updated: May 16, 2026
6 min read
# CHAPTER 9
SQL Operators and Conditional Statements
1. Introduction
Business rules are rarely simple. An HR system does not just search for "managers". It searches for "managers in the IT department who were hired between 2020 and 2022, but exclude those who are currently on leave." To process this level of complex logic, PostgreSQL relies on logical operators to chain multiple conditions together. In this chapter, we will learn how to write multi-layered boolean queries that filter data with surgical precision.2. Learning Objectives
By the end of this chapter, you will be able to:-
Combine multiple conditions using
ANDandOR.
-
Exclude data using the
NOToperator.
-
Filter data against arrays using the
INoperator.
-
Search within ranges using the
BETWEENoperator.
-
Identify missing data using
IS NULL.
-
Write if/then conditional logic using the
CASEstatement.
3. The AND and OR Operators
When you have multiple conditions in your WHERE clause, you chain them together.
-
AND: Both conditions MUST be true.
-
OR: Only ONE condition has to be true.
sql
4. The IN Operator
If you need to find employees in IT, HR, Sales, and Marketing, writing OR department = '...' four times is exhausting. The IN operator checks if a value exists inside a provided list.
sql
5. The BETWEEN Operator
Used for ranges, particularly numbers and dates. BETWEEN is inclusive, meaning the start and end values are included in the results.
sql
6. Handling the Unknown: IS NULL
In database theory, NULL does not mean "Zero" or "Empty String". NULL means Unknown/Missing.
Because it is unknown, you cannot use math on it! WHERE phone_number = NULL will fail. You must use IS NULL or IS NOT NULL.
sql
7. Conditional Logic: The CASE Statement
What if you want to categorize data directly inside your query? The CASE statement is the SQL equivalent of an if/else block in programming.
sql
*Output: The database creates a brand new, temporary column named "experience_level" and injects the correct label for each row!*
8. Mini Project: Complex Access Control
An admin wants a list of active users who are either from New York or California, registered over a year ago, but explicitly exclude any banned accounts.
sql
9. Common Mistakes
-
Parentheses in Logic: When mixing
ANDandOR, you must use parentheses to group your logic, just like in algebra.
WHERE dept = 'IT' OR dept = 'HR' AND salary > 50000 (This means: ANYONE in IT, plus HR people over 50k).
*Right:* WHERE (dept = 'IT' OR dept = 'HR') AND salary > 50000 (This means: HR and IT people, but both must be over 50k).
10. Best Practices
-
Prefer
INoverOR: Whenever you find yourself writingcolumn = X OR column = Y, replace it withcolumn IN (X, Y). It is significantly easier to read and often optimized better by the PostgreSQL query planner.
11. Exercises
-
1.
Write a query to find all products that are NOT in the 'Electronics' or 'Clothing' categories. (Hint: combine
NOTwithIN).
-
2.
Write a query to find all employees whose
terminationdateis unknown.
12. SQL Challenges
Write aCASE statement query that selects product names and creates a new column called stockstatus. If quantity is 0, it should say 'Out of Stock'. If quantity is between 1 and 10, it should say 'Low Stock'. Otherwise, 'In Stock'.
sql
13. MCQ Quiz with Answers
Question 1
Why must you use IS NULL instead of = NULL when searching for missing data?
Question 2
In a CASE statement, what happens if none of the WHEN conditions are met and no ELSE clause is provided?
14. Interview Questions
-
Q: Explain the mathematical precedence of
ANDversusORin aWHEREclause, and why parentheses are critical when mixing them.
-
Q: Describe how a
CASEstatement functions and provide an example of a business reporting scenario where it would be utilized.
15. FAQs
Q: Can I useBETWEEN for letters/strings?
A: Yes! WHERE name BETWEEN 'A' AND 'C' will return names starting with A and B. However, be careful, as it stops exactly at 'C', meaning it will not include 'Charlie'.
16. Summary
By mastering logical operators, you have upgraded your SQL from simple data fetching to complex algorithmic filtering. You can now build highly specific segments of data, handle missing values gracefully, and inject dynamic conditional logic directly into your outputs usingCASE.