Learn to Code SQL Example – SQL | Arithmetic Operators

(SQL Example for Citizen Data Scientist & Business Analyst)

 

SQL | Arithmetic Operators

Arithmetic Operators are:

+           [Addition]
-           [Subtraction]
/           [Division]
*           [Multiplication]
%           [Modulus]

Addition (+) :

It is used to perform addition operation on the data items, items include either single column or multiple columns.

Implementation:

   AS "salary + 100" FROM addition;

Output:

EMPLOYEE_ID EMPLOYEE_NAME SALARY SALARY+100
1 alex 25000 25100
2 rr 55000 55100
3 jpm 52000 52100
4 ggshmr 12312 12412

Here we have done addition of 100 to each Employee’s salary i.e, addition operation on single column.

Let’s perform addition of 2 columns:

SELECT employee_id, employee_name, salary, salary + employee_id
   AS "salary + employee_id" FROM addition;

Output:

EMPLOYEE_ID EMPLOYEE_NAME SALARY SALARY+EMPLOYEE_ID
1 alex 25000 25001
2 rr 55000 55002
3 jpm 52000 52003
4 ggshmr 12312 12316

Here we have done addition of 2 columns with each other i.e, each employee’s employee_id is added with its salary.

Subtraction (-) :

It is use to perform subtraction operation on the data items, items include either single column or multiple columns.

Implementation:

SELECT employee_id, employee_name, salary, salary - 100
    AS "salary - 100" FROM subtraction;

Output:

EMPLOYEE_ID EMPLOYEE_NAME SALARY SALARY-100
12 Finch 15000 14900
22 Peter 25000 24900
32 Warner 5600 5500
42 Watson 90000 89900

Here we have done subtraction of 100 to each Employee’s salary i.e, subtraction operation on single column.

Let’s perform subtraction of 2 columns:

SELECT employee_id, employee_name, salary, salary - employee_id
    AS "salary - employee_id" FROM subtraction;

Output:

EMPLOYEE_ID EMPLOYEE_NAME SALARY SALARY – EMPLOYEE_ID
12 Finch 15000 14988
22 Peter 25000 24978
32 Warner 5600 5568
42 Watson 90000 89958

Here we have done subtraction of 2 columns with each other i.e, each employee’s employee_id is subtracted from its salary.

Multiplication (*) :

It is use to perform multiplication of data items.

Implementation:

SELECT employee_id, employee_name, salary, salary * 100
    AS "salary * 100" FROM addition;

Output:

EMPLOYEE_ID EMPLOYEE_NAME SALARY SALARY * 100
1 Finch 25000 2500000
2 Peter 55000 5500000
3 Warner 52000 5200000
4 Watson 12312 1231200

Here we have done multiplication of 100 to each Employee’s salary i.e, multiplication operation on single column.

Let’s perform multiplication of 2 columns:

     AS "salary * employee_id" FROM addition;

Output:

EMPLOYEE_ID EMPLOYEE_NAME SALARY SALARY * EMPLOYEE_ID
1 Finch 25000 25000
2 Peter 55000 110000
3 Warner 52000 156000
4 Watson 12312 49248

Here we have done multiplication of 2 columns with each other i.e, each employee’s employee_id is multiplied with its salary.

Modulus ( % ) :

It is use to get remainder when one data is divided by another.

Implementation:

SELECT employee_id, employee_name, salary, salary % 25000
    AS "salary % 25000" FROM addition;

Output:

EMPLOYEE_ID EMPLOYEE_NAME SALARY SALARY % 25000
1 Finch 25000 0
2 Peter 55000 5000
3 Warner 52000 2000
4 Watson 12312 12312

Here we have done modulus of 100 to each Employee’s salary i.e, modulus operation on single column.

Let’s perform modulus operation between 2 columns:

SELECT employee_id, employee_name, salary, salary % employee_id
    AS "salary % employee_id" FROM addition;

Output:

EMPLOYEE_ID EMPLOYEE_NAME SALARY SALARY % EMPLOYEE_ID
1 Finch 25000 0
2 Peter 55000 0
3 Warner 52000 1
4 Watson 12312 0

Here we have done modulus of 2 columns with each other i.e, each employee’s salary is divided with its id and corresponding remainder is shown.

Basically, modulus is use to check whether a number is Even or Odd. Suppose a given number if divided by 2 and gives 1 as remainder, then it is an odd number or if on dividing by 2 and gives 0 as remainder, then it is an even number.

Concept of NULL :

If we perform any arithmetic operation on NULL, then answer is always null.

Implementation:

SELECT employee_id, employee_name, salary, type, type + 100
    AS "type+100" FROM addition;

Output:

EMPLOYEE_ID EMPLOYEE_NAME SALARY TYPE TYPE + 100
1 Finch 25000 NULL NULL
2 Peter 55000 NULL NULL
3 Warner 52000 NULL NULL
4 Watson 12312 NULL NULL

Here output always came null, since performing any operation on null will always result in a null value.

 

How to perform JOIN and MERGE in Pandas DataFrame in Python

 

Learn to Code SQL Example – SQL | Arithmetic Operators

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!