PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References
PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References

PostgreSQL - Subtract (-) Operator



The PostgreSQL - (subtract) operator is used to subtract two values. It operates on numerical values.

The example below describes how to use subtract operator in various conditions:

Example:

Consider a database table called Sample with the following records:

DataVar1Var2
Data1101
Data2152
Data3203
Data4254
Data5305
Data6356

  • Using with WHERE Clause: To select records of table where difference of Var1 and Var2 column values is greater than 20, the query is given below.

    SELECT * FROM Sample
    WHERE Var1 - Var2 > 20;
    

    The query will produce following result:

    DataVar1Var2
    Data4254
    Data5305
    Data6356
  • Using with AS Clause: The difference of Var1 and Var2 column values can be displayed in a different column using AS clause:

    SELECT *, (Var1 - Var2) AS Diff FROM Sample;
    

    The query will produce following result:

    DataVar1Var2Diff
    Data11019
    Data215213
    Data320317
    Data425421
    Data530525
    Data635629
  • Using with UPDATE Clause: To update the column Var1 with the difference 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:

    DataVar1Var2
    Data191
    Data2132
    Data3173
    Data4214
    Data5255
    Data6296
  • Using with values: To subtract two values, we can simply use SELECT statement:

    SELECT 50 - 30;
    

    The query will produce following result:

    20
    
    (1 row) 
    

❮ PostgreSQL Operators