#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
SQL for Testers

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:

  1. The expression rebuilds the total from raw columns.
  2. ABS() takes the size of the difference, ignoring its sign.
  3. The tolerance of 0.01 allows for normal rounding to two decimals.
  4. 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

SituationMySQLSQL Server
7 / 2 with integers3.5000 (decimal)3 (integer division)
Integer division keyword7 DIV 2 gives 3Plain / on integers
Divide by zeroReturns NULL by defaultRaises an error
Any value + NULLNULLNULL

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 COALESCE where 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.

Explore Course