Oracle AND & OR Operators πΒΆ
Prerequisites: WHERE Clause
Mentor's Note: Logical operators allow you to build complex rules. Think of
ANDas a strict gatekeeper (must meet all rules) andORas a flexible one (any rule will do). π‘
π The Scenario: The Recruitment Search πΒΆ
You are looking for a specific candidate. - AND: "I need someone who lives in Surat AND has 5 years of experience." (Very strict). - OR: "I need someone who knows Java OR Python." (More options).
π» 1. The AND OperatorΒΆ
Returns TRUE only if all conditions separated by AND are TRUE.
π» 2. The OR OperatorΒΆ
Returns TRUE if any of the conditions separated by OR is TRUE.
SELECT first_name, last_name, hire_date
FROM employees
WHERE department_id = 10
OR department_id = 20;
ποΈ Architect's Note: Precedence & Parentheses π‘οΈΒΆ
Oracle processes AND before OR. If you mix them without parentheses, your logic might fail!
- Rule: Always use ( ) to group your logic. It makes the "Execution Plan" clear to both you and the Oracle Optimizer.
-- β Dangerous: Might return IT_PROG staff OR anyone with high salary
WHERE job_id = 'IT_PROG' OR job_id = 'HR_REP' AND salary > 5000;
-- β
Secure: Logic is grouped clearly
WHERE (job_id = 'IT_PROG' OR job_id = 'HR_REP') AND salary > 5000;