# 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

BookNo | Name | Cost | Discount |
---|---|---|---|

9871 | Nancy Drew | 200 | 5% |

9560 | Goosebump | 250 | 10% |

9810 | Sherlock Holmes | 300 | 15% |

8700 | The Blue Umbrella | 200 | 6% |

5086 | Gulliver Travels | 160 | 4% |

1272 | Hellen Keller | 150 | 4% |

### 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**

BookNo | Name | Cost | Discount | Final Cost |
---|---|---|---|---|

9871 | Nancy Drew | 200 | 5% | 250 |

9560 | Goosebump | 250 | 10% | 300 |

9810 | Sherlock Holmes | 300 | 15% | 350 |

8700 | The Blue Umbrella | 200 | 6% | 250 |

5086 | Gulliver Travels | 160 | 4% | 210 |

1272 | Hellen Keller | 150 | 4% | 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**

BookNo | Name | Cost | Discount | Final Cost |
---|---|---|---|---|

9871 | Nancy Drew | 200 | 12.50 | 237.50 |

9560 | Goosebump | 250 | 30.00 | 270.00 |

9810 | Sherlock Holmes | 300 | 52.50 | 297.50 |

8700 | The Blue Umbrella | 200 | 15.00 | 235.00 |

5086 | Gulliver Travels | 160 | 8.40 | 201.6 |

1272 | Hellen Keller | 150 | 8.00 | 192.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**

BookNo | Name | Cost | Discount | Final Cost |
---|---|---|---|---|

9871 | Nancy Drew | 200 | 12.50 | 2500 |

9560 | Goosebump | 250 | 30.00 | 7500 |

9810 | Sherlock Holmes | 300 | 52.50 | 15750 |

8700 | The Blue Umbrella | 200 | 15.00 | 3000 |

5086 | Gulliver Travels | 160 | 8.40 | 1344 |

1272 | Hellen Keller | 150 | 8.00 | 1200 |

### 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**

BookNo | Name | Cost | Discount | Final Cost |
---|---|---|---|---|

9871 | Nancy Drew | 200 | 12.50 | 16.00 |

9560 | Goosebump | 250 | 30.00 | 8.33 |

9810 | Sherlock Holmes | 300 | 52.50 | 5.71 |

8700 | The Blue Umbrella | 200 | 15.00 | 13.33 |

5086 | Gulliver Travels | 160 | 8.40 | 19.04 |

1272 | Hellen Keller | 150 | 8.00 | 18.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**

BookNo | Name | Cost | Discount | Final Cost |
---|---|---|---|---|

9871 | Nancy Drew | 200 | 12.50 | 0 |

9560 | Goosebump | 250 | 30.00 | 10 |

9810 | Sherlock Holmes | 300 | 52.50 | 37.5 |

8700 | The Blue Umbrella | 200 | 15.00 | 5 |

5086 | Gulliver Travels | 160 | 8.40 | 0.4 |

1272 | Hellen Keller | 150 | 8.00 | 6 |

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.