MySQL operators let beginners calculate values with arithmetic operators, compare values with comparison operators, and filter rows with logical operators in SELECT and WHERE clauses. Used poorly, they return the wrong rows or the wrong totals. This guide uses one sample table and one repeated dataset, then walks through arithmetic, comparison, and logical examples side by side.
What are MySQL operators?
MySQL operators are symbols or keywords that act on values inside SQL statements such as SELECT and WHERE. They let you calculate new numbers, compare values, and combine conditions so a query returns the rows you want instead of every row in the table.
Operator families at a glance
Beginners usually need three families first: arithmetic operators for math, comparison operators for checking values, and logical operators for combining conditions. MySQL also includes bitwise operators like &, |, and ^, plus compound operators listed by some references, but those are not the main tools for beginner filtering.
| Operator family | What it does | Common place to use it |
|---|---|---|
| Arithmetic | Calculates values | SELECT |
| Comparison | Checks whether values match a rule | WHERE |
| Logical | Combines conditions | WHERE |
Where operators appear in SQL
In practice, operators show up most often in SELECT and WHERE. SELECT uses expressions to calculate output columns. WHERE uses comparisons and logical operators to filter rows. That split matters because the same symbol can do a different job depending on where the database reads it.
Arithmetic operators in SELECT
Arithmetic operators let you calculate with numbers in a SELECT statement. The core set is addition, subtraction, multiplication, division, and modulus: +, -, *, /, and %. Use them for computed columns such as totals, discounts, or unit conversions.
Using expressions to calculate new values
Picture a products table with a unit price and a quantity. A SELECT expression can multiply those columns to produce a line total. Another expression can subtract a discount from a price or show the remainder from a quantity split.
| id | product_name | category | price | stock | status |
|---|---|---|---|---|---|
| 1 | Notebook | office | 8 | 25 | active |
| 2 | Pen | office | 3 | 0 | active |
| 3 | Desk Lamp | home | 45 | 12 | active |
| 4 | Chair | home | 120 | 5 | discontinued |
| 5 | Mouse Pad | office | 6 | 30 | active |
- Multiply values. Use * when two numeric columns should produce a total. For example, price * stock calculates inventory value for each product.
- Subtract values. Use - when you need a difference, such as price - 2 for a simple markdown or stock - 1 for a one-unit change.
- Divide values. Use / for ratios or averages. Division can return decimals, so the output may be more precise than the source values.
- Use modulus. Use % when you need the remainder after division. That is handy for checking whether a number is even or for repeating patterns.
SELECT example: SELECT product_name, price * stock AS inventory_value FROM products; On the sample data, Notebook returns 200, Pen returns 0, Desk Lamp returns 540, Chair returns 600, and Mouse Pad returns 180. The math changes the output without changing the rows.
How do I use the =, <, >, <=, and >= operators in MySQL?
Use comparison operators in WHERE when you want MySQL to keep or reject rows based on a rule. = checks for equality, > for greater than, < for less than, >= for greater than or equal to, and <= for less than or equal to. MySQL comparison operators also include <> for not equal.
Comparison operators in WHERE
These operators decide which rows survive the filter. A price threshold, a stock threshold, or a status check all start here. The operator you choose changes the result set immediately, even when the table data stays the same.
| Operator | Meaning | Example condition | Rows kept from the sample table |
|---|---|---|---|
| = | equal to | status = 'active' | 1, 2, 3, 5 |
| > | greater than | price > 20 | 3, 4 |
| < | less than | stock < 10 | 2, 4 |
| >= | greater than or equal to | stock >= 12 | 1, 3, 5 |
| <= | less than or equal to | price <= 8 | 1, 2, 5 |
| <> | not equal to | category <> 'home' | 1, 2, 5 |
Suppose you want low-cost items, but you also want to exclude home products. WHERE price <= 8 AND category <> 'home' keeps Notebook, Pen, and Mouse Pad. The comparison operators do the filtering, and the logical operator combines the two rules.
The difference between = and <> is simple but easy to mix up. = keeps rows that match a value. <> keeps rows that do not match it. If you ask for status <> 'active', the sample table returns only Chair.
How do AND, OR, and NOT work?
Logical operators combine conditions in WHERE clauses. AND means both conditions must be true, OR means either condition can be true, and NOT flips a condition. These operators control how several comparisons work together in one filter.
Combining conditions
AND is stricter than OR. A query with AND returns fewer rows because every condition must pass. A query with OR returns more rows because any listed condition can pass. That difference is one of the first places beginners get unexpected results.
A common first-timer mistake is writing a filter like WHERE status = 'active' OR price <= 8 when the real goal is active items priced at 8 or less. The OR version returns too many rows because any active product qualifies, even if the price is high. The AND version is narrower.
- Use AND for shared limits. Write AND when both conditions must be true at the same time, such as price > 5 AND stock > 10. This pattern fits tight filters.
- Use OR for alternatives. Write OR when one condition or the other should qualify, such as category = 'office' OR status = 'discontinued'. This pattern fits broader searches.
- Use NOT to invert. Write NOT before a condition when you want everything except the matched rows, such as NOT status = 'discontinued'. It changes a yes rule into a no rule.
Logical operators are especially useful for real-world filtering patterns, such as price thresholds combined with status checks. Example: WHERE price <= 45 AND status = 'active' returns Notebook, Pen, Desk Lamp, and Mouse Pad. That is a simple pattern, but it already matches the kind of query beginners write most often.
Why does operator precedence matter?
Operator precedence matters because MySQL evaluates some parts of a condition before others. Comparison happens before logical evaluation, and parentheses can override the default order. If you skip parentheses, the query may return a different set of rows than you expect.
Comparison before logical evaluation
In a condition like price <= 8 OR stock >= 20 AND status = 'active', MySQL evaluates the comparisons first, then applies AND before OR. That means the result is not the same as reading left to right. Webcoderspeed points to operator precedence as a source of subtle bugs, and this is the kind of mistake it warns about.
Say your filter should return only active products that are either cheap or heavily stocked. If you write WHERE status = 'active' AND price <= 8 OR stock >= 20, the stock condition can pull in rows you did not mean to keep. The parentheses version, WHERE status = 'active' AND (price <= 8 OR stock >= 20), gives the intended result.
Using parentheses to control the result
Parentheses are the clearest way to make intent obvious. They help with both readability and correctness. When a filter has more than one logical operator, parentheses should be the default choice unless the expression is very short and obvious.
| Query | Result on the sample table | Why |
|---|---|---|
| WHERE status = 'active' AND price <= 8 OR stock >= 20 | 1, 2, 5 | AND is applied before OR, so stock alone can qualify row 1 and row 5. |
| WHERE status = 'active' AND (price <= 8 OR stock >= 20) | 1, 2, 5 | Parentheses keep the price-or-stock test inside the active-item check. |
| WHERE (status = 'active' AND price <= 8) OR stock >= 20 | 1, 2, 5 | The OR still allows any row with stock at least 20, which is why row 1 and row 5 remain. |
MySQL operators workbook
This workbook repeats the same products table so each operator family is easier to compare. The point is not to memorize query text. The point is to see how arithmetic changes values, comparison changes row membership, and logical operators combine those comparisons into a final result set.
Mysql operators table
Use the table below as the shared dataset for every query in this section. The values stay fixed so the output stays checkable.
| id | product_name | category | price | stock | status |
|---|---|---|---|---|---|
| 1 | Notebook | office | 8 | 25 | active |
| 2 | Pen | office | 3 | 0 | active |
| 3 | Desk Lamp | home | 45 | 12 | active |
| 4 | Chair | home | 120 | 5 | discontinued |
| 5 | Mouse Pad | office | 6 | 30 | active |
12 annotated queries
- Calculate inventory value. SELECT product_name, price * stock AS inventory_value FROM products; This uses multiplication in SELECT, not WHERE. It returns a computed value for every row.
- Subtract a discount. SELECT product_name, price - 2 AS sale_price FROM products; This uses subtraction to show a smaller number for each product. The row set stays the same.
- Check for equality. SELECT product_name FROM products WHERE status = 'active'; This uses the equal-to operator and returns rows 1, 2, 3, and 5.
- Find expensive items. SELECT product_name FROM products WHERE price > 20; This uses greater than and returns Desk Lamp and Chair.
- Find cheaper items. SELECT product_name FROM products WHERE price < 10; This uses less than and returns Notebook, Pen, and Mouse Pad.
- Set a minimum stock rule. SELECT product_name FROM products WHERE stock >= 12; This uses greater than or equal to and returns Notebook, Desk Lamp, and Mouse Pad.
- Set a maximum price rule. SELECT product_name FROM products WHERE price <= 8; This uses less than or equal to and returns Notebook, Pen, and Mouse Pad.
- Exclude one category. SELECT product_name FROM products WHERE category <> 'home'; This uses not equal and returns Notebook, Pen, and Mouse Pad.
- Require two true conditions. SELECT product_name FROM products WHERE status = 'active' AND price <= 8; This uses AND and returns Notebook, Pen, and Mouse Pad.
- Allow either condition. SELECT product_name FROM products WHERE price <= 8 OR stock = 0; This uses OR and returns Notebook, Pen, and Mouse Pad. Pen qualifies because stock is zero.
- Flip a condition. SELECT product_name FROM products WHERE NOT status = 'discontinued'; This uses NOT and returns all rows except Chair.
- Group mixed logic. SELECT product_name FROM products WHERE status = 'active' AND (price <= 8 OR stock >= 20); This uses parentheses to control precedence and returns Notebook, Pen, and Mouse Pad.
Mysql operators cheat sheet
| Query number | Operator family | Rows returned | What changed |
|---|---|---|---|
| 1 | Arithmetic | All rows, with new totals | Calculated inventory value |
| 2 | Arithmetic | All rows, with new prices | Subtracted a fixed amount |
| 3 | Comparison | 1, 2, 3, 5 | Kept rows that matched a text value |
| 4 | Comparison | 3, 4 | Kept rows above a threshold |
| 5 | Comparison | 1, 2, 5 | Kept rows below a threshold |
| 6 | Comparison | 1, 3, 5 | Kept rows at or above a threshold |
| 7 | Comparison | 1, 2, 5 | Kept rows at or below a threshold |
| 8 | Comparison | 1, 2, 5 | Kept rows that did not match the category |
| 9 | Logical | 1, 2, 5 | Required both conditions |
| 10 | Logical | 1, 2, 5 | Allowed either condition |
| 11 | Logical | 1, 2, 3, 5 | Flipped the status test |
| 12 | Logical with precedence control | 1, 2, 5 | Used parentheses to keep the filter narrow |
Common beginner mistakes
- Using = when the query needs <>, or the reverse.
- Writing AND where OR was intended, then wondering why too few rows appear.
- Skipping parentheses in mixed conditions and trusting left-to-right reading instead of MySQL precedence.
- Expecting comparisons with missing values to behave like normal values. With NULL present, equality checks and inequality checks do not act like ordinary true or false tests.
NULL needs direct handling. In MySQL, a comparison with NULL does not behave like a normal comparison, so = and <> are not enough by themselves when a column may be empty. Beginner filters should account for that separately, because a row with NULL can be excluded even when the rest of the condition looks right.
Compact memory aid
Use this shortcut: calculate in SELECT, compare in WHERE, combine with AND/OR/NOT, group with parentheses. If the query needs a new number, reach for arithmetic. If it needs a yes or no check, reach for comparison. If it needs multiple checks, use logical operators and control the order with parentheses.
Frequently asked questions
What are the basic MySQL operators for beginners?
The main beginner operators are arithmetic operators, comparison operators, and logical operators. Arithmetic operators calculate values in SELECT. Comparison operators check whether a value matches a rule. Logical operators combine several rules in WHERE so a query can filter rows with more precision.
What is the difference between arithmetic, comparison, and logical operators in MySQL?
Arithmetic operators change numbers. Comparison operators decide whether a row matches a condition. Logical operators join conditions together. That difference matters because the same query may calculate a value in SELECT, then filter by that value in WHERE, using different operator families for each job.
How do I write arithmetic examples in MySQL using +, -, *, /, and %?
Use + to add, - to subtract, * to multiply, / to divide, and % to get a remainder. Place them in SELECT expressions, such as price * stock or price - 2, when you want a calculated output column.
How do AND, OR, and NOT work in MySQL?
AND keeps a row only if every linked condition is true. OR keeps a row if any linked condition is true. NOT reverses a condition. These operators are most useful in WHERE clauses, especially when a filter needs more than one rule.
What is the difference between = and <> in MySQL?
= keeps rows that match a value exactly. <> keeps rows that do not match that value. A query like status = 'active' finds active rows, while status <> 'active' finds non-active rows. That is a common beginner distinction.
Can I use MySQL operators with WHERE and LIKE?
Yes. WHERE is the main place to use comparison and logical operators, and LIKE is often paired with them in the same filter. A query can combine pattern matching with other rules, such as status checks or price thresholds, to narrow the result set further.
Which MySQL operators are used most often in beginner queries?
The most common beginner operators are =, >, <, >=, <=, <>, AND, OR, and the arithmetic set +, -, *, /, and %. Most beginner queries use them in SELECT and WHERE long before they need more specialized operators.