Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to SQL Notes
Topic #204

SQL AND / OR Operators

AND requires every condition to be true. OR requires at least one to be true. Combining them without parentheses is the single biggest source of filtering bugs in SQL.

Syntax

WHERE condition1 AND condition2
WHERE condition1 OR condition2

Sample Table: employees

namedepartmentsalary
AmanEngineering65000
RiyaMarketing52000
KaranEngineering71000
NehaDesign48000

AND Example

SELECT name FROM employees
WHERE department = 'Engineering' AND salary > 60000;

Output: Aman, Karan — both conditions must hold.

OR Example

SELECT name FROM employees
WHERE department = 'Design' OR salary > 60000;

Output: Aman, Karan, Neha — any row matching either condition qualifies.

The Parentheses Trap

AND binds tighter than OR — mixing them without parentheses often doesn't do what you'd expect:

-- Ambiguous intent, relies on AND-before-OR precedence
SELECT name FROM employees
WHERE department = 'Engineering' AND salary > 60000 OR department = 'Design';

-- What it ACTUALLY means:
WHERE (department = 'Engineering' AND salary > 60000) OR department = 'Design';

If the intent was "Engineering employees who either earn over 60,000 OR are in Design," that query is wrong — every Design employee gets included regardless of salary, because OR is evaluated at the top level. Always use explicit parentheses when mixing AND and OR:

WHERE department = 'Engineering' AND (salary > 60000 OR department = 'Design');

Common Mistakes

  • Mixing AND/OR without parentheses and getting silently wrong results (no error — just the wrong rows)
  • Writing WHERE department = 'Engineering' OR 'Design' — invalid; each side of OR needs its own full condition: department = 'Engineering' OR department = 'Design' (or use IN)

Interview Relevance

Interviewers often deliberately give you a filter with three or more conditions to see if you reach for parentheses instead of guessing at operator precedence.

Practice Question

Write a query for employees who are either in Engineering with a salary above 60,000, or in Marketing with a salary above 50,000 — using explicit parentheses.

Related SQL Notes

Want to go beyond the notes?

Join CodingNow's SQL course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →