SQL Arithmetic Operators | SQL Tutorial - Learn with VOKS
Back Next

SQL Arithmetic Operators


Arithmetic operators in SQL are used to perform mathematical operations on numeric columns. These are useful for calculating totals, differences, averages, and more.

1️⃣ Common Arithmetic Operators in SQL

| Operator | Description         | Example            |
|----------|-------------------|------------------|
| `+`      | Addition            | `Salary + Bonus`   |
| `-`      | Subtraction         | `Salary - Tax`     |
| `*`      | Multiplication      | `Quantity * Price` |
| `/`      | Division            | `Total / Count`    |
| `%`      | Modulus (remainder) | `Salary % 1000`    |

2️⃣ Using Arithmetic Operators in SQL

Suppose we have this table:

| EmployeeID | Name  | Salary | Bonus |
|------------|-------|--------|-------|
| 1          | John  | 3000   | 500   |
| 2          | Mary  | 3500   | 600   |
| 3          | David | 4000   | 800   |
| 4          | Anna  | 3200   | 400   |

a) Addition

SELECT Name, Salary + Bonus AS TotalCompensation
FROM Employees;

Output:

| Name  | TotalCompensation |
|-------|------------------|
| John  | 3500             |
| Mary  | 4100             |
| David | 4800             |
| Anna  | 3600             |

b) Subtraction

SELECT Name, Salary - Bonus AS NetSalary
FROM Employees;

Output:

| Name  | NetSalary |
|-------|-----------|
| John  | 2500      |
| Mary  | 2900      |
| David | 3200      |
| Anna  | 2800      |

c) Multiplication

SELECT Name, Bonus * 2 AS DoubleBonus
FROM Employees;

Output:

| Name  | DoubleBonus |
|-------|-------------|
| John  | 1000        |
| Mary  | 1200        |
| David | 1600        |
| Anna  | 800         |

d) Division

SELECT Name, Salary / 1000 AS SalaryInThousands
FROM Employees;

Output:

| Name  | SalaryInThousands |
|-------|------------------|
| John  | 3                |
| Mary  | 3.5              |
| David | 4                |
| Anna  | 3.2              |

e) Modulus (Remainder)

SELECT Name, Salary % 1000 AS Remainder
FROM Employees;

Output:

| Name  | Remainder |
|-------|-----------|
| John  | 0         |
| Mary  | 500       |
| David | 0         |
| Anna  | 200       |


Example Code:
-- Addition
SELECT Name, Salary + Bonus AS TotalCompensation
FROM Employees;

-- Subtraction
SELECT Name, Salary - Bonus AS NetSalary
FROM Employees;

-- Multiplication
SELECT Name, Bonus * 2 AS DoubleBonus
FROM Employees;

-- Division
SELECT Name, Salary / 1000 AS SalaryInThousands
FROM Employees;

-- Modulus
SELECT Name, Salary % 1000 AS Remainder
FROM Employees;
SQL
Introduction What is a Database, Advantages of Database, and Database Tables Creating Databases, Tables, Constraints, and Keys Defining Data Types, Unique ID, Inserting Values, and Handling NULL SELECT, DISTINCT, TOP, LIMIT, UPDATE, DELETE, ORDER BY, WHERE, HAVING, AND, OR, NOT Aggregate function; Min, Max, Count, Avg, Sum Wildcards, IN, AS, LIKE, BETWEEN, and Aliases SQL Joins: Inner, Outer, Left, Right, Full, Cross Set Theory for SQL: Joins, UNION, INTERSECT, EXCEPT, GROUP BY SQL Arithmetic Operators SQL Bitwise Operators SQL Comparison Operator SQL View/Null Functions SQL Comments SQL Case
All Courses
Advance AI Bootstrap C C++ Computer Vision Content Writing CSS Cyber Security Data Analysis Deep Learning Email Marketing Excel Figma HTML Java Script Machine Learning MySQLi Node JS PHP Power Bi Python Python for AI Python for Analysis React React Native SEO SMM SQL