SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
Mathematical functions are essential tools in SQL, enabling you to perform calculations and transform data directly within queries. Here's a breakdown of the functions you mentioned:
SQRT()
SELECT SQRT(25) AS SquareRoot; -- Returns 5
PI()
-- In some databases like MySQL: SELECT PI(); -- Returns 3.141592653589793
SQUARE()
SELECT SQUARE(5) AS SquareValue; -- Returns 25
ROUND()
ROUND(number, decimals)
SELECT ROUND(123.4567, 2) AS RoundedValue; -- Returns 123.46
CEILING()
SELECT CEILING(123.01) AS CeilingValue; -- Returns 124
FLOOR()
SELECT FLOOR(123.99) AS FloorValue; -- Returns 123
Do note that while the functions listed above are standard across many database systems, there might be variations in implementation or the presence of additional functions in specific RDBMSs. Always consult the official documentation of the database system you are working with to ensure compatibility and understand additional options or nuances.
SQL SQRT function example:
SQRT
function is used to calculate the square root of a number.SELECT SQRT(25) AS square_root_result;
Using PI in SQL queries:
PI()
represents the mathematical constant pi (��).SELECT PI() AS pi_value;
SQUARE function in SQL:
POWER
function can be used to calculate the square of a number.SELECT POWER(4, 2) AS square_result;
CEILING vs FLOOR in SQL mathematics:
CEILING
rounds a number up to the nearest integer, while FLOOR
rounds down.SELECT CEILING(4.3) AS ceil_result, FLOOR(4.8) AS floor_result;
Mathematical functions in SQL with examples:
ABS
, EXP
, LOG
, SIN
, COS
, and TAN
.SELECT ABS(-5) AS abs_result, EXP(2) AS exp_result, LOG(10) AS log_result, SIN(30) AS sin_result, COS(45) AS cos_result, TAN(60) AS tan_result;
Performing calculations with SQL ROUND:
ROUND
function is used for rounding a numeric value to a specified number of decimal places.SELECT ROUND(4.567, 2) AS rounded_result;
Using SQL CEILING for rounding up:
CEILING
function rounds a number up to the nearest integer.SELECT CEILING(4.3) AS ceil_result;
Applying FLOOR function in SQL:
FLOOR
function rounds a number down to the nearest integer.SELECT FLOOR(4.8) AS floor_result;
Calculating square roots in SQL with SQRT:
SQRT
function calculates the square root of a number.SELECT SQRT(25) AS square_root_result;