PostgreSQL - Add (+) Operator
The PostgreSQL + (add) operator is used to add two values. It operates on numerical values.
The example below describes how to use add operator in various conditions:
Example:
Consider a database table called Sample with the following records:
Data | Var1 | Var2 |
---|---|---|
Data1 | 10 | 1 |
Data2 | 15 | 2 |
Data3 | 20 | 3 |
Data4 | 25 | 4 |
Data5 | 30 | 5 |
Data6 | 35 | 6 |
-
Using with WHERE Clause: To select records of table where sum of Var1 and Var2 column values is greater than 25, the query is given below:
SELECT * FROM Sample WHERE Var1 + Var2 > 25;
The query will produce following result:
Data Var1 Var2 Data4 25 4 Data5 30 5 Data6 35 6 -
Using with AS Clause: The sum of Var1 and Var2 column values can be displayed in a different column using AS clause:
SELECT *, (Var1 + Var2) AS SUM FROM Sample;
The query will produce following result:
Data Var1 Var2 SUM Data1 10 1 11 Data2 15 2 17 Data3 20 3 23 Data4 25 4 29 Data5 30 5 35 Data6 35 6 41 -
Using with UPDATE Clause: To update the column Var1 with the sum of columns Var1 and Var2, the query is given below:
UPDATE Sample SET Var1 = Var1 + Var2; --See result SELECT * FROM Sample;
The query will produce following result:
Data Var1 Var2 Data1 11 1 Data2 17 2 Data3 23 3 Data4 29 4 Data5 35 5 Data6 41 6 -
Using with values: To add two values, we can simply use SELECT statement:
SELECT 30 + 50;
The query will produce following result:
80 (1 row)
❮ PostgreSQL Operators