SQL Exercise 7
ARITHMETIC OPERATORS: +, -, /, *
There are four basic arithmetic functions; plus(+), minus(-), divide(/) and multiply(*). Their use is straightforward. If you want to perform a calculation the format is as follows:
SQL> SELECT <column_name> <math operator> <value or column_name>
FROM <table_name>;
A few illustrations:
SQL> SELECT NAME, SALARY, COMM, SALARY + COMM ç plus operator
FROM EMPLOYEE;
The results table will look like this:
|
NAME |
SALARY |
COMM |
SALARY + COMM |
|
Wilson |
1700 |
|
|
|
Smith |
2500 |
1300 |
3800 |
|
Reed |
3500 |
|
|
|
Watson |
4500 |
0 |
4500 |
|
Allen |
3800 |
8000 |
11800 |
|
Turner |
1800 |
|
|
|
Chen |
2900 |
|
|
|
Ramirez |
3650 |
|
|
|
McDonnel |
1625 |
|
|
|
Simpson |
825 |
|
|
|
Di Salvo |
2700 |
|
|
|
Schwartz |
4250 |
5300 |
9550 |
It is important to note that in the results table a column titled SALARY + COMM will be returned as specified in the SELECT clause. This column is not physically stored in the database but rather it is calculated as a result of the query.
Note that some of the 'SALARY+COMM' columns are empty. Why? If every employee in the table has a value for salary why didn't it carry over to the 'SALARY+COMM' column? Because the 'COMM' column is defined as null which does not mean zero. The total could not be calculated unless a numeric value had existed within the COMM column. Look at Watson, his commission was equal to zero so zero plus his salary was put into the SALARY+COMM column.
If for example the value carried in the salary column is a monthly figure and you wanted an annual figure the following query would accomplish that by using the multiply operator:
SQL> SELECT NAME, SALARY * 12 ç multiply operator on a column
FROM EMPLOYEE;
The results table should look like this:
|
NAME |
SALARY*12 |
|
Wilson |
20400 |
|
Smith |
30000 |
|
Reed |
42000 |
|
Watson |
54000 |
|
Allen |
45600 |
|
Turner |
21600 |
|
Chen |
34800 |
|
Ramirez |
43800 |
|
McDonnel |
19500 |
|
Simpson |
9900 |
|
Di Salvo |
32400 |
|
Schwartz |
51000 |
For you to try:
Go here to check your answers.
Last revised 10/21/1997 by J. Warren, Kennesaw State University
Department of Computer Science and Information Systems
![]()
Return to Oracle/SQL Tutorial Contents Page | Return to Exercise 6 | Go to SQL Exercise 8 - Group Functions