Oracle 12c Chapter 4
Oracle 12c Chapter 4
Oracle 12c
Database
Chapter 4 Functions
Using Single-Row
And Aggregate
Functions
Functions
Functions are programs that take zero or more arguments and return a single value.
Oracle has built a number of functions into SQL, and these functions can be called from
SQL statements.
Single-row functions operate on expressions derived from columns or literals, and they
are executed once for each row retrieved.
Functions are a very powerful feature of SQL. They can be used to do the following:
Perform calculations on data
Modify individual data items
Manipulate output for groups of rows
Format dates and numbers for display
Convert column data types
SQL functions sometimes take arguments and always return a value.
Function Classifications
Single-row functions
Multiple-row functions
Single-Row Functions
These functions operate on single rows only and return one result per row.
Manipulate data items
Accept arguments and return one value
Act on each row that is returned
Return one result per row
May modify the data type
Can be nested
Accept arguments that can be a column or an expression
Types of single-row functions
There are different types of single-row functions. This lesson covers the following
ones:
Types of single-row functions
• Character functions: Accept character input and can return both character and number values
• Date functions: Operate on values of the DATE data type (All date functions return a value of the DATE
data type except the MONTHS_BETWEEN function, which returns a number.)
• Conversion functions: Convert a value from one data type to another datatype
• General functions:
• NVL
• NVL2
• NULLIF
• COALESCE
• CASE
• DECODE
Character Functions
• Single-row character functions accept character data as input and can return both
character and numeric values.
Case-conversion functions
Character-manipulation functions
Case-Conversion Functions
• These functions convert the case for character strings:
LOWER: Converts mixed-case or uppercase character strings to lowercase
UPPER: Converts mixed-case or lowercase character strings to uppercase
INITCAP: Converts the first letter of each word to uppercase and the
remaining letters to lowercase
Function Result
LOWER('SQL Course') sql course
UPPER('SQL Course') SQL COURSE
INITCAP('SQL Course') Sql Course
Character-manipulation functions
• These functions manipulate character strings:
Function Result
CONCAT('Hello', 'World') HelloWorld
SUBSTR('HelloWorld',1,5) Hello
LENGTH('HelloWorld') 10
INSTR('HelloWorld', 'W') 6
LPAD(salary,10,'*') *****24000
1 2 3
Number Functions
Number functions accept numeric input and return numeric values
ROUND: Rounds value to a specified decimal
Function Result
ROUND(45.926, 2) 45.93
TRUNC(45.926, 2) 45.92
MOD(1600, 300) 100
Using the ROUND Function
DUAL is a dummy table that you can use to view results from functions and calculations
and owned by the user SYS and can be accessed by all users.
1 2
1 2 3
Using the TRUNC Function
• The TRUNC function truncates the column, expression, or value to n decimal
places.
• The TRUNC function works with arguments similar to those of the ROUND
function. 1 2
1 2 3
Using the MOD Function
• The MOD function finds the remainder of the first argument divided by the second argument.
RR Date Format
CurrentYear
Current Year Specified Date RR Format YY Format
1995 27-OCT-95 1995 1995
1995 27-OCT-17 2017 1917
2001 27-OCT-17 2017 2017
2001 27-OCT-95 1995 2095
Working with Dates
CurrentYear
Current Year Specified Date RR Format YY Format
1995 27-OCT-95 1995 1995
1995 27-OCT-17 2017 1917
2001 27-OCT-17 2017 2017
2001 27-OCT-95 1995 2095
0–49 50–99
If two digits The return date is in The return date is in
of the 0–49 the current century the century before
current the current one
year are: The return date is The return date is in
50–99 in the century after the current century
the current one
Using the SYSDATE Function
• SYSDATE is a date function that returns the current database server date and time.
• You can use SYSDATE just as you would use any other column name. SELECT sysdate
FROM dual;
• Add or subtract a number to or from a date for a resultant date value.
• Subtract two dates to find the number of days between those dates.
Function Result
MONTHS_BETWEEN Number of months between two dates
• Date functions operate on Oracle dates. All date functions return a value of the DATE data type
MONTHS_BETWEEN(date1, date2): Finds the number of months between date1 and date2.
ADD_MONTHS(date, n): Adds n number of calendar months to date. The value of n must be
NEXT_DAY(date, 'char'): Finds the date of the next specified day of the week ('char') following
date. The value of char may be a number representing a day or a character string.
LAST_DAY(date): Finds the date of the last day of the month that contains date.
Date-Manipulation Functions
SELECT employee_id, hire_date, MONTHS_BETWEEN (SYSDATE, hire_date) TENURE,
ADD_MONTHS (hire_date, 6) REVIEW,
NEXT_DAY (hire_date, 'FRIDAY’),
LAST_DAY(hire_date)
FROM employees
WHERE MONTHS_BETWEEN (SYSDATE, hire_date) < 100;
Function Result
MONTHS_BETWEEN('01-SEP-95','11-JAN-94') 19.6774194
Function Result
ROUND(SYSDATE,'MONTH') 01-AUG-03
ROUND(SYSDATE ,'YEAR') 01-JAN-04
TRUNC(SYSDATE ,'MONTH') 01-JUL-03
TRUNC(SYSDATE ,'YEAR') 01-JAN-03
What Are Group
Functions?
Group functions, also called multiple-row functions, return one result per group of rows
processed. Group functions operate on sets of rows to give one result per group.
EMPLOYEES
Maximum salary in
EMPLOYEES table
…
Types of Group Functions
Because these functions return only one result per group of data, they’re also known as
aggregate functions.
AVG
COUNT
MAX
MIN Group
functions
STDDEV
SUM
VARIANCE
SELECT group_function(column), ...
FROM table
[WHERE condition]
[ORDER BY column];
Guidelines for using the group
Thefunctions:
group function is placed after the SELECT keyword.
You may have multiple group functions separated by commas.
DISTINCT makes the function consider only nonduplicate values; ALL
makes it consider every value, including duplicates.
The default is ALL and therefore does not need to be specified.
The data types for the functions with an expr argument may be CHAR,
VARCHAR2, NUMBER, or DATE.
All group functions ignore null values.
To substitute a value for null values, use the NVL, NVL2, or COALESCE
functions.
Using Aggregate Functions
You can use AVG and SUM for numeric data but you can use MIN
and MAX for numeric, character, and date data types.
SELECT AVG(salary), MAX(salary),MIN(salary),SUM(salary)
FROM employees
WHERE job_id LIKE '%REP%';
You can divide rows in a table into smaller groups by using the GROUP BY clause.
SELECT column, group_function(column)
FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[ORDER BY column];
All columns in the SELECT list that are not in group functions must be in the GROUP BY
clause.
…
Grouping by More than One
Column
You can return summary results for groups and subgroups by listing more than one GROUP BY
column.
You can determine the default
SELECT sort order of the
department_id results by
dept_id, the order
job_id, of the columns in the
SUM(salary)
GROUP BY clause.FROM employees
GROUP BY department_id, job_id
ORDER BY department_id;
Illegal Queries Using Group Functions
Any column or expression in the SELECT list that is not an aggregate function must be in the GROUP BY
clause: SELECT department_id, COUNT(last_name)
FROM employees;
A GROUP BY clause must be added to
count the last names for each
department_id.
SELECT MAX(AVG(salary))
Nesting Group FROM employees
Functions: GROUP BY department_id;
THANK YOU