Tuesday 28 May 2013

Operator Precedence in SELECT statement


*      /     +     -

  • Multiplication and division take priority over addition and subtraction
  • Operators of the same priority are evaluated from left to right 
  • Parentheses are used to force prioritized evaluation and to clarify statements
Operator Precedence with example

SELECT ename,sal,12*sal+100 
FROM emp;

SELECT ename,sal,12*(sal+100)
FROM emp;


In the above examples parentheses makes a lot of differences in arithmetic calculation.



Arithmetic Expressions in SELECT statement

You may need to modify the way in which data is displayed, perform calculations, etc.

   Operator       Description


     +                     Add
      -                   Subtract
      *                  Multiply
      /                   Divide

Using Arithmetic Operators 

If we need to show Annual salary of employes then;


Another example:


Selecting Specific Columns




In the above example we 've selected only 3 columns from the table.

Errors: 

If we want to select column that is not in that table then SQL returns an error. 
Example:







Basic Select Statement or Selecting ALL Columns

Syntax of SELECT Statement


SELECT *|{[DISTINCT] column|expression [alias],....}
FROM table;

> SELECT identifies what columns
> FROM identifies which table

Selecting All Columns

SELECT * 
FROM dept;

Result: