Wednesday, April 24, 2013

Arithmetic Expressions in SQL



=> What is Arithmetic in SQL?
  
   Arithmetic in SQL includes

    + Addition
    -  Subtraction
    * Multiplication
    / Division

 We use arithmetic operators in any clause of a SQL statement except the from clause 

=> Using arithmetic expressions in select statement

 For example assume if we want to calculate salary increase by 400 you will write below select statement

SELECT EMPNO, DEPT, SAL, SAL+400
FROM EMP;


EMPNO    SAL     SAL+400
-------    ---------- ----------
   7369        800        1200
   7499       1600       2000
   7521       1250       1650
   7566       2975       3375
   7654       1250       1650
   7698       2850       3250
   7782       2450       2850

=>Rules of Precedence:

In SQL statements, arithmetic expressions are evaluated according to a standard order of operations.
Multiplication and division take priority over addition and subtraction.
If operators within an expression are of the same priority then the expression is evaluated from left to right.

=> Using Parentheses:

We use parentheses to change the order of evolution.    

EXAMPLE:

4*(5+5)=40

(5+5) Evaluate first
4*     Evaluate second

=>Arithmetic Expression with NULL Values

If we perform arithmetic on null values, the result is NULL

 EXAMPLE

10+NULL=NULL
10- NULL=NULL
NULL*10=NULL
NULL/10=NULL
10+0=10
10-0=10
0*10=10
0/10=10



No comments:

Post a Comment

Thank You For Comment