
- SQL - Home
- SQL - Roadmap
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL - Comments
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Cheatsheet
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
SQL - Arithmetic Operators
The SQL Arithmetic operators are used to perform basic mathematical operations on numeric data types in a database. The most common arithmetic operators used in SQL are:
Here is a list of all the arithmetic operators available in SQL.
Operator | Description | Example |
---|---|---|
+ | Adds the values both sides of the operator. | 10 + 20 gives 30 |
- | Subtracts right hand operand from left hand operand. | 20 - 30 gives -10 |
* | Multiplies the values from both sides of the operator. | 10 * 20 gives 200 |
/ | Divides left hand operand by right hand operand. | 20 / 10 gives 2 |
% | Divides left hand operand by right hand operand and returns the reminder remainder. | 5 % 2 gives 1 |
In this chapter we will learn about each operator one-by-one with examples showing its usage. Before proceeding further, let us create a table named EMPLOYEE using the following query −
CREATE TABLE EMPLOYEE( ID INT NOT NULL, NAME VARCHAR(15) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(25), SALARY DECIMAL(10, 2), JOIN_DATE DATE, PRIMARY KEY(ID) );
Once the table is created, we can insert some values into the table using the following queries −
INSERT INTO EMPLOYEE(ID, NAME, AGE, ADDRESS, SALARY, JOIN_DATE) VALUES(2, 'Ramesh', 21, 'Hyderabad', 25550.12, '2023/01/02'); INSERT INTO EMPLOYEE(ID, NAME, AGE, ADDRESS, SALARY, JOIN_DATE) VALUES(1, 'khilan', 22, 'Nijamabad', 57500.84, '2022/01/14'); INSERT INTO EMPLOYEE(ID, NAME, AGE, ADDRESS, SALARY, JOIN_DATE) VALUES(4, 'Kaushik', 18, 'Bangolore', 47275.43, '2023/03/15'); INSERT INTO EMPLOYEE(ID, NAME, AGE, ADDRESS, SALARY, JOIN_DATE) VALUES(5, 'chaitali', 23, 'Ranchi', 40700.76, '2022/04/18'); INSERT INTO EMPLOYEE(ID, NAME, AGE, ADDRESS, SALARY, JOIN_DATE) VALUES(6, 'Hardhik', 19, 'Noida', 44200.09, '2023/06/04'); INSERT INTO EMPLOYEE(ID, NAME, AGE, ADDRESS, SALARY, JOIN_DATE) VALUES(3, 'komal', 23, 'Chennai', 44200.09, '2023/10/08');
Let us verify whether the table is created or not using the following query −
SELECT * FROM EMPLOYEE;
As we can see in the below output, the table has been created in the database.
ID | NAME | AGE | ADDRESS | SALARY | JOIN_DATE |
---|---|---|---|---|---|
1 | Khilan | 22 | Nizamabad | 57500.84 | 2022-01-14 |
2 | Ramesh | 21 | Hyderabad | 25550.12 | 2023-01-02 |
3 | Komal | 23 | Chennai | 44200.09 | 2023-10-08 |
4 | kaushik | 18 | Bangalore | 47275.43 | 2023-03-15 |
5 | Chaitali | 23 | Ranchi | 40700.76 | 2022-04-18 |
6 | Hardhik | 19 | Noida | 44200.09 | 2023-06-04 |
Now, let us perform all the SQL comparison operations using the above table.
SQL Addition (+) Operator
The addition operator in SQL is used to add two or more numeric values. It is similar to the "plus" symbol in basic mathematics.
Example
In the following example, we are trying to perform the addition operation to give all employees a bonus of 5000 to their existing salary.
SELECT ID, NAME, SALARY, SALARY + 5000 AS "SALARY_BONUS" FROM employee;
Output
When we execute the above query, the output is obtained as follows −
ID | NAME | SALARY | SALARY_BONUS |
---|---|---|---|
1 | Khilan | 57500.84 | 62500.84 |
2 | Ramesh | 25550.12 | 30550.12 |
3 | Komal | 44200.09 | 49200.09 |
4 | kaushik | 47275.43 | 52275.43 |
5 | Chaitali | 40700.76 | 45700.76 |
6 | Hardhik | 44200.09 | 49200.09 |
SQL Subtraction (-) Operator
The subtraction operator in SQL is used to subtract one numeric value from another. It is similar to the "minus" symbol in basic mathematics.Example
In here, we are using the - operator to find the salary difference between the highest-paid and lowest-paid employees.
SELECT MAX(salary), MIN(salary), MAX(salary)-MIN(salary) AS salary_difference FROM employee;
Output
On executing the above query, the output is displayed as follows −
MAX(salary) | MIN(salary) | salary_difference |
---|---|---|
57500.84 | 25550.12 | 31950.72 |
SQL Multiplication (*) Operator
The multiplication operator in SQL is used to perform mathematical multiplication on numeric values. It allows us to multiply two or more columns or numeric expressions together, resulting in a new value representing the product of the operands.
Example
Now, we are performing * operation to give all employees a 10% bonus on their salary.
SELECT NAME, AGE, ADDRESS, SALARY * 1.10 AS new_salary_with_bonus FROM employee;
Output
The table for the above query produced as given below −
NAME | AGE | ADDRESS | new_salary_with_bonus |
---|---|---|---|
Khilan | 22 | Nijamabad | 63250.9240 |
Ramesh | 21 | Hyderabad | 28105.1320 |
Komal | 23 | Chennai | 48620.0990 |
kaushik | 18 | Bangalore | 52002.9730 |
Chaitali | 23 | Ranchi | 44770.8360 |
Hardhik | 19 | Noida | 48620.0990 |
SQL Division (/) Operator
The division operator (/) in SQL is used to perform mathematical division on numeric values. It allows us to divide one numeric operand by another, resulting in a new value representing the quotient of the division.
Example
Here, we are using the / operator to calculate the average salary of all employees.
SELECT AVG(SALARY) AS average_salary FROM employee;
Output
Following is the output of the above query −
average_salary |
---|
43237.888333 |
SQL Modulus (%) Operator
The modulus operator (%) in SQL is used to find the remainder after dividing one numeric value by another. It returns the integer remainder of the division operation.
Example
In the following example, we are using the % operator to find out the employees having an even employee ID.
SELECT * FROM employee WHERE ID % 2 = 0;
Output
The output produced is as shown below −
ID | NAME | AGE | ADDRESS | SALARY | JOIN_DATE |
---|---|---|---|---|---|
2 | Ramesh | 21 | Hyderabad | 25550.12 | 2023-01-02 |
4 | kaushik | 18 | Bangalore | 47275.43 | 2023-03-15 |
6 | Hardhik | 19 | Noida | 44200.09 | 2023-06-04 |