SQL AND, OR, NOT and XOR Conditions

Use AND, OR and NOT inside a WHERE clause to combine or reverse conditions. MySQL also supports XOR when exactly one of two conditions should be true.

SELECT id, name, class, mark
FROM student
WHERE class = 'Four'
  AND mark > 70;

This returns only rows where both conditions are true.

OperatorResult
ANDAll combined conditions must be true.
ORAt least one combined condition must be true.
NOTNegates a condition.
XORMySQL: true when exactly one of two conditions is true.
Parentheses matter. In MySQL, AND is evaluated before OR. Add parentheses whenever the intended grouping should be obvious.

SQL AND Operator Top ↑

AND requires every combined condition to be true.

SELECT id, name, class, mark
FROM student
WHERE class = 'Four'
  AND mark > 70;

The result contains class Four students only when their mark is also greater than 70.

More than two conditions can be combined:

SELECT id, name, class, mark, gender
FROM student
WHERE class = 'Four'
  AND mark >= 60
  AND gender = 'female';

SQL OR Operator Top ↑

OR keeps a row when at least one condition is true.

SELECT id, name, class, mark
FROM student
WHERE class = 'Five'
   OR mark > 90;

A row is returned if it belongs to class Five, has a mark above 90, or satisfies both conditions.

AND, OR and Parentheses Top ↑

Suppose the requirement is:

Return students from class Five or Six, but only when the mark is greater than 80.

This query does not express that requirement correctly:

SELECT id, name, class, mark
FROM student
WHERE class = 'Five'
   OR class = 'Six'
  AND mark > 80;

Because AND has higher precedence than OR, MySQL interprets it like this:

WHERE class = 'Five'
   OR (class = 'Six' AND mark > 80)

That means every class Five row can match regardless of its mark.

Use parentheses to group the class conditions first:

SELECT id, name, class, mark
FROM student
WHERE (class = 'Five' OR class = 'Six')
  AND mark > 80;
Even when you know operator precedence, parentheses make mixed AND/OR conditions easier to review and maintain.

OR vs IN for Several Values of the Same Column Top ↑

When several OR conditions compare the same column for equality, IN is usually clearer.

Instead of:

SELECT id, name, class
FROM student
WHERE class = 'Four'
   OR class = 'Five'
   OR class = 'Six';

write:

SELECT id, name, class
FROM student
WHERE class IN (
    'Four',
    'Five',
    'Six'
);

Both express the same equality logic here; IN is easier to extend when the list grows.

Different Rules for Different Groups Top ↑

Parenthesized groups are useful when different classes have different pass marks.

SELECT id, name, class, mark
FROM student
WHERE (class = 'Five' AND mark > 75)
   OR (class = 'Six' AND mark > 80)
   OR (class = 'Seven' AND mark > 85);

Each parenthesized block represents one complete rule; OR then combines the rules.

SQL NOT Operator Top ↑

NOT reverses a condition.

SELECT id, name, class
FROM student
WHERE NOT class = 'Five';

For simple comparisons, the same intent may be clearer with a comparison operator:

SELECT id, name, class
FROM student
WHERE class <> 'Five';

See SQL comparison operators.

Exclude Several Values with NOT IN Top ↑

SELECT id, name, class
FROM student
WHERE class NOT IN (
    'Three',
    'Four',
    'Five',
    'Six',
    'Seven'
);
NULL caution: NOT IN can produce unexpected results when a compared value or a subquery result contains NULL because SQL uses three-valued logic. Check NULL handling explicitly when it is possible in your data.

For NULL values themselves, use IS NULL or IS NOT NULL, not equality comparisons with NULL.

Using NOT BETWEEN Top ↑

NOT can also be part of operators such as NOT BETWEEN.

SELECT id, name, mark
FROM student
WHERE mark NOT BETWEEN 50 AND 100;

BETWEEN is inclusive, so this returns rows outside the range 50 through 100.

MySQL XOR Operator Top ↑

MySQL supports XOR for exclusive OR. With ordinary true/false expressions, XOR is true when exactly one condition is true.

SELECT 1 XOR 0; -- 1
SELECT 0 XOR 1; -- 1
SELECT 1 XOR 1; -- 0
SELECT 0 XOR 0; -- 0

Example:

SELECT id, name, class, mark
FROM student
WHERE class = 'Five'
   XOR mark < 50;

This keeps rows where exactly one of those two conditions is true.

XOR is a MySQL feature and is less portable than AND, OR and NOT. For cross-database SQL, do not assume an XOR keyword is available.

AND / OR Conditions with PHP PDO Top ↑

When condition values come from an application, keep them as prepared-statement parameters.

<?php
require 'config.php';

$class1='Five';
$class2='Six';
$minimum_mark=80;

$sql="SELECT id,name,class,mark
      FROM student
      WHERE (class=:class1 OR class=:class2)
        AND mark>:minimum_mark
      ORDER BY id";

$stmt=$dbo->prepare($sql);

$stmt->bindValue(
    ':class1',
    $class1,
    PDO::PARAM_STR
);

$stmt->bindValue(
    ':class2',
    $class2,
    PDO::PARAM_STR
);

$stmt->bindValue(
    ':minimum_mark',
    $minimum_mark,
    PDO::PARAM_INT
);

$stmt->execute();

The parentheses belong to the SQL logic; prepared statements safely supply the data values.

DELETE with AND / OR Top ↑

Destructive query: preview the same WHERE condition with SELECT before running DELETE against important data.

Delete rows from either class Three or class Four:

DELETE FROM student
WHERE class = 'Three'
   OR class = 'Four';

For the same-column equality case, this is clearer:

DELETE FROM student
WHERE class IN (
    'Three',
    'Four'
);

Delete class Six or Four students only when the mark is below 80:

DELETE FROM student
WHERE (class = 'Six' OR class = 'Four')
  AND mark < 80;

See SQL DELETE for the full destructive-query workflow.

UPDATE with AND / OR Top ↑

Add five marks to class Three and Four students whose mark is above 80:

UPDATE student
SET mark = mark + 5
WHERE (class = 'Three' OR class = 'Four')
  AND mark > 80;

Again, because the class checks use equality on one column, IN can make the condition shorter:

UPDATE student
SET mark = mark + 5
WHERE class IN (
    'Three',
    'Four'
)
  AND mark > 80;

See SQL UPDATE for safe update practices.

AND / OR Compared with && / || Top ↑

MySQL has supported symbolic logical forms such as && for AND and, depending on SQL mode, || may be treated as logical OR or string concatenation.

For tutorials and portable SQL, prefer the keyword forms:

WHERE class = 'Four'
  AND mark > 70

and:

WHERE class = 'Five'
   OR class = 'Six'

The keyword forms are clearer and avoid SQL-mode ambiguity.

Common AND / OR / NOT Mistakes Top ↑

Mixing AND and OR without parentheses Top ↑

AND has higher precedence than OR in MySQL. Add parentheses to express the intended grouping explicitly.

Using many OR conditions for one column Top ↑

For repeated equality checks on the same column, IN is often shorter and easier to maintain.

Comparing NULL with = or <> Top ↑

Use IS NULL and IS NOT NULL for NULL checks.

Using NOT IN without considering NULL Top ↑

NULL values can make a NOT IN condition evaluate as unknown. Test NULL behavior explicitly, particularly when the values come from a subquery.

Assuming XOR is portable SQL Top ↑

MySQL supports XOR, but other database systems may not provide the same keyword.

Running DELETE or UPDATE before previewing the condition Top ↑

For important data, first run a SELECT using the same WHERE condition and verify the rows it matches.

SQL LIKE SQL IN SQL BETWEEN

SQL WHERE Comparison Operators SQL NULL

SQL DELETE SQL UPDATE Full Student Table with SQL Dump

Frequently Asked Questions Top ↑

Q1: What is the difference between AND and OR in SQL?

AND requires all combined conditions to be true. OR requires at least one of the combined conditions to be true.

Q2: Which is evaluated first, AND or OR?

In MySQL, AND has higher precedence than OR. Use parentheses when mixing them so the intended grouping is explicit.

Q3: When should I use IN instead of OR?

When several equality conditions compare the same column to different values, IN is usually clearer than repeating column=value with OR.

Q4: What does NOT do in SQL?

NOT negates a condition. It is also used in expressions such as NOT IN, NOT BETWEEN and NOT LIKE.

Q5: Why can NOT IN behave unexpectedly with NULL?

SQL uses three-valued logic. A NULL involved in a NOT IN comparison can make the result unknown rather than true, so NULL handling must be considered explicitly.

Q6: What does XOR mean in MySQL?

For ordinary true and false expressions, MySQL XOR is true when exactly one of the two conditions is true.

Q7: Should I use && and || instead of AND and OR?

Prefer AND and OR. They are clearer and more portable, while symbolic forms are MySQL-specific and || can be affected by SQL mode.



SQL LIKE SQL IN


Subscribe to our YouTube Channel here



plus2net.com




SQL Video Tutorials










We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer