Wednesday, September 9, 2009

Using Arithmetic Operators with SQL SELECT

Using Arithmetic Operators & Expressions :

Create expressions with number and date data by using arithmetic operators
  • + : Add
  • - : Subtract
  • * : Multiply
  • / : Divide

Example :

postgres=# select ename, sal, sal+100 from emp;

ename | sal | ?column?
--------+----------+---------
SMITH | 800.00 | 900.00
ALLEN | 160.00 | 1700.00
WARD | 1250.00 | 1350.00

  • The example in the slide uses the addition operator to calculate a salary increase of $100 for all employees. The slide also displays a SAL+100 column in the output
  • Note that the resultant calculated column SAL+100 is not a new column in the EMP table; it is for display only
Operator Precedence :
  • postgres=# select ename, sal, 12*sal+100 from emp limit 3;
ename | sal | ?column?
-------+---------+----------
SMITH | 800.00 | 9700.00
ALLEN | 1600.00 | 19300.00
WARD | 1250.00 | 15100.00
(3 rows)

No comments:

Post a Comment

Followers