SQL Tutorial
SQL Database
SQL Table
SQL Select
SQL Order By
SQL Insert
SQL Update
SQL Delete
Difference
SQL Injection
SQL String Functions
Miscl
- SQL Formatter
- SQL group by
- SQL add/drop/update column operation
- SQL CAST Function
- SQL Comments
- SQL CONCAT Function
- CTE (Common Table Expression)SQL
- How to use distinct in SQL?
- Joining Three or More Tables in SQL
- What is Web SQL?
- How to create functions in SQL?
- How to run SQL Script?
- How to Delete Duplicate Rows in SQL?
- Nth Highest salary
- 12 Codd's Rules
- SQL EXCEPT
- Types of SQL JOIN
- Change datatype of column in SQL
- SQL Auto Increment
- SQL Like
- Commit and Rollback in SQL
- SQL Concatenate
- SQL get month from the date
- Savepoint in SQL
- SQL ORDER BY DATE
- TIME Datatype in SQL
- SQL BETWEEN
- CRUD Operations in SQL
- SQL INDEX
- Scalar Functions in SQL
- SET Operators in SQL
- Types of SQL Commands
- TCL Commands in SQL
- SQL Subquery
- SQL View
- Constraints in SQL
- Pattern Matching in SQL
- SQL Date Functions
- DDL Commands in SQL
- DML Commands in SQL
- SQL CASE
- SQL Inner Join
- SQL IN Operator
- Check Constraint in SQL
- SQL CLAUSES
- SQL LOGICAL OPERATORS
- Delete Column from Table
- Add Column in the Table
- Delete one row in SQL
- Change the Column Value
- How to Add Foreign Key in SQL
- Add a Primary Key
- Insert One or More rows
- How to Use LIKE in SQL
- Cursor in SQL
- Difference Between DROP and Truncate
- SQL Comparison Operators
- SQL COUNT WHERE
- SQL SELECT MIN
- SQL Stored Procedure
- SQL SELECT AVG
- SQL SELECT MAX
- SQL ADD COLUMN
- How to use Auto-Increment in SQL
- SQL Languages
- SQL Arithmetic Operators
- How to Use GROUP BY in SQL
- How to Use ORDER BY in SQL
- Trigger in SQL
- What is Race Condition
- SQL COUNT DISTINCT
PL/SQL Tutorial
Sql Interview Question
SQl Quiz
SQL Arithmetic Operators
In Structured Query Language, the arithmetic operators are used to perform mathematical operations on the numerical values stored in the database tables.
We can use these operators with the SELECT statement in SQL. We can also use the WHERE clause in the SELECT statement for performing operations on particular rows.
These types of operators are used between two numerical operands for performing addition, subtraction, multiplication, and division operations.
The arithmetic operators in SQL are categorized into the following five types:
- SQL Addition Operator (+)
- SQL Subtraction Operator (-)
- SQL Multiplication Operator (*)
- SQL Division Operator (/)
- SQL Modulus Operator (%)
SQL Addition Operator (+)
The SQL Addition Operator performs the addition on the numerical columns in the table.
If you want to add the values of two numerical columns in the table, then you have to specify both columns as the first and second operand. You can also add the new integer value in the value of the integer column.
Syntax of SQL Addition Operator:
Addition Operator with WHERE Clause
The addition operator can also be used with the WHERE clause in the SQL SELECT query.
The syntax for using the WHERE clause with the addition operator is given below:
Implementation of Addition operator in SQL:
The following CREATE query creates the Employee table with five fields:
(
Employee_ID INT AUTO_INCREMENT PRIMARY KEY,
Emp_Name VARCHAR (50),
Emp_City VARCHAR (20),
Emp_Salary INT NOT NULL,
Emp_Bonus INT NOT NULL
) ;
The following INSERT query inserts the record of employees into the Employee table:
(102, Tushar, Lucknow, 29000, 1000),
(103, Vivek, Kolkata, 35000, 2500),
(104, Shivam, Goa, 22000, 3000);
The following SELECT query shows the data of the Employee table:
Employee_Id | Emp_Name | Emp_City | Emp_Salary | Emp_bonus |
---|---|---|---|---|
101 | Anuj | Ghaziabad | 25000 | 2000 |
102 | Tushar | Lucknow | 29000 | 1000 |
103 | Vivek | Kolkata | 35000 | 2500 |
104 | Shivam | Goa | 22000 | 3000 |
The following query adds the Emp_Salary and Emp_Bonus of each employee of the Employee table using the addition operator:
Output:
The following query adds 15000 to the salary of each employee in the Emp_Salary column of the Employee table:
Output:
The following query performs the addition operation on the above Employee table with the WHERE clause:
It shows only records of those employees whose Emp_Salary is greater than 25000:
Output:
SQL Subtraction Operator (-)
The SQL Subtraction Operator performs the subtraction on the numerical columns in the table.
If we want to subtract the values of one numerical column from the values of another numerical column, then we have to specify both columns as the first and second operand. We can also subtract the integer value from the values of the integer column.
Syntax of SQL Subtraction Operator:
Subtraction Operator with WHERE Clause
The subtraction operator can also be used with the WHERE clause in the SELECT query.
The syntax for using the WHERE clause with the subtraction operator is given below:
Implementation of Subtraction operator in SQL:
The following CREATE query creates the Employee table with five fields:
(
Employee_ID INT AUTO_INCREMENT PRIMARY KEY,
Emp_Name VARCHAR (50),
Emp_City VARCHAR (20),
Emp_Salary INT NOT NULL,
Emp_Panelty INT NOT NULL
) ;
The following INSERT query inserts the record of employees into the Employee table:
(102, Tushar, Lucknow, 29000, 1000),
(103, Vivek, Kolkata, 35000, 700),
(104, Shivam, Goa, 22000, 500);
The following SELECT query shows the data of the Employee table:
Employee_Id | Emp_Name | Emp_City | Emp_Salary | Emp_Panelty |
---|---|---|---|---|
101 | Anuj | Ghaziabad | 25000 | 500 |
102 | Tushar | Lucknow | 29000 | 1000 |
103 | Vivek | Kolkata | 35000 | 700 |
104 | Shivam | Goa | 22000 | 500 |
The following query subtracts the values of the Emp_Panelty column from the Emp_Salary column of the Employee table using the subtraction operator:
Output:
The following query performs the subtraction operation on the above Employee table with the WHERE clause:
It shows only records of those employees whose Employee_ID is 103:
Output:
The following query subtracts 10000 from the salary of each employee of the Employee table:
Output:
SQL Multiplication Operator (*)
The SQL Multiplication Operator performs the multiplication on the numerical columns in the table.
If you want to multiply the values of two numerical columns, then you have to specify both columns as the first and second operand. You can also multiply the integer value with the values of an integer column.
Syntax of SQL Multiplication Operator:
Multiplication Operator with WHERE Clause
The multiplication operator (*) can also be used with the WHERE clause in the SELECT query.
The syntax for using the WHERE clause with the multiplication operator is given below:
Implementation of Multiplication operator in SQL:
The following CREATE query creates the Cars table with four fields:
(
Car_Number INT PRIMARY KEY,
Car_Name VARCHAR (50),
Car_Price INT NOT NULL,
Car_AmountINT NOT NULL
) ;
The following INSERT query inserts the record of cars into the Cars table:
VALUES (2578, Creta, 3, 1500000),
(9258, Audi, 2, 3000000),
(8233, Venue, 6, 900000),
(6214, Nexon, 7, 1000000);
The following SELECT query shows the data of the Cars table:
Car_Number | Car_Name | Car_Amount | Car_Price |
---|---|---|---|
2578 | Creta | 3 | 1500000 |
9258 | Audi | 2 | 3000000 |
8233 | Venue | 6 | 900000 |
6214 | Nexon | 7 | 1000000 |
The following query multiplies the values of the Car_Amount column with the Car_Price column of the Cars table using the Multiplication operator:
Output:
The following query performs the multiplication operation on the above Cars table with the WHERE clause:
It shows only those records of cars whose Car_Price is greater than and equal to 1000000.
Output:
SQL Division Operator (/)
The SQL Division operator divides the numerical values of one column by the numerical values of another column.
Syntax of SQL Division Operator:
Division Operator with WHERE Clause
The SQL division operator can also be used with the WHERE clause in the SELECT query.
The syntax for using the WHERE clause with the division operator is given below:
Implementation of Division operator in SQL:
The following CREATE query creates the Cars table with four fields:
(
Car_Number INT PRIMARY KEY,
Car_Name VARCHAR (50),
Car_Price INT NOT NULL,
Car_AmountINT NOT NULL
) ;
The following INSERT query inserts the record of cars into the Cars table:
VALUES (2578, Creta, 3, 1500000),
(9258, Audi, 2, 3000000),
(8233, Venue, 6, 900000),
(6214, Nexon, 10, 1000000);
The following SELECT query shows the data of the Cars table:
Car_Number | Car_Name | Car_Amount | Car_Price |
---|---|---|---|
2578 | Creta | 3 | 1500000 |
9258 | Audi | 2 | 3000000 |
8233 | Venue | 6 | 900000 |
6214 | Nexon | 10 | 1000000 |
The following query divides the values of the Car_Price column by the Car_Amount column of the Cars table using the Multiplication operator:
Output:
The following query performs the division operation on the above Cars table with the WHERE clause:
It shows the record of those cars whose Car_Number is 9258 from the Cars table.
Output:
SQL Modulus Operator (%)
The SQL Modulus Operator provides the remainder when the numerical values of one column are divided by the numerical values of another column.
Syntax of Modulus Operator in SQL:
Implementation of Modulus operator in SQL:
The following CREATE query creates the Student table with four fields:
(
Student_ID INT PRIMARY KEY,
Student_Name VARCHAR (50),
Student_MathsINT,
Student_English INT NOT NULL
) ;
The following INSERT query inserts the record of the student into the Student table:
(202, Tushar, 25, 100),
(203, Vivek, 30, 90),
(204, Shivam, 40, 80);
The following SELECT query shows the data of the Student table:
Student_Id | Student_Name | Student_Maths | Student_English |
---|---|---|---|
201 | Anuj | 30 | 60 |
202 | Tushar | 25 | 100 |
203 | Vivek | 30 | 90 |
204 | Shivam | 40 | 80 |
The following query divides the marks Student_English column by Marks of Student_Maths of each student in the Student table:
Output:
The following query performs the modulus operation on the above Student table with the WHERE clause:
It shows the record of those students whose Student_Id is greater than 202.
Output: