Arithmetic Operator
What Are Arithmetic Operators?
SQL supports standard arithmetic operators for performing calculations directly within a query: + (addition), - (subtraction), * (multiplication), and / (division).
Example
SELECT salary, salary * 1.10 AS revised_salary
FROM employees;
Why Testers Use Them
Arithmetic operators let a tester independently recompute expected values — like a total, discount, or tax amount — directly in a query and compare it against what the application displays or stores.
Operators Beyond the Basic Four
Most databases also support a remainder operator. MySQL and SQL Server accept %, and MySQL, Oracle and PostgreSQL offer the MOD() function. Testers use it for checks like "every invoice number must be even" or to sample every tenth row.
SELECT order_id
FROM orders
WHERE MOD(order_id, 10) = 0; -- every 10th order for a spot check
Precedence follows normal maths: multiplication, division and remainder happen before addition and subtraction. Use brackets whenever a formula mixes them, so the intention is obvious to the next reader.
SELECT price + tax * qty AS wrong_total,
(price + tax) * qty AS right_total
FROM order_lines;
Recomputing an Expected Value: A Worked Example
Suppose the application stores total for each order line, and the rule is: total = quantity × unit price − discount. A tester can find every row where the stored value disagrees with the rule:
SELECT line_id,
quantity,
unit_price,
discount,
total AS stored_total,
quantity * unit_price - discount AS expected_total
FROM order_lines
WHERE ABS(total - (quantity * unit_price - discount)) > 0.01;
How it works step by step:
- The expression rebuilds the total from raw columns.
ABS()takes the size of the difference, ignoring its sign.- The tolerance of 0.01 allows for normal rounding to two decimals.
- Any row returned is a candidate defect, with both values visible for the bug report.
An empty result means every stored total matches the business rule.
Database Differences That Trip Testers Up
| Situation | MySQL | SQL Server |
|---|---|---|
| 7 / 2 with integers | 3.5000 (decimal) | 3 (integer division) |
| Integer division keyword | 7 DIV 2 gives 3 | Plain / on integers |
| Divide by zero | Returns NULL by default | Raises an error |
| Any value + NULL | NULL | NULL |
The NULL rule matters a lot. If discount is NULL for some rows, the whole expression becomes NULL and those rows quietly disappear from the comparison above. Wrap nullable columns with COALESCE(discount, 0) when NULL should mean zero.
To avoid divide-by-zero errors when calculating percentages, use NULLIF:
SELECT region,
returns * 100.0 / NULLIF(orders, 0) AS return_rate_pct
FROM monthly_stats;
Multiplying by 100.0 instead of 100 also forces decimal maths in SQL Server.
Tester's Checklist for Calculation Checks
- Write the business rule in plain words before turning it into SQL.
- Add brackets so precedence is explicit.
- Handle NULLs with
COALESCEwhere the rule allows it. - Guard divisions with
NULLIF. - Compare with a tolerance instead of exact equality for decimal money values.
- Run the query on a known small data set first, so you trust it before running it on everything.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.