Arithmetic Operators in PostgreSQL

Learn via video courses
Topics Covered

Overview

Arithmetic operator in PostgreSQL works similarly to operators in any other database and programming language. We can perform mathematical operations on numeric values using arithmetic operators. Numeric values can be modified by performing arithmetic operations within SQL queries by using the arithmetic operators.

Introduction

ARithmetic operator allows us to perform mathematical calculations on the numeric data type and it also allows us to change the numeric values by performing operations within the SQL query. Arithmetic operators in PostgreSQL include Addition Operator (+), Subtraction Operator (-), Multiplication Operator (*), Division Operator (/), Modulus Operator (%), Exponentiation Operator (^), etc.

Basic Syntax and Structure

The syntax of using arithmetic operators in PostgreSQL is similar to the syntax of using operators in any mathematical calculations. The operator is placed between the values of both the operands, and operands are the values on which we want to operate.

The parameters of the syntax are:

  • operand1 is the first operand on which the operation is performed.
  • operator specifies the operation to be performed.
  • operand2 is the first operand on which the operation is performed.

Addition Operator (+)

Addition operator adds the value of both the operands and returns the sum of both values as a result.

Example: Let us assume we have a table named Student and in this table, we have attributes Student_Id, Student_Name and Student_Marks. And following data is stored in the table.

Student_IdStudent_NameStudent_Marks
1Mrinal80
2Steve70
3Clerk90
4Janavi60
5John50

Now we want to fetch the marks record of all the students and we want that 10 will be added to the marks of every student in the output. Then we have to write the query given below:

Output:

Student_IdStudent_Namemarks+10
1Mrinal90
2Steve80
3Clerk100
4Janavi70
5John60

Subtraction Operator (-)

The subtraction operator subtracts the value of the right operand from the value of the left operand and returns the difference of both values as a result.

Example: Let us assume we have a table named Student and in this table, we have attributes Student_Id, Student_Name and Student_Marks. And following data is stored in the table.

Student_IdStudent_NameStudent_Marks
1Mrinal80
2Steve70
3Clerk90
4Janavi60
5John50

Now we want to fetch the marks record of all the students and we want that 10 will be subtracted from the marks of every student in the output. Then we have to write the query given below:

Output:

Student_IdStudent_Namemarks-10
1Mrinal70
2Steve60
3Clerk80
4Janavi50
5John40

Multiplication Operator (*)

The multiplication operator multiplies the value of the right operand with the value of the left operand and returns the multiplication of both values as a result.

Example: Let us assume we have a table named Student and in this table, we have attributes Student_Id, Student_Name and Student_Marks. And following data is stored in the table.

Student_IdStudent_NameStudent_Marks
1Mrinal80
2Steve70
3Clerk90
4Janavi60
5John50

Now we want to fetch the marks record of all the students and we want that 10 will be multiplied by the marks of every student in the output. Then we have to write the query given below:

Output:

Student_IdStudent_Namemarks*10
1Mrinal800
2Steve700
3Clerk900
4Janavi600
5John500

Division Operator (/)

The division operator divides the value of both operands and returns the quotient of the division of both values as a result.

Example: Let us assume we have a table named Student and in this table, we have attributes Student_Id, Student_Name and Student_Marks. And following data is stored in the table.

Student_IdStudent_NameStudent_Marks
1Mrinal80
2Steve70
3Clerk90
4Janavi60
5John50

Now we want to fetch the marks record of all the students and we want the marks of every student will be divided by 10 in the output. Then we have to write the query given below:

Output:

Student_IdStudent_Namemarks/10
1Mrinal8
2Steve7
3Clerk9
4Janavi6
5John5

Modulus Operator (%)

The modulus operator returns the remainder of the division of values of both operands as a result.

Example: Let us assume we have a table named Student and in this table, we have attributes Student_Id, Student_Name and Student_Marks. And following data is stored in the table.

Student_IdStudent_NameStudent_Marks
1Mrinal80
2Steve70
3Clerk90
4Janavi60
5John50

Now we want to fetch the marks record of all the students and we want that marks % 8(remainder when marks are divided by 8) in the output. Then we have to write the query given below:

Output:

Student_IdStudent_Namemarks%10
1Mrinal0
2Steve6
3Clerk2
4Janavi4
5John2

Exponentiation Operator (^)

The Exponentiation Operator in PostgreSQL calculates the exponent of the right-hand operand. And returns the calculated value as an output. Example:

Output

exponentiationExample
8

Explanation: As we have written 2^3 in the query, so 2 raise to power 3 will be calculated and the result of the calculation i.e. 8 is returned as an output.

Understanding Operator Precedence

Operators have the precedence order which is used during the calculation of the result of expressions. Operator precedence tells the sequence of calculation of operators in an expression. The operator precedence table of PostgreSQL is given below:

OperatorAssociativityDescription
::lefttypecasting operator
[ ]leftfor selecting element of an array
.leftfor separating column/table name
-rightunary minus
^leftexponentiation
* / %leftmultiplication, division, modulo
+ -leftaddition, subtraction
IS Check for UNKNOWN, NULL, FALSE, TRUE
ISNULL check for NULL
NOTNULL check for NOT NULL
(any other)leftany user-defined or native operators
IN set membership
BETWEEN containment
OVERLAPS time interval overlap
LIKE ILIKE for matching string patterns
< > less than, greater than
=rightequality, assignment
NOTrightlogical negation
ANDleftlogical conjunction
ORleftlogical disjunction

Combining and Chaining Operators

Multiple operators can be combined and chained to form a complex expression. In complex expressions, the expression value will be calculated according to the operator's precedence.

Example:

Output:

ComplexExpression
7

Explanation: Here we have 2^2*3-5 in the query, among all the operators, the exponent operator has higher precedence so firstly it will be calculated.

2^2*3-5 = 4*3-5

Now among * and - operators, * has higher precedence so now * will be calculated.

4*3-5 = 12-5

Now we have left with a single operator in the expression, - will be calculated.

12-5 = 7

In this way, we are getting the 7 as an answer.

Importance of Parentheses

Parentheses plays a very important role in the calculation of complex expression. Parentheses will always be calculated first irrespective of the precedence of the operator. But inside the parentheses calculation again operator precedence is followed.

Example:

Output:

ComplexExpression
12

Explanation: Here we have (2+2)*3 in the query, in the expression * has the higher precedence, but as we have parentheses in the expression so firstly parentheses will be solved.

( 2 + 2 ) * 3 = 4 * 3

Now we have left with a single operator in the expression, * will be calculated.

4 * 3 = 12

In this way, we are getting the 12 as an answer.

Handling Arithmetic Operators With NULL Values

We have to handle NULL values properly while using arithmetic operators in PostgreSQL. PostgreSQL does not perform arithmetic calculations on NULL values. It will return nothing when we try to use NULL values with arithmetic operators in PostgreSQL.

Example:

Output

operationWithNull

Explanation: As we have written 4+null in the query, so here we are performing addition operation with null value, so we are getting nothing in the output.

Conclusion

  • We can perform mathematical operations on numeric values using arithmetic operators in PostgreSQL.
  • Arithmetic operators in PostgreSQL includes Addition Operator (+), Subtraction Operator (-), Multiplication Operator (*), Division Operator (/), Modulus Operator (%), Exponentiation Operator (^), etc.
  • operand1 operator operand2 is a syntax of using arithmetic operators in PostgreSQL.
  • Operators have the precedence order which is used during the calculation of the result of expressions.
  • Multiple operators can be combined and chained to form a complex expression.
  • PostgreSQL does not perform arithmetic calculations on NULL values.