← Back to Chapters

MySQL Arithmetic Functions

? MySQL Arithmetic Functions

? Quick Overview

MySQL arithmetic functions allow you to perform mathematical calculations directly inside SQL queries. They are commonly used to compute totals, differences, ratios, rounding values, and numeric transformations.

? Key Concepts

  • Arithmetic operators work directly on numeric values
  • Functions return calculated numeric results
  • They are mostly used inside SELECT statements

? Syntax & Theory

MySQL supports both operators and built-in functions to perform arithmetic operations such as addition, subtraction, multiplication, division, rounding, and square root calculations.

? Code Examples

➕ Addition

? View Code Example
// Adds two numbers and returns their sum
SELECT 10 + 5 AS sum;

➖ Subtraction

? View Code Example
// Subtracts one value from another
SELECT 10 - 5 AS difference;

✖️ Multiplication

? View Code Example
// Multiplies two numeric values
SELECT 10 * 5 AS product;

➗ Division

? View Code Example
// Divides one number by another
SELECT 10 / 5 AS quotient;

? Modulo

? View Code Example
// Returns the remainder of a division
SELECT 10 % 3 AS remainder;

? Absolute Value

? View Code Example
// Converts negative values into positive values
SELECT ABS(-10) AS absolute_value;

? Square Root

? View Code Example
// Calculates the square root of a number
SELECT SQRT(16) AS square_root;

? Rounding

? View Code Example
// Rounds a number to two decimal places
SELECT ROUND(10.567, 2) AS rounded_number;

? Live Output / Explanation

Each query returns a calculated value based on the operation applied. These results can be used directly in reports, calculations, or further SQL expressions.

? Interactive Concept

Try combining multiple arithmetic functions together:

? View Code Example
// Combines ABS, division, and ROUND together
SELECT ROUND(ABS(-12.345) / 2, 1) AS final_result;

? Use Cases

  • Calculating totals and averages
  • Generating financial reports
  • Handling numeric transformations
  • Normalizing values

? Tips & Best Practices

  • Always handle division by zero carefully
  • Use ROUND() to control decimal precision
  • Combine functions for advanced calculations

? Try It Yourself

  • Create queries using CEIL() and FLOOR()
  • Test arithmetic with NULL values
  • Mix multiple arithmetic functions in one query