RDBMS and Oracle - Full Book PDF
RDBMS and Oracle - Full Book PDF
IN THIS CHAPTER . . .
BUILT-IN FUNCTIONS
The built-in functions provide a powerful tool for the enhancement of a basic query.
A function takes zero or more arguments and returns a single value. Just like other
software and programming languages, the functions covered in this section are specific
to Oracle. Functions are used for performing calculations on data, converting data,
modifying individual data, manipulating a group of rows, and formatting columns. In
Oracle’s SQL, there are two types of functions:
1. Single-row functions, which work on columns from each row and return
one result per row.
2. Group functions or aggregate functions, which manipulate data in a group
of rows and return single result.
ShahCh06v3.qxd 4/16/04 11:54 AM Page 133
Single-Row Functions
The single-row functions take different types of arguments, work on a data item from
each row, and return one value for each row. The arguments are in the form of a con-
stant value, variable name, column, and/or expression. The value returned by a func-
tion may be of a different type than the argument(s) supplied. The general syntax is
where function is the name of the function, column is a column from a table,
expression is a character string or a mathematical expression, and argument is any
argument used by the function.
There are various types of single-row functions:
UPPER(‘Oracle’) : ‘ORACLE’
LOWER(‘DaTaBaSe SyStEmS’) : ‘database systems’
INITCAP(‘DaTaBaSe SyStEmS’) : ‘Database Systems’
For example, Figure 6-2 shows the use of character functions UPPER, LOWER,
and INITCAP in the SELECT clause to display columns with different cases.
Often, more than one data-entry person will populate a table. One person en-
ters names in all uppercase, and the other uses proper case. This could become a
nightmare for data retrieval query writers if not for functions. Functions are very
useful in the WHERE clause’s conditions as well.
For example, in Figure 6-3, a query is issued with the condition WHERE State
= ‘ny’, and it resulted in a “no row selected” message.The table does contain students
from New York state. The problem here is the case used in entering state values. The
same query is rewritten with condition WHERE UPPER1State2 = ‘NY’, and it re-
turned two student names. The use of the UPPER function converted the value in the
ShahCh06v3.qxd 4/16/04 11:54 AM Page 134
8 rows selected.
SQL>
no rows selected
LAST FIRST
--------------- ---------------
Tyler Mickey
Lee Brian
SQL>
column to uppercase, and it was then compared to the value NY, which has same case.
The condition can be written as WHERE LOWER 1State2 = ‘ny’ instead.
A character function is used in various SELECT clauses, including the
ORDER BY clause. The LENGTH function returns the length of a character col-
umn or string literal. Suppose we want to see names in ascending order by length of
names. The clause will use ORDER BY LENGTH(Last) instead of just ORDER BY
Last, as shown in Figure 6-4.
LAST FIRST
--------------- ---------------
Lee Brian
Diaz Jose
Khan Amir
Tyler Mickey
Patel Rajesh
Rickles Deborah
6 rows selected.
SQL>
INSTR(‘abcdefg’, ‘d’) : 4
LTRIM(‘00022345’, ‘0’) : ‘22345’
RTRIM(‘0223455’, ‘5’) : ‘02234’
TRIM(‘ ‘FROM’ Monroe’) : ‘Monroe’
LENGTH(‘Oracle9i’) : 8
LPAD(265000, 9, ‘$’) : $$$265000
RPAD(265000, 9, ‘*’ ) : 265000***
REPLACE(‘Basketball’, ‘ket’, ‘e’) : ‘Baseball’
where we are looking for string ‘OR’ and function is asked to start at the first char-
acter from the left to look for the second occurrence of the string. The result is 5, as
the second ‘OR’ starts at position 5. If argument 1 is changed to -3, the function will
start at the third character from right and search in the reverse direction.
Numeric Functions. The numeric functions take numeric value(s) and re-
turn a numeric value. The ROUND function rounds the value, expression, or col-
umn to n decimal places. If n is omitted, zero decimal place is assumed. If n is
negative, rounding takes place to the left side of the decimal place. For example,
ROUND(25.465, 2) = 25.47
ROUND(25.465, 0) = 25
ROUND(25.465, –1) = 30
TRUNC(25.465, 2) = 25.46
TRUNC(25.465, 0) = 25
TRUNC(25.465, –1) = 20
The POWER function finds the power of a number 1np2. For example,
POWER(2, 4) = 16
POWER(5, 3) = 125
The ABS function returns the absolute value of a column, expression, or value.
For example,
ABS( –10) = 10
ShahCh06v3.qxd 4/16/04 11:54 AM Page 137
The MOD function finds the integer remainder of x divided by y. It ignores the
quotient. For example,
MOD(5, 2) = 1
MOD(3, 5) = 3
MOD(8, 4) = 0
The SIGN function returns -1 for a negative number, 1 for a positive number,
and 0 for a zero. For example,
SIGN(-50) = –1
SIGN(+43) = 1
SIGN(0) = 0
The FLOOR function is similar to the TRUNC function, and the CEIL func-
tion is similar to the ROUND function. However, both take one argument instead
of two. For example,
FLOOR(54.7) = 54
CEIL(54.7) = 55
There is a difference in CEIL function, because it always returns the next higher in-
teger value. For example,
ROUND (54.3) = 54
CEIL (54.3) = 55
Figure 6-5 shows the use of numeric functions, and Figure 6-6 explains their
use. Did you notice the table name in Figure 6-5? The table is called DUAL, which
is provided by Oracle. The DUAL table is owned by user SYS, and it is available to
all users. The DUAL table is useful when you want to find the outcome of a function
and the argument is not taken from any table. The DUAL table can also be used
perform arithmetic expressions. For example,
The DUAL table contains a single column called DUMMY and a single row with
value X (see Fig. 6-7).
Date Functions. We already know that Oracle stores dates internally with
day, month, year, century, hour, minute, and second information.The default date display
format is DD-MON-YY.There is a very useful date function called SYSDATE that does
not take any arguments. SYSDATE returns the system’s current date. For example,
SELECT SYSDATE
FROM DUAL;
ShahCh06v3.qxd 4/16/04 11:54 AM Page 138
SQL>
D
-
X
SQL>
This query will display the current date.You can get more information about day, date
and time by using a format mask with SYSDATE function. Try the following state-
ment, which will make more sense after format masks are explained in Fig. 6-15:
SELECT TO_CHAR(SYSDATE, ’DY, MONTH DD, YYYY HH:MI:SS P.M.’)
FROM DUAL;
ShahCh06v3.qxd 4/16/04 11:54 AM Page 139
Similarly, the DUAL table can be used to display the outcome of any character and
number functions or an arithmetic expression.
The date-type column is very important. You can derive a lot of information
from date columns by performing “date arithmetic.” As you see in Figure 6-8, you
can add or subtract a number of days to or from a date to get a new resulting date.
You can also add a number of hours to a date. If you have two dates, you can find the
gap in days between them.
6 rows selected.
SQL>
To take leap years into consideration, we can divide by 365.25 days instead of by
365. The resulting age has a decimal value.
You can truncate (with the TRUNC function) the result to zero decimal places,
and the age will be a whole number. The modified expression will look like this:
TRUNC((SYSDATE – BirthDate) / 365.25)
ShahCh06v3.qxd 4/16/04 11:54 AM Page 140
The function NEXT_DAY returns the next occurrence of a day of the week
following the date supplied. The second argument could be a number in quotes or a
day of the week. For example,
NEXT_DAY (‘14-OCT-03’, ‘SUNDAY’) → ‘19-OCT-03’
NEXT_DAY(‘14-OCT-03’, ‘TUESDAY’) ‘21-OCT-03’
The function LAST_DAY finds the last date of the month for the date sup-
plied as an argument. If something is due by the end of this month, what is that
date? For example,
LAST_DAY(‘05-FEB-04’) → ’29-FEB-04’
ShahCh06v3.qxd 4/16/04 11:54 AM Page 141
The ROUND function rounds a date based on the format specified. If a for-
mat is missing, rounding is to the nearest day. For example,
ROUND(TO_DATE(‘07/20/03’, ‘MM/DD/YY’), ‘MONTH’) → ‘01-AUG-03’
Here, the date is nearer to August 1 than to July 1. In the next example, the
date is nearest to the first of next year:
The TRUNC function truncates the date to the nearest format specified. Trun-
cation to the nearest month returns the first day of the date’s month, and truncation
to the nearest year returns the January 1 of the date’s year. For example,
The EXTRACT function extracts year, month, or day from a date value.For
example,
The following is a list of a few more date- and time-related functions intro-
duced in Oracle9i:
NVL(Commission, 0)
NVL(HireDate, ‘01-JAN-03’)
NVL(PreReq, ‘None’)
ShahCh06v3.qxd 4/16/04 11:54 AM Page 142
8 rows selected.
SQL>
An extension of the NVL function is the NVL2 function. It takes three param-
eters instead of the two parameters used by NVL function. The NVL2 function
checks for null as well as not null values. If the column has a not null value, the sec-
ond parameter is displayed. If the column has a null value, the third parameter is dis-
played. The general syntax is
NVL2(column, notnullvalue, nullvalue)
For example,
NVL2(PreReq, ‘YES’, ‘NO’)
For example,
In other words, if the commission value is not null, then display it. If commission value
is null, then display salary value. If salary value is null, then display -1.
The NULLIF function generates null values. First, it compares two expressions.
Then, if their values are equal, it generates a null, or it returns the first expression.
The general syntax is
NULLIF(exp1, exp2)
For example,
NULLIF(Supervisor, 111)
If Supervisor is equal to 111, then a null is displayed; otherwise, the supervisor’s value
is displayed.
The DECODE function is a conditional statement type of function. If you are fa-
miliar with any programming language like Visual Basic 6 (If Á ElseIf or Select Á
Case structures) or C (if Á else if or switch Á case structures), you will understand
the function with ease. The DECODE function tests a column or expression and for
each of its matching value, provides an action. The general syntax is
The default action is provided for any value that does not match the values
checked within the function. If the default value is not used, a null value is returned
for nonmatching values. For example, we are displaying new salary for all employees
based on their PositionId. PositionId 1 gets a 20% raise, 2 gets 15%, 3 gets 10%, 4
gets 5%, and others get no increment at all. If the last default salary is not included
in the statement, the new salary for employees with PositionId 5 is displayed as null.
8 rows selected.
SQL>
6 rows selected.
SQL>
As we have been saying, the default date format is set to DD-MON-YY in most
Oracle installations. In Oracle-9i, DD-MON-YYYY also works as a default. If you
enter a birth date as 15-APR-60, it will be stored with year as 2060. That will create
undesired result. If the default format is DD-MON-RR, years 50 to 99 are interpreted
as 1950 to 1999 and years 00 to 49 as 2000 to 2049. If you are not sure about your ses-
sion settings, use a four-digit year with INSERT as well as other SQL statements. In
Figure 6-17, the character date values are converted to date type with the TO_DATE
function and format mask DD-MON-RR. Then, it is changed back to character for-
mat with the TO_CHAR function and format mask YYYY.
19TH 20TH
---- ----
1960 2003
SQL>
TAX DAY O
---------
15-APR-03
SQL>
to nearest month next, the ADD_MONTHS function adds three months to it, the
NEXT_DAY function finds the date on next Tuesday, and finally, seven is added to it.
The result is the tax day, which happens to be my birthday also. (People get gifts on
their birthday, but I always have to send a gift to Uncle Sam on mine!)
Group Functions
The group functions perform an operation on a group of rows and return one result.
Look at the EMPLOYEE and STUDENT tables:
● Who makes the lowest salary?
● Who got the maximum commission?
● What is the company’s total payroll?
● How many students started in the Winter 2003 semester?
It is easy to look through small tables and find answers. In a real-life situation,
however, most tables have thousands or even millions of records. It is efficient to
look through them with simple queries and group functions.
While using the functions described in Figure 6-19, the key words DISTINCT
or ALL can be used before listing the argument in parenthesis. The key word ALL,
SUM (column) Finds the sum of all values in a column; ignores null values.
AVG (column) Finds the average of all values in a column; ignores null
values.
MAX1column ƒ expression2 Finds the maximum value; ignores null values.
MIN1column ƒ expression2 Finds the minimum value; ignores null values.
COUNT1* ƒ column ƒ expression2 Counts the number of rows, including nulls, for *; counts
nonnull values if the column or expression is used as an
argument.
which means use all values (including duplicate values), is the default. The key word
DISTINCT tells the function to use nonduplicate values only.
Let us write a query to find the total, average, highest, and lowest salaries from
the EMPLOYEE table. Figure 6-20 shows the use of group functions on a number
column, Salary.
1 row selected.
SQL>
Now, we will try the MAX and MIN functions on a date field. Which student
from the STUDENT table was born first, and which was born last? Check out
Figure 6-21. The MAX of a date returns the latest date, and the MIN of a date re-
turns the earliest date. If you use the function on a character column, MAX will re-
turn the last name alphabetically, and MIN will return the first name alphabetically.
YOUNGEST OLDEST
--------- ---------
12-DEC-85 20-OCT-70
SQL>
In Figures 6-22 and 6-23, uses of the COUNT function on an entire row and a
column are given. In Figure 6-22, when rows are counted in the EMPLOYEE table,
all eight employees’ rows are counted. In Figure 6-23, when EmployeeId column val-
ues are counted, it returns eight employees. When Commission column values are
counted, the null values are ignored, giving us only five commissioned employees.
We can change that using the NVL function:
COUNT(NVL(Commission,0))
ShahCh06v3.qxd 4/16/04 11:54 AM Page 149
COUNT(*)
----------
8
SQL>
COUNT(EMPLOYEEID) COUNT(COMMISSION)
----------------- -----------------
8 5
SQL>
Null values in Commission columns are replaced with 0 in the query, and the value 8
is returned from the query.
Question: Which of the following queries will return a higher average from
the Commission column?
Answer: The first query, because it adds five commission values and divides the total
by five, whereas the second query divides the total by eight. The output
would be 14600 from the first query but 9125 from the second query.
GROUPING DATA
The rows in a table can be divided into different groups to treat each group sepa-
rately. The group functions can be applied to individual groups in the same fashion
they are applied to all rows. The GROUP BY clause is used for grouping data. The
general syntax is
SELECT column, groupfunction(column)
FROM tablename
[WHERE condition(s)]
[GROUP BY column | expression]
[ORDER BY column | expression [ASC | DESC]];
ShahCh06v3.qxd 4/16/04 11:54 AM Page 150
● When you include a group function and the GROUP BY clause in your
query, the individual column(s) appearing in SELECT must also appear in
GROUP BY.
● The WHERE clause can still be used to restrict data before grouping.
● The WHERE clause cannot be used to restrict groups.
● A column alias cannot be used in a GROUP BY clause.
● The GROUP BY column does not have to appear in a SELECT query.
● When a column is used in the GROUP BY clause, the result is sorted in as-
cending order by that column by default. In other words, GROUP BY has
an implied ORDER BY. You can still use an ORDER BY clause explicitly
to change the implied sort order.
● In Oracle9i, the order of the WHERE and GROUP BY clauses in the SELECT
query does not matter, but traditionally, the WHERE clause is written before
the GROUP BY clause.
In the next few figures, you will see the effect of a GROUP BY clause on queries
with group functions.
As you see in Figure 6-24, the DeptId column is automatically sorted, because it
is used in the GROUP BY clause.The DeptId column is not necessary in the SELECT
clause, but it is a good idea to include it so the counts make sense.
DEPTID # of Emp
------- -------
10 3
20 2
30 2
40 1
SQL>
What will happen if the query in Figure 6-24 is typed without a GROUP BY
clause? If the SELECT clause contains only a group function, it does not matter.
The query in the figure, in fact, has the SELECT clause with a column and a group
function. When a column appears in the SELECT clause along with the group func-
tion, the column must appear in the GROUP BY clause. Failure to do so results in
error ORA-00937 (see Fig. 6-25).
ShahCh06v3.qxd 4/16/04 11:54 AM Page 151
ERROR at line 1:
ORA-00937: not a single-group group function
SQL>
Can we use a condition in the WHERE clause that contains a group function?
The WHERE clause can be used to restrict rows, but it cannot be used to restrict
groups, as you see in Figure 6-26. In this figure, we are trying to see buildings that
have four or more rooms. The error ORA-00934 states that the group function is not
allowed in the WHERE clause. We can fix this problem with a new, HAVING
clause, which is used for restricting groups, as you will see shortly.
ERROR at line 3:
ORA-00934: group function is not allowed here
SQL>
HAVING Clause
The HAVING clause can restrict groups. The WHERE clause restricts rows, the
GROUP BY clause groups remaining rows, the Group function works on each
group, and the HAVING clause keeps the groups that match the group condition.
In the sample query (see Fig. 6-27), the WHERE clause filters out the building
named Kennedy, the rest of the rows are grouped by the building names Gandhi and
Nehru, the group function COUNT counts the number of rows in each group, and
the HAVING clause keeps groups with four or more rows—that is, the Gandhi
building with five rows/rooms.
The implied ascending sort with the GROUP BY clause can be overriden by
adding an explicit ORDER BY clause to the query. Figure 6-28 shows outcome sorted
in ascending order by Building column with the GROUP BY clause, then it is re-
versed to descending order by inserting the ORDER BY clause in line 5.
ShahCh06v3.qxd 4/16/04 11:54 AM Page 152
BUILDIN ROOMS
------- ------
Gandhi 5
1 row selected.
SQL>
BUILDIN COUNT(*)
------- --------
Gandhi 5
Kennedy 4
SQL> i
5 ORDER BY Building DESC;
BUILDIN COUNT(*)
------- --------
Kennedy 4
Gandhi 5
SQL>
Let us look at another example of GROUP BY and HAVING. Let us find em-
ployees who have more than two dependents. We will use the DEPENDENT table
and GROUP BY EmployeeId column, find COUNT of DependentId, and also
check for COUNT higher than 2:
The single-row functions can be nested to many levels, but the group functions can
only be nested to two levels. For example,
SELECT SUM(MaxCount) FROM crssection GROUP BY CourseId;
will find the total available seats for each CourseId. If you use this output for an
outer function in a nested scenario as follows, you will get a different answer:
SELECT MAX(SUM(MaxCount)) FROM crssection GROUP BY CourseId;
The answer returned by this query is 85, because the outer query takes totals by
each CourseId and finds the one with the largest value.
IN A NUTSHELL . . .
EXERCISE QUESTIONS
True/False:
1. A single-row function may be used in the SELECT clause, but it is not allowed in the
WHERE clause.
2. The SYSDATE function can return the current date but not the current time.