SlideShare a Scribd company logo
Advanced Functions in PL/SQL
Titles
Analytic Functions
LISTAGG Function
TRANSLATE Function
REGEXP_LIKE Condition
REGEXP_COUNT Function
COALESCE Function
EXTRACT Function
ADD_MONTHS Function
INITCAP Function
INSTR Function
GREATEST Function
2/39
Analytic Functions
• It is true that whatever an analytic function does can be done by native SQL, with join
and sub-queries. But the same routine done by analytic function is always faster, or at
least as fast.
• Syntax
analytic_function([ arguments ]) OVER (analytic_clause)
analytic_clause:
[ query_partition_clause ] [ order_by_clause [ windowing_clause ] ]
• An aggregate function aggregates data from several rows into a single result row.
3/39
How are analytic functions different from group or aggregate functions?
Query-1 returns departments and their employee
count. Most importantly it groups the records into
departments in accordance with the GROUP BY
clause. As such any non-"group by" column is not
allowed in the select clause.
Query-2 Though analytic functions give
aggregate result they do not group the result set.
They return the group value multiple times with
each record. As such any other non-"group by"
column or expression can be present in the select
clause.
4/39
How are analytic functions different from group or aggregate functions?
Analytic functions are computed after all joins, WHERE clause, GROUP BY and HAVING are
computed on the query. The main ORDER BY clause of the query operates after the analytic
functions. So analytic functions can only appear in the select list and in the main ORDER BY clause of
the query.
In absence of any PARTITION or <window_clause> inside the OVER( ) portion, the function acts on
entire record set returned by the where clause.
5/39
Analytic Functions(order-by-clause)
• The order_by_clause is used to order rows, or siblings, within a partition. So if an
analytic function is sensitive to the order of the siblings in a partition you should
include an order_by_clause.
• ORDER BY <sql_expr> [ASC or DESC] NULLS [FIRST or LAST]
• The functions SUM, COUNT, AVG, MIN, MAX are the common analytic functions the
result of which does not depend on the order of the records.
• Functions like LEAD, LAG, RANK, DENSE_RANK, ROW_NUMBER, FIRST, FIRST
VALUE, LAST, LAST VALUE depends on order of records.
6/39
ROW_NUMBER, RANK and DENSE_RANK
All the above three functions assign integer
values to the rows depending on their order.
ROW_NUMBER( ) gives a running serial number
to a partition of records. It is very useful in
reporting, especially in places where different
partitions have their own serial numbers.
7/39
ROW_NUMBER, RANK and DENSE_RANK
RANK and DENSE_RANK both provide rank to
the records based on some column value or
expression. In case of a tie of 2 records at
position N, RANK declares 2 positions N and
skips position N+1 and gives position N+2 to the
next record. While DENSE_RANK declares 2
positions N but does not skip position N+1.
8/39
LEAD and LAG
LEAD has the ability to compute an expression on the next rows (rows which are going to
come after the current row) and return the value to the current row.
• Syntax
LEAD (<sql_expr>, <offset>, <default>) OVER (<analytic_clause>)
<sql_expr> is the expression to compute from the leading row.
<offset> is the index of the leading row relative to the current row.
<offset> is a positive integer with default 1.
<default> is the value to return if the <offset> points to a row outside the partition range.
The syntax of LAG is similar except that the offset for LAG goes into the previous rows.
9/39
LEAD and LAG
10/39
FIRST VALUE and LAST VALUE function
• Syntax
FIRST_VALUE(<sql_expr>) OVER
(<analytic_clause>)
The FIRST_VALUE analytic function picks the
first record from the partition after doing the
ORDER BY.
The <sql_expr> is computed on the columns of
this first record and results are returned.
The LAST_VALUE function is used in similar
context except that it acts on the last record of
the partition.
How many days after the first hire of each department were
the next employees hired?
11/39
FIRST and LAST function
The FIRST function (or more properly KEEP FIRST function) is used in a very special situation.
Suppose we rank a group of record and found several records in the first rank. Now we want to apply
an aggregate function on the records of the first rank. KEEP FIRST enables that.
• syntax
Function( ) KEEP (DENSE_RANK FIRST ORDER BY <expr>) OVER (<partitioning_clause>)
Please note that FIRST and LAST are the only functions that deviate from the general syntax of
analytic functions. They do not have the ORDER BY inside the OVER clause. Neither do they support
any <window> clause. The ranking done in FIRST and LAST is always DENSE_RANK.
The LAST function is used in similar context to perform computations on last ranked records.
How each employee's salary compare with the average salary of the first year hires of their department?
12/39
FIRST and LAST function
13/39
Analytic Functions(window-clause)
• This group of rows is known as a window, which is why analytic functions are
sometimes referred to as window[ing] functions.
• We have seen previously the query_partition_clause controls the window, or group of
rows, the analytic operates on. The windowing_clause gives some analytic functions
a further degree of control over this window within the current partition. The
windowing_clause is an extension of the order_by_clause and as such, it can only be
used if an order_by_clause is present.
14/39
Analytic Functions(window-clause)
<window_clause>
• [ROW or RANGE] BETWEEN <start_expr> AND <end_expr>
<start_expr> can be any one of the following
• UNBOUNDED PRECEDING
• CURRENT ROW
• <sql_expr> PRECEDING or FOLLOWING.
<end_expr> can be any one of the following
• UNBOUNDED FOLLOWING
• CURRENT ROW
• <sql_expr> PRECEDING or FOLLOWING.
15/39
Analytic Functions(window-clause)
• For ROW type windows the definition is in terms of row numbers before or after the
current row. So for ROW type windows <sql_expr> must evaluate to a positive
integer.
• For RANGE type windows the definition is in terms of values before or after the
current ORDER.
• The ROW or RANGE window cannot appear together in one OVER clause.
• The window clause is defined in terms of the current row. But may or may not include
the current row.
• The start point of the window and the end point of the window can finish before the
current row or after the current row.
• Only start point cannot come after the end point of the window.
16/39
ROW Type Windows
17/39
RANGE Windows
18/39
LISTAGG Function
• Description
The Oracle/PLSQL LISTAGG function concatenates values of the
measure_column for each GROUP based on the order_by_clause.
• SYNTAX
LISTAGG (measure_column [, 'delimiter']) WITHIN GROUP (order_by_clause)
[OVER (query_partition_clause)]
• measure_column
The column whose values you wish to concatenate together in the result set.
Null values in the measure_column are ignored.
• Delimiter
Optional. It is the delimiter to use when separating the measure_column values
when outputting the results.
19/39
LISTAGG Function
• order_by_clause
It determines the order that the concatenated values (ie: measure_column) are
returned.
• Example
SELECT LISTAGG(product_name, ', ') WITHIN GROUP (ORDER BY product_name)
"Product_Listing" FROM products;
Product_id Product_name
1001 Bananas
1002 Apples
1003 Pears
1004 Oranges
Product_Listing
Apples, Bananas, Oranges, Pears
20/39
TRANSLATE Function
• Description
The Oracle/PLSQL TRANSLATE function replaces a sequence of characters in
a string with another set of characters. However, it replaces a single character
at a time.
For example, it will replace the 1st character in the string_to_replace with the
1st character in the replacement_string. Then it will replace the 2nd character
in the string_to_replace with the 2nd character in the replacement_string, and
so on.
• Syntax
TRANSLATE( string1, string_to_replace, replacement_string )
• string1
The string to replace a sequence of characters with another set of characters.
21/39
TRANSLATE Function
• string_to_replace
The string that will be searched for in string1.
• replacement_string
All characters in the string_to_replace will be replaced with the corresponding
character in the replacement_string.
• Example
TRANSLATE('1tech23', '123', '456') Result: '4tech56'
TRANSLATE('222tech', '2ec', '3it') Result: '333tith'
22/39
REGEXP_LIKE Condition
• Description
The Oracle REGEXP_LIKE condition allows you to perform regular expression
matching in the WHERE clause of a SELECT, INSERT, UPDATE, or DELETE
statement.
• SYNTAX
REGEXP_LIKE ( expression, pattern [, match_parameter ] )
• Expression
A character expression such as a column or field. It can be a
VARCHAR2, CHAR, NVARCHAR2, NCHAR, CLOB or NCLOB data type.
23/39
REGEXP_LIKE Condition
• Pattern
The regular expression matching information.
^ Matches the beginning of a string.
$ Matches the end of a string.
* Matches zero or more occurrences.
| Used like an "OR" to specify more than one alternative.
+ Matches one of more occurrences.
? Matches zero or one occurrence.
. Matches any character except NULL.
[ ] Used to specify a matching list where you are trying to match any one of the characters in the
list.
[^ ] Used to specify a nonmatching list where you are trying to match any character except for the
ones in the list.
24/39
REGEXP_LIKE Condition
• match_parameter
Optional. It allows you to modify the matching behavior for the REGEXP_LIKE
condition.
• Example
SELECT last_name FROM contacts WHERE REGEXP_LIKE (last_name, 'Anders(o|e|a)n');
SELECT last_name FROM contacts WHERE REGEXP_LIKE (last_name, '^A(*)');
SELECT last_name FROM contacts WHERE REGEXP_LIKE (last_name, '(*)n$');
‘c’ Perform case-sensitive matching.
‘i’ Perform case-insensitive matching.
‘n’ Allows the period character (.) to match the newline character.
‘m’ expression is assumed to have multiple lines, where ^ is the start of a line and $ is the end
of a line, regardless of the position of those characters in expression.
‘x’ Whitespace characters are ignored.
25/39
REGEXP_COUNT Function
• Description
The Oracle/PLSQL REGEXP_COUNT function counts the number of times that
a pattern occurs in a string.
• Syntax
REGEXP_COUNT( string, pattern [, start_position [, match_parameter ] ] )
• string
The string to search. string can be CHAR, VARCHAR2, NCHAR,
NVARCHAR2, CLOB, or NCLOB.
• pattern
The regular expression matching information.
• start_position
Optional. It is the position in string where the search will start. If omitted, it
defaults to 1 which is the first position in the string.
• match_parameter
Optional. It allows you to modify the matching behavior for the
REGEXP_COUNT function.
26/39
REGEXP_COUNT Function
• Example
SELECT REGEXP_COUNT ('TechOnTheNet is a great resource!', 't') FROM dual;
Result: 2.
SELECT REGEXP_COUNT ('TechOnTheNet is a great resource!', 't', 1, 'i') FROM dual;
Result: 4
SELECT REGEXP_COUNT ('The example shows how to use the REGEXP_COUNT function.', 'the',
4, 'i') FROM dual;
Result: 1
SELECT REGEXP_COUNT ('Anderson', 'a|e|i|o|u') FROM dual;
Result: 2
SELECT REGEXP_COUNT ('Anderson', 'a|e|i|o|u', 1, 'i') FROM dual;
Result: 3
27/39
COALESCE Function
• Description
The Oracle/PLSQL COALESCE function returns the first non-null expression in
the list. If all expressions evaluate to null, then the COALESCE function will
return null.
• Syntax
COALESCE( expr1, expr2, ... expr_n )
• expr1, expr2, ... expr_n
The expressions to test for non-null values.
28/39
COALESCE Function
• Example
SELECT COALESCE( address1, address2, address3 ) result FROM suppliers;
The above COALESCE function is equivalent to the following IF-THEN-ELSE statement:
IF address1 is not null THEN
result := address1;
ELSIF address2 is not null THEN
result := address2;
ELSIF address3 is not null THEN
result := address3;
ELSE
result := null;
END IF;
29/39
EXTRACT Function
• Description
The Oracle/PLSQL EXTRACT function extracts a value from a date or interval
value.
• Syntax
EXTRACT ( { YEAR | MONTH | DAY | HOUR | MINUTE | SECOND } | {
TIMEZONE_HOUR | TIMEZONE_MINUTE } | { TIMEZONE_REGION |
TIMEZONE_ABBR } FROM { date_value | interval_value } )
• Example
EXTRACT(YEAR FROM DATE '2003-08-22') Result: 2003
EXTRACT(MONTH FROM DATE '2003-08-22') Result: 8
EXTRACT(DAY FROM DATE '2003-08-22') Result: 22
30/39
ADD_MONTHS Function
• Description
The Oracle/PLSQL ADD_MONTHS function returns a date with a specified
number of months added.
• Syntax
ADD_MONTHS( date1, number_months )
• Date1
The starting date (before the n months have been added).
• number_months
The number of months to add to date1.
31/39
ADD_MONTHS Function
• Example
ADD_MONTHS('01-Aug-03', 3)
Result: '01-Nov-03‘
ADD_MONTHS('01-Aug-03', -3)
Result: '01-May-03'
ADD_MONTHS('21-Aug-03', -3)
Result: '21-May-03'
ADD_MONTHS('31-Jan-03', 1)
Result: '28-Feb-03'
32/39
INITCAP Function
• Description
The Oracle/PLSQL INITCAP function sets the first character in each word to uppercase
and the rest to lowercase.
• Syntax
INITCAP( string1 )
• string1
The string argument whose first character in each word will be converted to uppercase and
all remaining characters converted to lowercase.
• Example
INITCAP('tech on the net');
Result: 'Tech On The Net‘
INITCAP('GEORGE BURNS');
Result: 'George Burns'
33/39
INSTR Function
• Description
The Oracle/PLSQL INSTR function returns the location of a substring in a
string.
• Syntax
INSTR( string, substring [, start_position [, nth_appearance ] ] )
• String
The string to search. string can be CHAR, VARCHAR2, NCHAR,
NVARCHAR2, CLOB, or NCLOB.
• Substring
The substring to search for in string. substring can be CHAR, VARCHAR2,
NCHAR, NVARCHAR2, CLOB, or NCLOB.
34/39
INSTR Function
• start_position
Optional. The position in string where the search will start. If omitted, it defaults
to 1. The first position in the string is 1. If the start_position is negative, the
INSTR function counts back start_position number of characters from the end
of string and then searches towards the beginning of string.
• nth_appearance
Optional. The nth appearance of substring. If omitted, it defaults to 1.
Note: If substring is not found in string, then the INSTR function will return 0.
35/39
INSTR Function
• Example
INSTR('Tech on the net', 'e') Result: 2 (the first occurrence of 'e')
INSTR('Tech on the net', 'e', 1, 1) Result: 2 (the first occurrence of 'e')
INSTR('Tech on the net', 'e', 1, 2) Result: 11 (the second occurrence of 'e')
INSTR('Tech on the net', 'e', 1, 3) Result: 14 (the third occurrence of 'e')
INSTR('Tech on the net', 'e', -3, 2) Result: 2
36/39
GREATEST Function
• Description
The Oracle/PLSQL GREATEST function returns the greatest value in a list of
expressions.
• Syntax
GREATEST( expr1 [, expr2, ... expr_n] )
• expr1
The first expression to be evaluated whether it is the greatest.
• expr2, ... expr_n
Optional. Additional expressions that are to be evaluated.
Note:
If the datatypes of the expressions are different, all expressions will be converted to
whatever datatype expr1 is.
If the comparison is based on a character comparison, one character is considered greater
than another if it has a higher character set value.
37/39
GREATEST Function
• Example
GREATEST(2, 5, 12, 3) Result: 12
GREATEST('2', '5', '12', '3') Result: '5'
GREATEST('apples', 'oranges', 'bananas') Result: 'oranges'
GREATEST('apples', 'applis', 'applas') Result: 'applis'
38/39
Name of Functions
select distinct
object_name
from
all_arguments
where
package_name = 'STANDARD';
39/39

More Related Content

PPTX
Database index
PPT
SQL DDL
PDF
Relational algebra in dbms
PDF
Parquet performance tuning: the missing guide
PPTX
Data discretization
PPT
Sql Server Performance Tuning
PPT
Databases: Normalisation
PPTX
Database index
SQL DDL
Relational algebra in dbms
Parquet performance tuning: the missing guide
Data discretization
Sql Server Performance Tuning
Databases: Normalisation

What's hot (20)

PDF
Introduction to DAX Language
PDF
Normalization in SQL | Edureka
PDF
Sql functions
PPT
C++ Arrays
PPTX
The Relational Model
PPTX
Lecture 4 sql {basics keys and constraints}
PPTX
Sql joins
PPTX
AWS (Amazon Redshift) presentation
PPTX
NOSQL Databases types and Uses
PPT
lists-and-links.ppt
PPT
Microsoft Excel VLOOKUP Function
PPTX
Index Structures.pptx
PPTX
Learn Normalization in simple language
PPTX
Intro to DAX Patterns
PPT
Advanced sql
PPTX
Database Performance Tuning
PPT
6 Data Modeling for NoSQL 2/2
PPT
Using vlookup in excel
PPTX
Database Keys & Relationship
Introduction to DAX Language
Normalization in SQL | Edureka
Sql functions
C++ Arrays
The Relational Model
Lecture 4 sql {basics keys and constraints}
Sql joins
AWS (Amazon Redshift) presentation
NOSQL Databases types and Uses
lists-and-links.ppt
Microsoft Excel VLOOKUP Function
Index Structures.pptx
Learn Normalization in simple language
Intro to DAX Patterns
Advanced sql
Database Performance Tuning
6 Data Modeling for NoSQL 2/2
Using vlookup in excel
Database Keys & Relationship
Ad

Similar to Advanced functions in PL SQL (20)

PDF
Oracle_Analytical_function.pdf
ODP
Oracle SQL Advanced
PPT
Analytics ioug 2011
PPSX
Analytic & Windowing functions in oracle
PDF
Sql wksht-3
PPTX
SQL.pptx
DOC
Sql functions
PPT
SQL select statement and functions
PDF
Oracle APEX Cheat Sheet
PPT
Module03
PDF
Dynamic websites lec2
PDF
lab2sql222222222222222222222222222222222
PPT
Advanced Sql Training
PDF
SQL – The Natural Language for Analysis - Oracle - Whitepaper - 2431343
PPT
Olap Functions Suport in Informix
PDF
advance-sqaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal.pdf
PPT
SQL || overview and detailed information about Sql
PPTX
Functions
PPT
Enabling Applications with Informix' new OLAP functionality
PDF
Introduction to oracle functions
Oracle_Analytical_function.pdf
Oracle SQL Advanced
Analytics ioug 2011
Analytic & Windowing functions in oracle
Sql wksht-3
SQL.pptx
Sql functions
SQL select statement and functions
Oracle APEX Cheat Sheet
Module03
Dynamic websites lec2
lab2sql222222222222222222222222222222222
Advanced Sql Training
SQL – The Natural Language for Analysis - Oracle - Whitepaper - 2431343
Olap Functions Suport in Informix
advance-sqaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal.pdf
SQL || overview and detailed information about Sql
Functions
Enabling Applications with Informix' new OLAP functionality
Introduction to oracle functions
Ad

Recently uploaded (20)

PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
ai tools demonstartion for schools and inter college
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
System and Network Administraation Chapter 3
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
System and Network Administration Chapter 2
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
Introduction to Artificial Intelligence
PPTX
Transform Your Business with a Software ERP System
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
top salesforce developer skills in 2025.pdf
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
L1 - Introduction to python Backend.pptx
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
CHAPTER 2 - PM Management and IT Context
How to Choose the Right IT Partner for Your Business in Malaysia
ai tools demonstartion for schools and inter college
Understanding Forklifts - TECH EHS Solution
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Reimagine Home Health with the Power of Agentic AI​
System and Network Administraation Chapter 3
Operating system designcfffgfgggggggvggggggggg
System and Network Administration Chapter 2
2025 Textile ERP Trends: SAP, Odoo & Oracle
Introduction to Artificial Intelligence
Transform Your Business with a Software ERP System
wealthsignaloriginal-com-DS-text-... (1).pdf
top salesforce developer skills in 2025.pdf
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Navsoft: AI-Powered Business Solutions & Custom Software Development
L1 - Introduction to python Backend.pptx
Which alternative to Crystal Reports is best for small or large businesses.pdf
CHAPTER 2 - PM Management and IT Context

Advanced functions in PL SQL

  • 2. Titles Analytic Functions LISTAGG Function TRANSLATE Function REGEXP_LIKE Condition REGEXP_COUNT Function COALESCE Function EXTRACT Function ADD_MONTHS Function INITCAP Function INSTR Function GREATEST Function 2/39
  • 3. Analytic Functions • It is true that whatever an analytic function does can be done by native SQL, with join and sub-queries. But the same routine done by analytic function is always faster, or at least as fast. • Syntax analytic_function([ arguments ]) OVER (analytic_clause) analytic_clause: [ query_partition_clause ] [ order_by_clause [ windowing_clause ] ] • An aggregate function aggregates data from several rows into a single result row. 3/39
  • 4. How are analytic functions different from group or aggregate functions? Query-1 returns departments and their employee count. Most importantly it groups the records into departments in accordance with the GROUP BY clause. As such any non-"group by" column is not allowed in the select clause. Query-2 Though analytic functions give aggregate result they do not group the result set. They return the group value multiple times with each record. As such any other non-"group by" column or expression can be present in the select clause. 4/39
  • 5. How are analytic functions different from group or aggregate functions? Analytic functions are computed after all joins, WHERE clause, GROUP BY and HAVING are computed on the query. The main ORDER BY clause of the query operates after the analytic functions. So analytic functions can only appear in the select list and in the main ORDER BY clause of the query. In absence of any PARTITION or <window_clause> inside the OVER( ) portion, the function acts on entire record set returned by the where clause. 5/39
  • 6. Analytic Functions(order-by-clause) • The order_by_clause is used to order rows, or siblings, within a partition. So if an analytic function is sensitive to the order of the siblings in a partition you should include an order_by_clause. • ORDER BY <sql_expr> [ASC or DESC] NULLS [FIRST or LAST] • The functions SUM, COUNT, AVG, MIN, MAX are the common analytic functions the result of which does not depend on the order of the records. • Functions like LEAD, LAG, RANK, DENSE_RANK, ROW_NUMBER, FIRST, FIRST VALUE, LAST, LAST VALUE depends on order of records. 6/39
  • 7. ROW_NUMBER, RANK and DENSE_RANK All the above three functions assign integer values to the rows depending on their order. ROW_NUMBER( ) gives a running serial number to a partition of records. It is very useful in reporting, especially in places where different partitions have their own serial numbers. 7/39
  • 8. ROW_NUMBER, RANK and DENSE_RANK RANK and DENSE_RANK both provide rank to the records based on some column value or expression. In case of a tie of 2 records at position N, RANK declares 2 positions N and skips position N+1 and gives position N+2 to the next record. While DENSE_RANK declares 2 positions N but does not skip position N+1. 8/39
  • 9. LEAD and LAG LEAD has the ability to compute an expression on the next rows (rows which are going to come after the current row) and return the value to the current row. • Syntax LEAD (<sql_expr>, <offset>, <default>) OVER (<analytic_clause>) <sql_expr> is the expression to compute from the leading row. <offset> is the index of the leading row relative to the current row. <offset> is a positive integer with default 1. <default> is the value to return if the <offset> points to a row outside the partition range. The syntax of LAG is similar except that the offset for LAG goes into the previous rows. 9/39
  • 11. FIRST VALUE and LAST VALUE function • Syntax FIRST_VALUE(<sql_expr>) OVER (<analytic_clause>) The FIRST_VALUE analytic function picks the first record from the partition after doing the ORDER BY. The <sql_expr> is computed on the columns of this first record and results are returned. The LAST_VALUE function is used in similar context except that it acts on the last record of the partition. How many days after the first hire of each department were the next employees hired? 11/39
  • 12. FIRST and LAST function The FIRST function (or more properly KEEP FIRST function) is used in a very special situation. Suppose we rank a group of record and found several records in the first rank. Now we want to apply an aggregate function on the records of the first rank. KEEP FIRST enables that. • syntax Function( ) KEEP (DENSE_RANK FIRST ORDER BY <expr>) OVER (<partitioning_clause>) Please note that FIRST and LAST are the only functions that deviate from the general syntax of analytic functions. They do not have the ORDER BY inside the OVER clause. Neither do they support any <window> clause. The ranking done in FIRST and LAST is always DENSE_RANK. The LAST function is used in similar context to perform computations on last ranked records. How each employee's salary compare with the average salary of the first year hires of their department? 12/39
  • 13. FIRST and LAST function 13/39
  • 14. Analytic Functions(window-clause) • This group of rows is known as a window, which is why analytic functions are sometimes referred to as window[ing] functions. • We have seen previously the query_partition_clause controls the window, or group of rows, the analytic operates on. The windowing_clause gives some analytic functions a further degree of control over this window within the current partition. The windowing_clause is an extension of the order_by_clause and as such, it can only be used if an order_by_clause is present. 14/39
  • 15. Analytic Functions(window-clause) <window_clause> • [ROW or RANGE] BETWEEN <start_expr> AND <end_expr> <start_expr> can be any one of the following • UNBOUNDED PRECEDING • CURRENT ROW • <sql_expr> PRECEDING or FOLLOWING. <end_expr> can be any one of the following • UNBOUNDED FOLLOWING • CURRENT ROW • <sql_expr> PRECEDING or FOLLOWING. 15/39
  • 16. Analytic Functions(window-clause) • For ROW type windows the definition is in terms of row numbers before or after the current row. So for ROW type windows <sql_expr> must evaluate to a positive integer. • For RANGE type windows the definition is in terms of values before or after the current ORDER. • The ROW or RANGE window cannot appear together in one OVER clause. • The window clause is defined in terms of the current row. But may or may not include the current row. • The start point of the window and the end point of the window can finish before the current row or after the current row. • Only start point cannot come after the end point of the window. 16/39
  • 19. LISTAGG Function • Description The Oracle/PLSQL LISTAGG function concatenates values of the measure_column for each GROUP based on the order_by_clause. • SYNTAX LISTAGG (measure_column [, 'delimiter']) WITHIN GROUP (order_by_clause) [OVER (query_partition_clause)] • measure_column The column whose values you wish to concatenate together in the result set. Null values in the measure_column are ignored. • Delimiter Optional. It is the delimiter to use when separating the measure_column values when outputting the results. 19/39
  • 20. LISTAGG Function • order_by_clause It determines the order that the concatenated values (ie: measure_column) are returned. • Example SELECT LISTAGG(product_name, ', ') WITHIN GROUP (ORDER BY product_name) "Product_Listing" FROM products; Product_id Product_name 1001 Bananas 1002 Apples 1003 Pears 1004 Oranges Product_Listing Apples, Bananas, Oranges, Pears 20/39
  • 21. TRANSLATE Function • Description The Oracle/PLSQL TRANSLATE function replaces a sequence of characters in a string with another set of characters. However, it replaces a single character at a time. For example, it will replace the 1st character in the string_to_replace with the 1st character in the replacement_string. Then it will replace the 2nd character in the string_to_replace with the 2nd character in the replacement_string, and so on. • Syntax TRANSLATE( string1, string_to_replace, replacement_string ) • string1 The string to replace a sequence of characters with another set of characters. 21/39
  • 22. TRANSLATE Function • string_to_replace The string that will be searched for in string1. • replacement_string All characters in the string_to_replace will be replaced with the corresponding character in the replacement_string. • Example TRANSLATE('1tech23', '123', '456') Result: '4tech56' TRANSLATE('222tech', '2ec', '3it') Result: '333tith' 22/39
  • 23. REGEXP_LIKE Condition • Description The Oracle REGEXP_LIKE condition allows you to perform regular expression matching in the WHERE clause of a SELECT, INSERT, UPDATE, or DELETE statement. • SYNTAX REGEXP_LIKE ( expression, pattern [, match_parameter ] ) • Expression A character expression such as a column or field. It can be a VARCHAR2, CHAR, NVARCHAR2, NCHAR, CLOB or NCLOB data type. 23/39
  • 24. REGEXP_LIKE Condition • Pattern The regular expression matching information. ^ Matches the beginning of a string. $ Matches the end of a string. * Matches zero or more occurrences. | Used like an "OR" to specify more than one alternative. + Matches one of more occurrences. ? Matches zero or one occurrence. . Matches any character except NULL. [ ] Used to specify a matching list where you are trying to match any one of the characters in the list. [^ ] Used to specify a nonmatching list where you are trying to match any character except for the ones in the list. 24/39
  • 25. REGEXP_LIKE Condition • match_parameter Optional. It allows you to modify the matching behavior for the REGEXP_LIKE condition. • Example SELECT last_name FROM contacts WHERE REGEXP_LIKE (last_name, 'Anders(o|e|a)n'); SELECT last_name FROM contacts WHERE REGEXP_LIKE (last_name, '^A(*)'); SELECT last_name FROM contacts WHERE REGEXP_LIKE (last_name, '(*)n$'); ‘c’ Perform case-sensitive matching. ‘i’ Perform case-insensitive matching. ‘n’ Allows the period character (.) to match the newline character. ‘m’ expression is assumed to have multiple lines, where ^ is the start of a line and $ is the end of a line, regardless of the position of those characters in expression. ‘x’ Whitespace characters are ignored. 25/39
  • 26. REGEXP_COUNT Function • Description The Oracle/PLSQL REGEXP_COUNT function counts the number of times that a pattern occurs in a string. • Syntax REGEXP_COUNT( string, pattern [, start_position [, match_parameter ] ] ) • string The string to search. string can be CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. • pattern The regular expression matching information. • start_position Optional. It is the position in string where the search will start. If omitted, it defaults to 1 which is the first position in the string. • match_parameter Optional. It allows you to modify the matching behavior for the REGEXP_COUNT function. 26/39
  • 27. REGEXP_COUNT Function • Example SELECT REGEXP_COUNT ('TechOnTheNet is a great resource!', 't') FROM dual; Result: 2. SELECT REGEXP_COUNT ('TechOnTheNet is a great resource!', 't', 1, 'i') FROM dual; Result: 4 SELECT REGEXP_COUNT ('The example shows how to use the REGEXP_COUNT function.', 'the', 4, 'i') FROM dual; Result: 1 SELECT REGEXP_COUNT ('Anderson', 'a|e|i|o|u') FROM dual; Result: 2 SELECT REGEXP_COUNT ('Anderson', 'a|e|i|o|u', 1, 'i') FROM dual; Result: 3 27/39
  • 28. COALESCE Function • Description The Oracle/PLSQL COALESCE function returns the first non-null expression in the list. If all expressions evaluate to null, then the COALESCE function will return null. • Syntax COALESCE( expr1, expr2, ... expr_n ) • expr1, expr2, ... expr_n The expressions to test for non-null values. 28/39
  • 29. COALESCE Function • Example SELECT COALESCE( address1, address2, address3 ) result FROM suppliers; The above COALESCE function is equivalent to the following IF-THEN-ELSE statement: IF address1 is not null THEN result := address1; ELSIF address2 is not null THEN result := address2; ELSIF address3 is not null THEN result := address3; ELSE result := null; END IF; 29/39
  • 30. EXTRACT Function • Description The Oracle/PLSQL EXTRACT function extracts a value from a date or interval value. • Syntax EXTRACT ( { YEAR | MONTH | DAY | HOUR | MINUTE | SECOND } | { TIMEZONE_HOUR | TIMEZONE_MINUTE } | { TIMEZONE_REGION | TIMEZONE_ABBR } FROM { date_value | interval_value } ) • Example EXTRACT(YEAR FROM DATE '2003-08-22') Result: 2003 EXTRACT(MONTH FROM DATE '2003-08-22') Result: 8 EXTRACT(DAY FROM DATE '2003-08-22') Result: 22 30/39
  • 31. ADD_MONTHS Function • Description The Oracle/PLSQL ADD_MONTHS function returns a date with a specified number of months added. • Syntax ADD_MONTHS( date1, number_months ) • Date1 The starting date (before the n months have been added). • number_months The number of months to add to date1. 31/39
  • 32. ADD_MONTHS Function • Example ADD_MONTHS('01-Aug-03', 3) Result: '01-Nov-03‘ ADD_MONTHS('01-Aug-03', -3) Result: '01-May-03' ADD_MONTHS('21-Aug-03', -3) Result: '21-May-03' ADD_MONTHS('31-Jan-03', 1) Result: '28-Feb-03' 32/39
  • 33. INITCAP Function • Description The Oracle/PLSQL INITCAP function sets the first character in each word to uppercase and the rest to lowercase. • Syntax INITCAP( string1 ) • string1 The string argument whose first character in each word will be converted to uppercase and all remaining characters converted to lowercase. • Example INITCAP('tech on the net'); Result: 'Tech On The Net‘ INITCAP('GEORGE BURNS'); Result: 'George Burns' 33/39
  • 34. INSTR Function • Description The Oracle/PLSQL INSTR function returns the location of a substring in a string. • Syntax INSTR( string, substring [, start_position [, nth_appearance ] ] ) • String The string to search. string can be CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. • Substring The substring to search for in string. substring can be CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. 34/39
  • 35. INSTR Function • start_position Optional. The position in string where the search will start. If omitted, it defaults to 1. The first position in the string is 1. If the start_position is negative, the INSTR function counts back start_position number of characters from the end of string and then searches towards the beginning of string. • nth_appearance Optional. The nth appearance of substring. If omitted, it defaults to 1. Note: If substring is not found in string, then the INSTR function will return 0. 35/39
  • 36. INSTR Function • Example INSTR('Tech on the net', 'e') Result: 2 (the first occurrence of 'e') INSTR('Tech on the net', 'e', 1, 1) Result: 2 (the first occurrence of 'e') INSTR('Tech on the net', 'e', 1, 2) Result: 11 (the second occurrence of 'e') INSTR('Tech on the net', 'e', 1, 3) Result: 14 (the third occurrence of 'e') INSTR('Tech on the net', 'e', -3, 2) Result: 2 36/39
  • 37. GREATEST Function • Description The Oracle/PLSQL GREATEST function returns the greatest value in a list of expressions. • Syntax GREATEST( expr1 [, expr2, ... expr_n] ) • expr1 The first expression to be evaluated whether it is the greatest. • expr2, ... expr_n Optional. Additional expressions that are to be evaluated. Note: If the datatypes of the expressions are different, all expressions will be converted to whatever datatype expr1 is. If the comparison is based on a character comparison, one character is considered greater than another if it has a higher character set value. 37/39
  • 38. GREATEST Function • Example GREATEST(2, 5, 12, 3) Result: 12 GREATEST('2', '5', '12', '3') Result: '5' GREATEST('apples', 'oranges', 'bananas') Result: 'oranges' GREATEST('apples', 'applis', 'applas') Result: 'applis' 38/39
  • 39. Name of Functions select distinct object_name from all_arguments where package_name = 'STANDARD'; 39/39