SUBTRACTION Operator (-) - SQL
Overview
The Subtraction operator (-) in SQL subtracts one number from another. It can be used in SQL queries to perform arithmetic subtraction on numeric columns or values.
Syntax:
SELECT number1 - number2
number1 - number2
subtracts the value of number2
from number1
.
Sample Data:
first_name | vacation_days |
---|---|
Frank | 5 |
Jane | 2 |
Ashley | 3 |
Glenn | -3 |
Kelly | 2 |
Richard | -7 |
George | 2 |
Kyle | 1 |
James | -2 |
Gustavo | -10 |
Example: Hard-coded values
In this example, the Subtraction operator is used to subtract 2 from 5.
Example:
SELECT 5 - 2
Example: Query without the WHERE Statement
In this example, we are returning the vacation_days column and a calculated column which subtracts 10 from each value in the vacation_days column. The final output is the original vacation_days column alongside the subtracted values column.
Example:
SELECT vacation_days, vacation_days - 10
FROM company.employees
Example: Query with the WHERE Statement
In this example, we are filtering for only rows in department_id 1. We are returning the vacation_days column and a calculated column which subtracts 10 from each value in the vacation_days column. The final output is the original vacation_days column alongside the subtracted values column.
Example:
SELECT vacation_days, vacation_days - 10
FROM company.employees
WHERE department_id = 1