Logical Operators
Logical operators let a WHERE clause combine more than one condition into a single, more precise filter. AND requires every condition to be true: WHERE age > 15 AND name = "Ali"; returns rows where both conditions hold. If even one is false for a given row, that row is excluded. OR requires at least one condition to be true: WHERE age > 16 OR name = "Ali"; returns rows matching either condition, even if only one of them holds. NOT reverses a condition: WHERE NOT age = 16; is the same as WHERE age != 16;. Parentheses matter when mixing AND and OR, exactly like in math: WHERE (age > 15 AND age < 18) OR name = "Ali"; groups the age range as one unit before combining it with the name condition, controlling exactly how the conditions relate to each other.
Why this matters
- ·AND and OR let a single query express genuinely complex, multi-part conditions.
- ·NOT lets you express an exclusion cleanly, instead of listing every value you don't want.
- ·Parentheses give you full control over how multiple conditions combine, just like in math.
Common mistakes
- ·Mixing AND and OR without parentheses can group conditions differently than you intended.
- ·Overusing OR without care can accidentally widen a filter to match far more rows than expected.
- ·A long, unparenthesized chain of conditions becomes hard for anyone (including future you) to read correctly.
+----+------+-----+ | id | name | age | +----+------+-----+ | 2 | Sara | 17 | +----+------+-----+ 1 row in set
PHP runs on a server, not in the browser this shows the expected output instead of a live one.
Frequently asked questions
AND requires every condition to be true for a row to match; OR requires only at least one of them to be true.
Practice questions
Question 1
Write a query that returns students where age is greater than 15 OR name is "Ali".
PHP runs on a server, not in the browser this shows the expected output instead of a live one.
Question 2
Write a query that returns students where age is not 17, using NOT.
PHP runs on a server, not in the browser this shows the expected output instead of a live one.
Quick check
Just for this topic answer honestly, then check yourself.
1 / 3
