ANSI SQL Extensions  «Prev  Next»

Lesson 2 Oracle built-in functions for SQL
Objective Describe the purpose of built-in functions with Oracle SQL

Oracle Built-In Functions for SQL

Oracle's SQL environment is distinguished by a comprehensive library of built-in functions (BIFs) that extend the language well beyond basic data retrieval. These functions are not optional enhancements — they are an integral part of how Oracle SQL operates, and understanding them is fundamental to writing queries that are both correct and performant. From string manipulation and date arithmetic through null handling, conditional logic, and aggregate calculations, BIFs allow entire application programs to be written in SQL without resorting to procedural code in the middle tier.

This lesson describes the purpose of built-in functions, explains how they are executed within the Oracle library cache, identifies the functions most relevant to SQL performance tuning, and provides practical examples demonstrating their use.

What Are Oracle Built-In Functions?

Oracle built-in functions are language extensions built directly into the Oracle SQL engine. They execute within the database server immediately after row data has been retrieved from storage and before the result set is returned to the client — a position in the execution pipeline that gives them a significant performance advantage over equivalent logic implemented in an external procedural program.

Because BIFs are intrinsic to the Oracle engine, they execute far faster than a PL/SQL function call or a Java stored procedure performing the same transformation. The trade-off is portability: Oracle BIFs are specific to the Oracle database and cannot be used without modification in DB2, SQL Server, or other relational databases. For applications that are Oracle-specific, this trade-off strongly favors BIF usage.

Oracle also allows you to define your own functions and integrate them into your SQL dialect as user-defined functions. To see all built-in functions available in your Oracle environment, query the data dictionary:
SELECT DISTINCT
  object_name
FROM
  all_arguments
WHERE
  package_name = 'STANDARD'
ORDER BY
  object_name;
This query returns every function defined in the STANDARD package — the source of Oracle's core SQL built-in function library, present in every Oracle installation from Oracle 8i through Oracle 23ai.

How Built-In Functions Are Executed: The Library Cache

Oracle SQL execution diagram showing Oracle data flowing into the library cache where executable SQL calls built-in functions to transform raw row data before delivering it to the end user
Figure 2-1: Oracle SQL execution and built-in function processing. The library cache parses and prepares executable SQL, which calls built-in functions (BIF) to transform raw row data before the result set is returned to the end user.
The diagram above illustrates the position of built-in functions within the Oracle execution pipeline:
  1. A SQL query is submitted and Oracle retrieves the raw row data from the data source (tables, indexes)
  2. The query enters the library cache, where it is parsed, validated, and prepared as executable SQL
  3. The executable SQL calls the relevant built-in functions (BIF), which apply scalar, aggregate, or analytic transformations to the raw row data
  4. The transformed row data — formatted, calculated, and filtered — is returned to the end user as the query result set

This pipeline positioning means that BIFs operate on data that has already been fetched from storage but has not yet been transmitted to the client. The transformation happens within the database server, eliminating the round-trip cost of shipping raw data to the application tier for processing.

Built-In Function Categories

Oracle organizes built-in functions into four primary categories, all of which have been present since Oracle 8i and remain fully supported in Oracle 23ai:
Category Description Examples
Aggregate Operate on groups of rows and return a single value per group AVG, COUNT, SUM, MAX, MIN, GROUPING
Single-Row (Scalar) Operate on each row individually and return one result per row SUBSTR, UPPER, NVL, DECODE, ROUND, INITCAP, INSTR, REPLACE, TRIM
Date and Time Perform date arithmetic, formatting, and conversion SYSDATE, TO_DATE, TO_CHAR, ADD_MONTHS, MONTHS_BETWEEN, NEXT_DAY, LAST_DAY, TRUNC
Analytic Compute aggregate values across a window of rows relative to the current row ROW_NUMBER, RANK, DENSE_RANK, LEAD, LAG, NTILE

Oracle 23ai has extended the function library with new categories including Vector functions (for AI similarity search), JSON functions (for native JSON document processing), and UUID generation. None of the core scalar, aggregate, date, or analytic functions from Oracle 8i have been removed.

Common Built-In Functions Reference

The following functions represent the core BIF vocabulary used in everyday Oracle SQL development and tuning:
-- String functions
SUBSTR, UPPER, LOWER, INITCAP, LENGTH, INSTR,
CONCAT, LPAD, RPAD, LTRIM, RTRIM, TRIM,
REPLACE, TRANSLATE, SOUNDEX, CHR

-- Numeric functions
ROUND, TRUNC, FLOOR, CEIL, MOD,
GREATEST, LEAST, ABS, SIGN

-- Date and time functions
SYSDATE, TO_DATE, TO_CHAR, ADD_MONTHS,
MONTHS_BETWEEN, NEXT_DAY, LAST_DAY, TRUNC

-- Null handling
NVL, NVL2, COALESCE, NULLIF

-- Conditional logic
DECODE, CASE

-- Conversion functions
TO_CHAR, TO_NUMBER, TO_DATE,
ROWIDTOCHAR, TO_LOB

-- Aggregate functions
AVG, COUNT, SUM, MAX, MIN, GROUPING

-- Analytic functions
ROW_NUMBER, RANK, DENSE_RANK,
LEAD, LAG, NTILE, FIRST_VALUE, LAST_VALUE

-- Context and user functions
USER, USERENV, SYS_CONTEXT

BIFs That Improve SQL Performance

Since this module is concerned with SQL performance tuning, the most important built-in functions are those that allow Oracle to perform data transformation within the SQL statement itself rather than requiring post-retrieval processing in application code. The following functions have the most direct impact on query performance:
Function Performance Impact
DECODE Translates row values from one representation to another within the SQL statement, eliminating the need for post-fetch conditional logic in the application layer. Executes in a single pass through the result set.
NVL / COALESCE Assigns a default value to null columns within the query, preventing null propagation errors and eliminating null-checking code in the application. COALESCE is the SQL-standard equivalent that accepts more than two arguments.
TO_CHAR Formats date and numeric values into character strings within the SQL layer. Eliminates format conversion code in the application and ensures consistent formatting at the database level.
MONTHS_BETWEEN Performs automatic date arithmetic to calculate elapsed time between two dates. Replaces complex procedural date-difference calculations with a single function call that executes natively in the Oracle engine.
TRANSLATE Maps one set of column values to another set in a single operation. Faster than equivalent REPLACE chains or CASE expressions for character-by-character substitution tasks.

BIF Example: DECODE and TO_CHAR

The following query demonstrates two performance-relevant BIFs working together. The DECODE function translates a state code column into readable state names, while TO_CHAR formats the hire date — both transformations happening within the SQL engine rather than in application code:
SELECT
  emp_name,
  TO_CHAR(hire_date, 'DD-MON-YYYY'),
  DECODE(state_code,
         'AZ', 'Arizona',
         'CA', 'California',
         'NC', 'North Carolina',
         'WY', 'Wyoming',
               'NONE')
FROM employee;
Result:
Barrett   03-Mar-1996    California
Burleeze  04-JAN-1993    North Carolina
Dahlstrom 06-Feb-1999    NONE

3 rows selected.
Without BIFs, this query would return raw state codes and numeric date representations, requiring the application to maintain a state-code lookup table and a date formatter. With BIFs, the transformation is delegated to Oracle, which executes it in the library cache during result-set construction — a single pass, no round trips.

The Role of the Optimizer

Understanding how BIFs interact with the Oracle optimizer is important for performance tuning. The optimizer determines the most efficient execution plan for a SQL statement by evaluating the objects referenced, the conditions specified, and the available access paths. Its output is a plan that may involve:
  • Full table scans — reading every block in the table
  • Index scans — using an index to locate specific rows
  • Nested loops — joining tables by iterating through one row set for each row in another
  • Hash joins — building a hash table from one input and probing it with the other

Oracle uses the Cost-Based Optimizer (CBO) as its primary optimization strategy. The CBO evaluates statistical information about tables, indexes, and data distribution to calculate the cost of alternative execution plans and select the lowest-cost path. The older Rule-Based Optimizer (RBO) is no longer recommended — Oracle's current guidance is to use the cost-based approach for all production workloads.

BIFs interact with the optimizer in an important way: wrapping an indexed column in a function call can prevent the optimizer from using the index, forcing a full table scan instead. For example:
-- Index on hire_date CANNOT be used (function applied to indexed column):
SELECT emp_name FROM employee
WHERE TO_CHAR(hire_date, 'YYYY') = '1996';

-- Index on hire_date CAN be used (range condition on raw column):
SELECT emp_name FROM employee
WHERE hire_date >= DATE '1996-01-01'
  AND hire_date  < DATE '1997-01-01';
This is one of the most common performance problems introduced by BIF usage. The solution is either to rewrite the predicate to avoid applying a function to the indexed column, or to create a function-based index that indexes the transformed value directly.

Legacy and Modern Equivalents

Several BIFs from Oracle 8i remain fully functional in Oracle 23ai but have modern SQL-standard equivalents that are preferred for new development:
Legacy Function Modern Equivalent Reason to Prefer Modern Version
DECODE CASE ... WHEN ... END ANSI SQL standard; more readable for complex conditions; supports range comparisons and IS NULL tests that DECODE cannot express
NVL COALESCE ANSI SQL standard; accepts more than two arguments; evaluates arguments lazily, avoiding unnecessary computation
USERENV SYS_CONTEXT More flexible, more secure; supports a wider range of session attributes; preferred by Oracle documentation for all new development
ROWIDTOCHAR Direct ROWID operations Modern Oracle handles ROWID comparisons natively without explicit conversion in most contexts
TO_LOB Native LOB operations Oracle's improved LOB handling in recent releases reduces the need for explicit type conversion in most use cases

All functions in both columns remain fully supported in Oracle 23ai. The recommendation to prefer modern equivalents is for new development — existing code using the legacy forms does not need to be rewritten unless there is a specific functional reason to do so.

Oracle query override functions — optimizer hints that allow developers to influence the execution plan chosen by the CBO — are examined in the next lesson.
[1] BIF: Built-In Function — a language extension built directly into the Oracle SQL engine that executes within the library cache as part of query processing, without requiring external procedural code.

SEMrush Software 2 SEMrush Banner 2