How to Subtract One Value from Another in SQL

If you need to subtract one value from another in your SQL database, use the SUBTRACT() function. This function takes two arguments, which are the two values you want to subtract from each other. For example, if you wanted to subtract the value in the price column from the value in the discount column, you would use the following query:

SELECT SUBTRACT(price, discount) FROM table_name;

Examples

Let's look at a few examples of how this function can be used. Suppose you have a table called products with the following data:

products
namepricediscount
Widget10.005.00
Gizmo20.0010.00
Thingamabob30.0015.00


If you wanted to subtract the discount from the price for each product, you would use the following query:

SELECT SUBTRACT(price, discount) FROM products;

This query would return the results 5.00, 10.00, and 15.00, respectively.

Additional Info

The SUBTRACT() function is supported by most major databases, including MySQL, PostgreSQL, and SQL Server. However, the syntax may vary slightly depending on the database you are using. For more information, check out the documentation for your particular database.

Want to build your own LLM Apps with AirOps👇👇