×

MySQL Math Function

The math function operates numerical values and displays required number data. The math function finds out sin, cos, tan values. Here, you find out the absolute value, reminder, and logarithmic values using a data table. The math function mostly includes ABS(), MOD(), ROUND(), and TRUNCATE() functions.

Syntax

MySQL math function syntax gives below.

SELECT math_function();

MySQL math function requires values or argument.

SELECT math_function(value);

The math functions with multiple values are given in the syntax.

SELECT math_function(value1, value2);

MySQL Math Functions

A list of the math functions with descriptions shows below:

FunctionDescription
ABS()The ABS() function displays the absolute value of a given data number. This function does not display available positive or negative signs with a number. You get only the numerical value of the mathematical operation.
CEIL()The CEIL() function returns the smallest integer value. This integer value is greater than or equal to the given value (n).
FLOOR()The FLOOR() function shows the largest integer value. This integer value is not greater than the given value.
MOD()The mod() function displays the remainder of a division operation. The moderator works on division operation and returns reminder values.
ROUND()The ROUND() function shows the round number of the decimal number or given number.
TRUNCATE()The TRUNCATE() function truncates a decimal point of the given number. This function uses in arithmetic operations.
ACOS (n)The ACOS(N) function shows the "arc cosine" of the given value (n). This function returns a null value when the given value is not in a range of -1 to 1.
ASIN (n)The ASIN(n) function displays the "arc sine" of the given value (n). This function shows a null value when the given value is not in a range of -1 to 1.
ATAN ()This function returns the arctangent of the given value (n). It displays a null value when the given value is not in a range of -1 to 1. This function uses in trigonometric operations.
ATAN2 (n,m), ATAN(m,n)This function displays the arctangent of the two variables or values such as an "n" and "m". This function uses in arithmetic and trigonometric operations.
CONV(n, from_base, to_base)This function converts from a given number base to the required number base. This function uses in arithmetic operations.
COS (n)The COS(n) function returns the cosine of the given value (n). Here, the given value (n) is the radian. This function works on the angle of the trigonometry operation.
COT (n)The COT(n) function shows the cotangent of the given value (n). This function works on the angle of the trigonometry operation. This function uses in trigonometric operations.
CRC32 ()The CRC32() function displays a 32-bit unsigned value. This function determines the cyclic redundancy check of the 32-bit value. This function uses in trigonometric operations.
DEGREES (n)The DEGREES() function converts radian's value into degrees of the given value (n).
 EXP (n)The EXP() function displays the specific power of e raised into the given value (n) power. This function uses in arithmetic and trigonometric operations.
LN (n)The LN(n) function shows the natural logarithm of the given value (n).
LOG (n)The LOG() function returns the natural logarithm of the given value. This function uses in arithmetic and trigonometric operations.
LOG10 ()The LOG10() function displays the base-10 logarithm of the given value.
LOG2 ()The LOG2() function shows the base-2 logarithm of the given value. This function uses in arithmetic and trigonometric operations.
PI ()The PI() function returns the PI value. The pi value has a constant or default value in the mathematical operation.
POW ()The POW() function displays the specific power of the given value. This function uses in arithmetic and trigonometric operations.
POWER () The POWER() function shows the specific power of the given value. This function uses in arithmetic and trigonometric operations.
RADIANS()The RADIANS() function returns given value converts into radians value.
RAND()The RAND() function displays a random value that belongs to a floating-point. This function uses in arithmetic operations.
SIGN (n)The SIGN() function shows the sign of the given value (n).  This value depends on the negative (-1), zero (0), or positive (1). This function does not display the original value of the data.
SIN (n)The SIN(n) function returns the sine of a given value (n). This function works on the angle of the trigonometry operation.
SQRT (n)The SQRT(n) function displays the square root of the given value (n). This function uses in arithmetic and trigonometric operations.
TAN (n)The TAN(n) function displays the tangent of the given value (n). This function works on the angle of the trigonometry operation.

Examples of the MySQL Math Function

The math function operates in arithmetic, trigonometric and other mathematical operations. This function shows the functional values of the given data. The math function provides logarithm, square, squarer root, power, etc. This function determines sin, tan, cos, cot, and other trigonometric functions.

Examples of the basic function and its output

MySQL math function returns the pi value, square value, square root value, power of the required value. You can use a single math function or many math functions in a single query.

Example

MySQL math function returns the constant value of the "PI" function. You need to select the pi function. This function does need to include any value in the function.

mysql> select PI();

OUTPUT

MySQL Math Function

The pi function output returns the "3.14" value. This value uses in arithmetic and trigonometric operations.

Example

The given an example uses five functions to operate numerical value. This query works with square root, square and 5th power of the required value. Here, you operate the absolute value and a round number of the value.

mysql> select SQRT(4), power(4, 2), pow(4, 5), ABS(-4.213), round(3.99);

OUTPUT

MySQL Math Function

The above math functions return five functions output. This output needs to use a specific value. The first two column shows square root and square of the "4" value. The third column returns the 5th power of the "4" value. The fourth column function removes positive and negative signs and returns an only value.

Examples of the logarithmic function and its output

Execute logarithmic math function with its value. The log uses base 10, base 2, and natural base with the value. You can see the difference between logarithmic values. This function contains a "10" value in the required log.

mysql> select LOG(10), LOG10(10), LOG2(10), ln(10);

OUTPUT

MySQL Math Function

The above image shows you different log functions with the same value. The first and last columns show the natural log value. The second column shows base 10 log values. The third column shows the base2 log value.

Examples of the trigonometric function and its output

The trigonometric math function returns the side of the angle and arc of the value. Mysql math function provides SIN, COS, TAN, COT functions. Each function shows several values of the same angle.

Example

The example uses multiple operators or functions in a single query. This function contains a "30" value in the required log.

Mysql> select sin(30), cos(30), cot(30), tan(30);

OUTPUT

MySQL Math Function

The output table returns the value of the trigonometric function. You can get the sin, cos, cot, and tan output with 30 angles in the table. Each column displays a different arc value of the same angle.

Example

Execute the ATAN function with value. You can use two functions to get the arctangent of the data. This function contains 2 and 3 data.

Mysql> select ATAN2(2 , 3), ATAN(3, 2);
OR
Mysql> select ATAN(2 , 3), ATAN(3, 2);

OUTPUT

MySQL Math Function

This output image shows two arctangents with swapping numbers. The same number swaps in the same ATAN operator. This operator returns different output values in the table.

Examples of the arithmetical operations and their output

The math function supports addition, subtraction, division, and multiplication operations. You do not need a special function. The mod() function provides a reminder of the division. You can use select keywords, values and operators to get output.

Example

MySQL functions use an operator and two values to operate mathematical operations. The query uses addition (+), subtraction (-), division (/), and multiplication (*) operators. Here, the example uses 5 and 3 as the first value and second value, respectively.

Mysql> select 5 + 3, 
              5 - 3, 
              5 * 3, 
              5 / 3;

OUTPUT

MySQL Math Function

You can see the output values of the mathematical operations. The columns show 8, 2, 15, and 1.6667 values in the table.

Example

Execute mathematical operations using basic operators in the MySQL interface. The multiple operations combine and use in a single query.

Mysql> select 8 + 3 / 2 * 4 - 1;

OUTPUT

MySQL Math Function

The output returns values of the arithmetic operations. You can operation multiple operators simultaneously and get the 13.0000 value.

Example of the mathematical operations and their output

You can combine several arithmetical and trigonometric functions for mathematical operation.  This example uses the pi value with sin(45) and the division operator. Execute the following math function with value.

Mysql> select pi() * sin(45) / 10;

OUTPUT

MySQL Math Function

The above image returns the output of the given math function. The sin(45) multiplies with the pi value and is divided by 10. The MySql returns the "0.2673" value in the output table.


Related Topics

MySQL POW() function

In this context, we will learn how we can use the MySQL POW() function with proper syntax and good examples. Introduction of MySQL POW() function This function in MySQL is used to...

2 minutes read.

MySQL EXPORT_SET() function

In this context, we will learn how we can use the MySQL EXPORT_SET() function with proper syntax and good examples. Introduction of MySQL EXPORT_SET() function This function returns a string and shows...

3 minutes read.

MySQL FORMAT() function

In this context, we will learn how we can use the MySQL FORMAT() function with proper syntax and good examples. Introduction of MySQL FORMAT() function This function in MySQL helps to format...

2 minutes read.

MySQL ROUND() function

In this context, we will learn how we can use the MySQL ROUND() function with proper syntax and good examples. Introduction of MySQL ROUND() function The ROUND() function in MySQL is used...

3 minutes read.

MySQL Union

MySQL contains the same categories of data in a different table. Sometimes, you require multiple tables to collect data together. MySQL union is a function to combine two table's data...

5 minutes read.

Clustered vs Non-Clustered Index

Clustered vs. Non-Clustered Index The difference between clustered and non-clustered index is the most famous question in the database-related interviews. Both indexes have the same physical structure and are stored as...

2 minutes read.

MySQL ELT() function

In this context, we will learn how we can use the MySQL ELT() function with proper syntax and good examples. Introduction of MySQL ELT() function In the ELT function, the number field...

2 minutes read.

MySQL CROSS JOIN

MySQL CROSS JOIN combines all possibilities of the two or more tables and returns the result that contains every row from all contributing tables. It links several columns from the...

4 minutes read.

MySQL Stored Procedure

MySQL creates the "stored procedure" function to operate database information. You can use parameters, blocks, and statements to create a new procedure. The procedure requires a database table to use...

3 minutes read.

MySQL DELETE JOIN

Sometimes join becomes complicated and unwanted. In such cases, we can delete the unwanted joins in the tables. You can delete inner join, left join, right join as per requirement....

3 minutes read.

MySQL Drop Index

Sometimes we have an index that is not required in data operations. In that case, we can use this statement to remove an existing index from the table. We can...

4 minutes read.

MySQL LN() function

In this context, we will learn how we can use the MySQL LN() function with proper syntax and good examples. Introduction of MySQL LN() function For evaluation of the natural logarithm of...

2 minutes read.

MySQL Unique index

It helps to maintain data integrity to enforce the uniqueness of values in one or more columns. We can create more than one UNIQUE index in a single table, which...

3 minutes read.

MySQL REVERSE() function

In this context, we will learn how we can use the MySQL REVERSE() function with proper syntax and good examples. Introduction of MySQL REVERSE() function This function could be used to reverse...

2 minutes read.

MySQL Command line client Basic Queries

MySQL – command line client Basic Queries There are many queries to work with the MySQL command-line client. Today, we will explore MySQL basic queries and their function. The MySQL query...

8 minutes read.

MySQL DAY() function

In this context, we will learn how we can use the MySQL DAY() function with proper syntax and good examples. Introduction of MySQL DAY() function This function in MySQL is used to...

2 minutes read.

MySQL Subquery

The subquery is a MySQL query used to manage data operations. Mainly subquery helps to retrieve data with the necessary condition. It creates a nested query with two different queries....

5 minutes read.

MySQL Invisible Index

Invisible Index MySQL index decides an option to the availability of the index for the query optimizer. This index shows the visible and invisible options to displays the index column in...

3 minutes read.

MySQL Index

An index is a widely used method to access table information quickly. MySQL requires an index for operating table data stored in rows and columns. It's an entry point of...

3 minutes read.

MySQL AVG function

This function works on numerical data type values. The average function shows the average value of the data set. If an average function returns a null value, then the row...

5 minutes read.