Oracle 12c Chapter 6
Oracle 12c Chapter 6
Oracle 12c
Database
Using Single-Row
And Aggregate
Functions
Single-row functions
Multiple-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
• 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
Case-conversion functions
Character-manipulation functions
Function Result
LOWER('SQL Course') sql course
UPPER('SQL Course') SQL COURSE
INITCAP('SQL Course') Sql Course
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
Function Result
ROUND(45.926, 2) 45.93
TRUNC(45.926, 2) 45.92
MOD(1600, 300) 100
1 2
1 2 3
• The TRUNC function works with arguments similar to those of the ROUND function.
1 2
1 2 3
• 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
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
• 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 except
MONTHS_BETWEEN(date1, date2): Finds the number of months between date1 and date2. The result can
be positive or negative.
ADD_MONTHS(date, n): Adds n number of calendar months to date. The value of n must be an integer
NEXT_DAY(date, 'char'): Finds the date of the next specified day of the week (' char') following date. The
LAST_DAY(date): Finds the date of the last day of the month that contains date.
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
EMPLOYEES
Maximum salary in
EMPLOYEES table
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 receive an error message if you fail to include the column list in the GROUP BY
clause.
Using a WHERE clause, you can exclude rows before dividing them into groups.
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.
SELECT MAX(AVG(salary))
Nesting Group FROM employees
Functions: GROUP BY department_id;