Arithmetic Operators
In SQL operations like addition, subtraction, multiplication and division on the data present in the table is done with the help of these operators ( +, -, * , / )
- Addition ( + ).
- Subtraction ( - ).
- Multiplication ( * ).
- Division ( / ).
let's us understand the working of each of these operators with the help of some examples ( Queries in MySQL).
Note: These Operations Have been performed on ORACLE 11g SE
1. Write a query to display all employees with the monthly increase of the salary 400.
Answer: SQL> SELECT ENAME, SAL, SAL+400 FROM EMP;
ENAME SAL SAL+400
---------- ---------- ----------
SMITH 800 1200
ALLEN 1600 2000
WARD 1250 1650
JONES 2975 3375
MARTIN 1250 1650
BLAKE 2850 3250
CLARK 2450 2850
SCOTT 3000 3400
KING 5000 5400
TURNER 1500 1900
ADAMS 1100 1500
ENAME SAL SAL+400
---------- ---------- ----------
JAMES 950 1350
FORD 3000 3400
MILLER 1300 1700
14 rows selected.
2. WRITE A QUERY TO DISPLAY ALL EMPLOYEES WITH THEIR ANNUAL SALARY.
Answer: SQL> SELECT ENAME, SAL, SAL*12 AS ANNUAL_SAL FROM EMP;
ENAME SAL ANNUAL_SAL
---------- ---------- ----------
SMITH 800 9600
ALLEN 1600 19200
WARD 1250 15000
JONES 2975 35700
MARTIN 1250 15000
BLAKE 2850 34200
CLARK 2450 29400
SCOTT 3000 36000
KING 5000 60000
TURNER 1500 18000
ADAMS 1100 13200
ENAME SAL ANNUAL_SAL
---------- ---------- ----------
JAMES 950 11400
FORD 3000 36000
MILLER 1300 15600
3. WRITE A QUERY TO DISPLAY ALL EMPLOYEES WITH AN ANNUAL INCREASE OF 400 IN THEIR SALARY;
Answer: SQL> SELECT ENAME,SAL,SAL*12+400 AS SALARY_WITH_INCREMENT FROM EMP;
ENAME SAL SALARY_WITH_INCREMENT
---------- ---------- -------------------------
SMITH 800 10000
ALLEN 1600 19600
WARD 1250 15400
JONES 2975 36100
MARTIN 1250 15400
BLAKE 2850 34600
CLARK 2450 29800
SCOTT 3000 36400
KING 5000 60400
TURNER 1500 18400
ADAMS 1100 13600
ENAME SAL NEW_SALARY_WITH_INCREMENT
---------- ---------- -------------------------
JAMES 950 11800
FORD 3000 36400
MILLER 1300 16000
14 rows selected.
4. WRITE A QUERY TO DISPLAY EMPLOYEES WHO ARE SALESMAN WITH AN ANNUAL INCREASE OF 400 IN THEIR SALARY;
ANSWER : SQL> SELECT ENAME,SAL, SAL*12+400 FROM EMP
2 WHERE JOB = 'SALESMAN';
ENAME SAL SAL*12+400
---------- ---------- ----------
ALLEN 1600 19600
WARD 1250 15400
MARTIN 1250 15400
TURNER 1500 18400
BONUS QUESTION:
5. SQL> SELECT ENAME, SAL+400+NULL FROM EMP; what will be the output ?
ENAME SAL+400+NULL
---------- ------------
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
ENAME SAL+400+NULL
---------- ------------
JAMES
FORD
MILLER
14 rows selected.
No comments:
Post a Comment