SQL Addition Operator

The SQL + (Addition) operator is used to add two numeric values. This operator can be applied within SQL queries to perform arithmetic calculations on numeric columns or values.

In this tutorial, we will explore the SQL Addition operator, its syntax, and practical examples.


Syntax of SQL Addition Operator

The basic syntax of the SQL Addition operator is as follows:

</>
Copy
SELECT number1 + number2 AS result;

Or when applied to table columns:

</>
Copy
SELECT column1 + column2 AS total_value
FROM table_name;

Explanation:

  • The + operator adds two numeric values.
  • If used in a SELECT statement, it can perform addition on table columns or hardcoded values.
  • The result can be displayed using an alias (e.g., AS total_value).

Step-by-Step Examples Using SQL Addition Operator

1. Using Addition Operator with Constants

Let’s perform a simple addition operation on numeric values:

</>
Copy
SELECT 15 + 10 AS sum_result;

Explanation:

  • This query adds 15 and 10.
  • The resul (25) is displayed under the alias sum_result.

2. Using Addition Operator in a Table Query

Let’s create an employees table and demonstrate the addition of salary components:

</>
Copy
CREATE TABLE employees (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(50),
    basic_salary DECIMAL(10,2),
    bonus DECIMAL(10,2)
);

Insert sample records:

</>
Copy
INSERT INTO employees (name, basic_salary, bonus)
VALUES 
('Alice', 5000.00, 500.00),
('Bob', 6000.00, 750.00),
('Charlie', 5500.00, 600.00);

Now, let’s calculate the total salary (basic salary + bonus) for each employee:

</>
Copy
SELECT name, basic_salary, bonus, (basic_salary + bonus) AS total_salary
FROM employees;

Explanation:

  • basic_salary + bonus calculates the total salary for each employee.
  • The result is displayed under the alias total_salary.

Conclusion

In this tutorial we explored:

  1. How to use the Addition Operator in SQL.
  2. Adding two constant numbers in a query.
  3. Performing addition on table columns to calculate total salary.