Home » SQL

Arithmetic Operators in SQL

SQL | Arithmetic Operators: In this tutorial, we are going to learn about the various arithmetic operators with query examples in SQL (Structured Query Language).
Submitted by Abhishek Goel, on April 05, 2020

SQL | Arithmetic Operators

Different number-crunching administrators are utilized in SQL to be specific Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulus (%).

For the further examples we will be referring to the following base table:

Table: BOOKS

BookNoNameCostDiscount
9871Nancy Drew2005%
9560Goosebump25010%
9810Sherlock Holmes30015%
8700The Blue Umbrella2006%
5086Gulliver Travels1604%
1272Hellen Keller1504%

1) Addition (+)

It is utilized to perform addition procedures on the information things, things incorporate either single section or different segments.

Implementation:

SELECT bookno, name, cost, discount, cost+50 AS "Final Cost" 
FROM BOOKS;

Output

BookNoNameCostDiscountFinal Cost
9871Nancy Drew2005%250
9560Goosebump25010%300
9810Sherlock Holmes30015%350
8700The Blue Umbrella2006%250
5086Gulliver Travels1604%210
1272Hellen Keller1504%200

Similarly, we can do the addition of two columns too.

2) Subtraction (-)

It is utilized to perform subtraction procedures on the information things, things incorporate either a single section or different segments.

Implementation:

SELECT bookno, name, cost, discount, cost-discount AS "Final Cost" 
FROM BOOKS;

Output

BookNoNameCostDiscountFinal Cost
9871Nancy Drew20012.50237.50
9560Goosebump25030.00270.00
9810Sherlock Holmes30052.50297.50
8700The Blue Umbrella20015.00235.00
5086Gulliver Travels1608.40201.6
1272Hellen Keller1508.00192.00

3) Multiplication (*)

It is utilized to perform multiplication procedures on the information things, things incorporate either a single section or different segments.

Implementation:

SELECT bookno, name, cost, discount, cost*discount AS "Final Cost" 
FROM BOOKS;

Output

BookNoNameCostDiscountFinal Cost
9871Nancy Drew20012.50‭2500‬
9560Goosebump25030.007500
9810Sherlock Holmes30052.5015750
8700The Blue Umbrella20015.003000
5086Gulliver Travels1608.401344
1272Hellen Keller1508.001200

4) Division (/)

It is utilized to perform division procedures on the information things, things incorporate either a single section or different segments.

Implementation:

SELECT bookno, name, cost, discount, cost/discount AS "Final Cost" 
FROM BOOKS;

Output

BookNoNameCostDiscountFinal Cost
9871Nancy Drew20012.50‭16.00‬
9560Goosebump25030.008.33
9810Sherlock Holmes30052.505.71
8700The Blue Umbrella20015.0013.33
5086Gulliver Travels1608.4019.04
1272Hellen Keller1508.0018.75

5) Modulus (%)

This operator is used to get the remainder when the division operation is performed with the data.

Implementation:

SELECT bookno, name, cost, discount, cost%discount AS "Final Cost" 
FROM BOOKS;

Output

BookNoNameCostDiscountFinal Cost
9871Nancy Drew20012.50‭0‬
9560Goosebump25030.0010
9810Sherlock Holmes30052.5037.5
8700The Blue Umbrella20015.005
5086Gulliver Travels1608.400.4
1272Hellen Keller1508.006


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.