PostgreSQL tutorial for Beginners – PostgreSQL – Numeric Function

PostgreSQL – Numeric Function

 

ABS(X)

The ABS() function returns the absolute value of X. Consider the following example −

testdb=# SELECT ABS(2);
+---------------------------------------------------------+
| ABS(2)                                                  |
+---------------------------------------------------------+
| 2                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

testdb=# SELECT ABS(-2);
+---------------------------------------------------------+
| ABS(2)                                                  |
+---------------------------------------------------------+
| 2                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

ACOS(X)

This function returns the arccosine of X. The value of X must range between -1 and 1 or NULL will be returned. Consider the following example −

testdb=# SELECT ACOS(1);
+---------------------------------------------------------+
| ACOS(1)                                                 |
+---------------------------------------------------------+
| 0.000000                                                |
+---------------------------------------------------------+
1 row in set (0.00 sec)

ASIN(X)

The ASIN() function returns the arcsine of X. The value of X must be in the range of -1 to 1 or NULL is returned.

testdb=# SELECT ASIN(1);
+---------------------------------------------------------+
| ASIN(1)                                                 |
+---------------------------------------------------------+
| 1.5707963267949                                         |
+---------------------------------------------------------+
1 row in set (0.00 sec)

ATAN(X)

This function returns the arctangent of X.

testdb=# SELECT ATAN(1);
+---------------------------------------------------------+
| ATAN(1)                                                 |
+---------------------------------------------------------+
| 0.78539816339745                                        |
+---------------------------------------------------------+
1 row in set (0.00 sec)

ATAN2(Y,X)

This function returns the arctangent of the two arguments: X and Y. It is similar to the arctangent of Y/X, except that the signs of both are used to find the quadrant of the result.

testdb=# SELECT ATAN2(3,6);
+---------------------------------------------------------+
| ATAN2(3,6)                                              |
+---------------------------------------------------------+
| 0.46364760900081                                        |
+---------------------------------------------------------+
1 row in set (0.00 sec)

CEIL(X) / CEILING(X)

These functions return the smallest integer value that is not smaller than X. Consider the following example −

testdb=# SELECT CEILING(3.46);
+---------------------------------------------------------+
| CEILING(3.46)                                           |
+---------------------------------------------------------+
| 4                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

testdb=# SELECT CEIL(-6.43);
+---------------------------------------------------------+
| CEIL(-6.43)                                             |
+---------------------------------------------------------+
| -6                                                      |
+---------------------------------------------------------+
1 row in set (0.00 sec)

COS(X)

This function returns the cosine of X. The value of X is given in radians.

testdb=#SELECT COS(90);
+---------------------------------------------------------+
| COS(90)                                                 |
+---------------------------------------------------------+
| -0.44807361612917                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

COT(X)

This function returns the cotangent of X. Consider the following example −

testdb=#SELECT COT(1);
+---------------------------------------------------------+
| COT(1)                                                  |
+---------------------------------------------------------+
| 0.64209261593433                                        |
+---------------------------------------------------------+
1 row in set (0.00 sec)

DEGREES(X)

This function returns the value of X converted from radians to degrees.

testdb=#SELECT DEGREES(PI());
+---------------------------------------------------------+
| DEGREES(PI())                                           |
+---------------------------------------------------------+
| 180.000000                                              |
+---------------------------------------------------------+
1 row in set (0.00 sec)

EXP(X)

This function returns the value of e (the base of the natural logarithm) raised to the power of X.

testdb=#SELECT EXP(3);
+---------------------------------------------------------+
| EXP(3)                                                  |
+---------------------------------------------------------+
| 20.085537                                               |
+---------------------------------------------------------+
1 row in set (0.00 sec)

FLOOR(X)

This function returns the largest integer value that is not greater than X.

testdb=#SELECT FLOOR(7.55);
+---------------------------------------------------------+
| FLOOR(7.55)                                             |
+---------------------------------------------------------+
| 7                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

GREATEST(n1,n2,n3,……….)

The GREATEST() function returns the greatest value in the set of input parameters (n1, n2, n3, and so on). The following example uses the GREATEST() function to return the largest number from a set of numeric values −

testdb=#SELECT GREATEST(3,5,1,8,33,99,34,55,67,43);
+---------------------------------------------------------+
| GREATEST(3,5,1,8,33,99,34,55,67,43)                     |
+---------------------------------------------------------+
| 99                                                      |
+---------------------------------------------------------+
1 row in set (0.00 sec)

LEAST(N1,N2,N3,N4,……)

The LEAST() function is the opposite of the GREATEST() function. Its purpose is to return the least-valued item from the value list (N1, N2, N3, and so on). The following example shows the proper usage and output for the LEAST() function −

testdb=#SELECT LEAST(3,5,1,8,33,99,34,55,67,43);
+---------------------------------------------------------+
| LEAST(3,5,1,8,33,99,34,55,67,43)                        |
+---------------------------------------------------------+
| 1                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

LOG(X) / LOG(B,X)

The single argument version of the function will return the natural logarithm of X. If it is called with two arguments, it returns the logarithm of X for an arbitrary base B. Consider the following example −

testdb=#SELECT LOG(45);
+---------------------------------------------------------+
| LOG(45)                                                 |
+---------------------------------------------------------+
| 1.65321251377534                                               |
+---------------------------------------------------------+
1 row in set (0.00 sec)

testdb=#SELECT LOG(2,65536);
+---------------------------------------------------------+
| LOG(2,65536)                                            |
+---------------------------------------------------------+
| 16.000000                                               |
+---------------------------------------------------------+
1 row in set (0.00 sec)

MOD(N,M)

This function returns the remainder of N divided by M. Consider the following example −

testdb=#SELECT MOD(29,3);
+---------------------------------------------------------+
| MOD(29,3)                                               |
+---------------------------------------------------------+
| 2                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

PI()

This function simply returns the value of pi. SQL internally stores the full double-precision value of pi.

testdb=#SELECT PI();
+---------------------------------------------------------+
| PI()                                                    |
+---------------------------------------------------------+
| 3.141593                                                |
+---------------------------------------------------------+
1 row in set (0.00 sec)

POW(X,Y) / POWER(X,Y)

These two functions return the value of X raised to the power of Y.

testdb=# SELECT POWER(3,3);
+---------------------------------------------------------+
| POWER(3,3)                                              |
+---------------------------------------------------------+
| 27                                                      |
+---------------------------------------------------------+
1 row in set (0.00 sec)

RADIANS(X)

This function returns the value of X, converted from degrees to radians.

testdb=#SELECT RADIANS(90);
+---------------------------------------------------------+
| RADIANS(90)                                             |
+---------------------------------------------------------+
|1.570796                                                 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

ROUND(X) / ROUND(X,D)

This function returns X rounded to the nearest integer. If a second argument, D, is supplied, then the function returns X rounded to D decimal places. D must be positive or all digits to the right of the decimal point will be removed. Consider the following example −

testdb=#SELECT ROUND(5.693893);
+---------------------------------------------------------+
| ROUND(5.693893)                                         |
+---------------------------------------------------------+
| 6                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

testdb=#SELECT ROUND(5.693893,2);
+---------------------------------------------------------+
| ROUND(5.693893,2)                                       |
+---------------------------------------------------------+
| 5.69                                                    |
+---------------------------------------------------------+
1 row in set (0.00 sec)

SIGN(X)

This function returns the sign of X (negative, zero, or positive) as -1, 0, or 1.

testdb=#SELECT SIGN(-4.65);
+---------------------------------------------------------+
| SIGN(-4.65)                                             |
+---------------------------------------------------------+
| -1                                                      |
+---------------------------------------------------------+
1 row in set (0.00 sec)

testdb=#SELECT SIGN(0);
+---------------------------------------------------------+
| SIGN(0)                                                 |
+---------------------------------------------------------+
| 0                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

testdb=#SELECT SIGN(4.65);
+---------------------------------------------------------+
| SIGN(4.65)                                              |
+---------------------------------------------------------+
| 1                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

SIN(X)

This function returns the sine of X. Consider the following example −

testdb=#SELECT SIN(90);
+---------------------------------------------------------+
| SIN(90)                                                 |
+---------------------------------------------------------+
| 0.893997                                                |
+---------------------------------------------------------+
1 row in set (0.00 sec)

SQRT(X)

This function returns the non-negative square root of X. Consider the following example −

testdb=#SELECT SQRT(49);
+---------------------------------------------------------+
| SQRT(49)                                                |
+---------------------------------------------------------+
| 7                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

TAN(X)

This function returns the tangent of the argument X, which is expressed in radians.

testdb=#SELECT TAN(45);
+---------------------------------------------------------+
| TAN(45)                                                 |
+---------------------------------------------------------+
| 1.619775                                                |
+---------------------------------------------------------+
1 row in set (0.00 sec)

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.  

Google –> SETScholars