MySQL Operators

MySQL operators let you compare values and combine conditions so your queries can filter, match, and evaluate data with precision.

What Are Operators in MySQL?

An operator is a symbol or keyword that tells MySQL to perform a specific operation on one or more values, called operands. Operators are the backbone of the WHERE clause — without them, you could only ever retrieve entire tables. MySQL groups operators into several families: arithmetic, comparison, logical, and bitwise. This lesson focuses on the two families you will use constantly: comparison operators and logical operators.

Comparison Operators

Comparison operators evaluate two values and return a boolean-like result: 1 (true), 0 (false), or NULL (unknown, when either operand is NULL). They are almost always used inside a WHERE or HAVING clause.

OperatorMeaningExample
=Equal toprice = 100
<> or !=Not equal tostatus <> 'archived'
>Greater thanage > 18
<Less thanstock < 10
>=Greater than or equal toscore >= 60
<=Less than or equal toscore <= 100
BETWEENWithin an inclusive rangeprice BETWEEN 10 AND 50
INMatches any value in a listcountry IN ('US','UK')
IS NULLValue is NULLmanager_id IS NULL
Note: MySQL's <> and != are exact synonyms for 'not equal to'. Most style guides prefer != for readability, but both compile to identical execution plans.

Logical Operators

Logical operators combine multiple boolean expressions into a single condition. MySQL supports AND, OR, NOT, and XOR. Understanding operator precedence matters here: NOT binds tighter than AND, which binds tighter than OR. When mixing AND and OR in the same clause, always use parentheses to make your intent explicit — relying on default precedence is a common source of subtle bugs.

  • AND — true only when both sides are true
  • OR — true when at least one side is true
  • NOT — inverts a boolean expression
  • XOR — true when exactly one side is true, but not both

Comparison operators in a WHERE clause

SELECT product_name, price
FROM products
WHERE price BETWEEN 20 AND 80
ORDER BY price;

Combining AND and OR with parentheses

SELECT customer_name, country, total_spent
FROM customers
WHERE (country = 'US' OR country = 'CA')
  AND total_spent > 500;

Filtering with IN and IS NOT NULL

SELECT employee_name, department
FROM employees
WHERE department IN ('Sales', 'Marketing')
  AND manager_id IS NOT NULL;
Note: Never compare a value to NULL using = or <>. A condition like manager_id = NULL always evaluates to NULL (not true), so matching rows are silently excluded. Use IS NULL or IS NOT NULL instead.

A practical rule of thumb: reach for comparison operators when evaluating a single column against a value or range, and reach for logical operators the moment you need to describe a condition in terms of 'and', 'or', or 'not'. Combining both families fluently is what separates a basic SELECT from a genuinely useful report query.

Exercise: MySQL Operators

Which operator tests if a value lies within an inclusive range?