Math Functions

18 Functions

Math functions allow you to perform numerical calculations directly within your SQL queries. Whether you are calculating financial discounts, determining leaderboard rankings, or generating random rewards, these tools help you process numbers efficiently.

1.

ABS()

Description

Returns the absolute (positive) value of a number.

Syntax
ABS(x)
When to Use

Calculating the difference between two scores in a match, regardless of who scored higher.

Example Query
SELECT ABS(score_a - score_b) AS point_gap FROM matches;
πŸ’‘ Pro Tip

This is perfect for distance calculations or any scenario where you only care about the magnitude of a change, not the direction.

2.

ROUND()

Description

Rounds a number to the nearest integer or a specified number of decimal places.

Syntax
ROUND(number, decimal_places)
When to Use

Displaying a clean price after a 15% tax calculation.

Example Query
SELECT ROUND(price * 1.15, 2) FROM products;
πŸ’‘ Pro Tip

If decimal_places is omitted, it rounds to the nearest whole number.

3.

CEIL() / CEILING()

Description

Returns the smallest integer greater than or equal to a number (always rounds up).

Syntax
CEIL(x)
When to Use

Calculating how many pages are needed for pagination if you have 11 items and 5 items per page.

Example Query
SELECT CEIL(11.0 / 5.0); -- Result: 3
πŸ’‘ Pro Tip

Think of this as "the ceiling"β€”it always pushes the number to the next highest integer, no matter how small the decimal.

4.

FLOOR()

Description

Returns the largest integer less than or equal to a number (always rounds down).

Syntax
FLOOR(x)
When to Use

Determining how many full levels a user has earned based on total XP.

Example Query
SELECT FLOOR(total_xp / 1000) AS level FROM users;
πŸ’‘ Pro Tip

Use FLOOR when you need to discard fractional parts without rounding up.

5.

TRUNC()

Description

Truncates a number to a specified number of decimal places by removing extra digits.

Syntax
TRUNC(number, precision)
When to Use

Showing a raw price without rounding (e.g., 9.999 becomes 9.99).

Example Query
SELECT TRUNC(9.999, 2); -- Result: 9.99
πŸ’‘ Pro Tip

Unlike ROUND, TRUNC never changes the value of the digits; it simply "cuts off" the end.

6.

POWER()

Description

Raises a number to the power of another (x^y).

Syntax
POWER(base, exponent)
When to Use

Calculating compound interest or exponential difficulty scaling in a game.

Example Query
SELECT POWER(2, 3); -- Result: 8
πŸ’‘ Pro Tip

You can use fractions as the exponent for roots, like POWER(9, 0.5) for a square root.

7.

SQRT()

Description

Returns the square root of a non-negative number.

Syntax
SQRT(x)
When to Use

Part of the formula for calculating the geometric distance between two points.

Example Query
SELECT SQRT(16); -- Result: 4
πŸ’‘ Pro Tip

Trying to use SQRT on a negative number will cause an error. Use ABS first if needed!

8.

CBRT()

Description

Returns the cube root of a number.

Syntax
CBRT(x)
When to Use

Calculating dimensions based on volume.

Example Query
SELECT CBRT(27); -- Result: 3
πŸ’‘ Pro Tip

Useful for 3D physics calculations or volumetric data.

9.

EXP()

Description

Returns the exponential value (e^x).

Syntax
EXP(x)
When to Use

Used in complex statistical models or natural growth calculations.

Example Query
SELECT EXP(1.0); -- Result: 2.718...
πŸ’‘ Pro Tip

Often the inverse operation of LN().

10.

LN() / LOG()

Description

LN returns the natural logarithm, while LOG returns the base-10 logarithm.

Syntax
LN(x) or LOG(x)
When to Use

Calculating the magnitude of a value on a logarithmic scale.

Example Query
SELECT LOG(100); -- Result: 2
πŸ’‘ Pro Tip

Logarithms are great for normalizing data with massive variance (e.g., income distribution).

11.

MOD()

Description

Returns the remainder of a division.

Syntax
MOD(dividend, divisor) or dividend % divisor
When to Use

Checking if a number is even or odd (e.g., MOD(id, 2) = 0).

Example Query
SELECT MOD(10, 3); -- Result: 1
πŸ’‘ Pro Tip

This is essential for features like alternating row colors or cycling through items in a list.

12.

DIV()

Description

Performs integer division (returns the quotient without the remainder).

Syntax
DIV(y, x)
When to Use

Calculating how many "stacks" of items a player has if each stack holds 64.

Example Query
SELECT DIV(130, 64); -- Result: 2
πŸ’‘ Pro Tip

Returns the integer part of the division result.

13.

SIGN()

Description

Returns -1 for negative numbers, 0 for zero, and 1 for positive numbers.

Syntax
SIGN(x)
When to Use

Determining the direction of a change (up, down, or flat).

Example Query
SELECT SIGN(new_xp - old_xp);
πŸ’‘ Pro Tip

Useful for visualizing trends (red for -1, green for 1).

14.

RANDOM()

Description

Returns a random float between 0.0 and 1.0.

Syntax
RANDOM()
When to Use

Selecting a random winner for a giveaway or a random challenge in the Arena.

Example Query
SELECT * FROM questions ORDER BY RANDOM() LIMIT 1;
πŸ’‘ Pro Tip

To get a random integer between 1 and 10, use: FLOOR(RANDOM() * 10 + 1).

15.

PI()

Description

Returns the constant value of Ο€ β‰ˆ 3.14159.

Syntax
PI()
When to Use

Calculating the area of a circular zone or boundary.

Example Query
SELECT PI();
πŸ’‘ Pro Tip

More precise than manually typing 3.14159...

16.

DEGREES()

Description

Converts radians to degrees.

Syntax
DEGREES(radians)
When to Use

Converting scientific coordinate data into human-readable map angles.

Example Query
SELECT DEGREES(PI()); -- Result: 180
πŸ’‘ Pro Tip

Scientific functions usually output radians, so this is needed for UI display.

17.

RADIANS()

Description

Converts degrees to radians.

Syntax
RADIANS(degrees)
When to Use

Preparing degree values for use in trigonometric functions like SIN or COS.

Example Query
SELECT RADIANS(180); -- Result: 3.14159...
πŸ’‘ Pro Tip

SQL trig functions (SIN, COS, TAN) expect input in radians, not degrees.

18.

WIDTH_BUCKET()

Description

Assigns a value to a "bucket" (group) within a specified range.

Syntax
WIDTH_BUCKET(value, min, max, count)
When to Use

Automatically grouping users into 4 "XP Tiers" based on their total score.

Example Query
SELECT username, WIDTH_BUCKET(xp, 0, 5000, 4) FROM users;
πŸ’‘ Pro Tip

This is a powerful "advanced" math function that makes building histograms or tiered ranking systems incredibly easy.