
- •Contents
- •Send Us Your Comments
- •Preface
- •What’s New in SQL Reference?
- •1 Introduction to Oracle SQL
- •History of SQL
- •SQL Standards
- •Embedded SQL
- •Lexical Conventions
- •Tools Support
- •2 Basic Elements of Oracle SQL
- •Datatypes
- •Oracle Built-in Datatypes
- •ANSI, DB2, and SQL/DS Datatypes
- •Oracle-Supplied Types
- •"Any" Types
- •XML Types
- •Spatial Type
- •Media Types
- •Datatype Comparison Rules
- •Data Conversion
- •Literals
- •Text Literals
- •Integer Literals
- •Number Literals
- •Interval Literals
- •Format Models
- •Number Format Models
- •Date Format Models
- •String-to-Date Conversion Rules
- •XML Format Model
- •Nulls
- •Nulls in SQL Functions
- •Nulls with Comparison Conditions
- •Nulls in Conditions
- •Pseudocolumns
- •CURRVAL and NEXTVAL
- •LEVEL
- •ROWID
- •ROWNUM
- •XMLDATA
- •Comments
- •Comments Within SQL Statements
- •Comments on Schema Objects
- •Hints
- •Database Objects
- •Schema Objects
- •Nonschema Objects
- •Parts of Schema Objects
- •Schema Object Names and Qualifiers
- •Schema Object Naming Rules
- •Schema Object Naming Examples
- •Schema Object Naming Guidelines
- •Syntax for Schema Objects and Parts in SQL Statements
- •How Oracle Resolves Schema Object References
- •Referring to Objects in Other Schemas
- •Referring to Objects in Remote Databases
- •Referencing Object Type Attributes and Methods
- •3 Operators
- •About SQL Operators
- •Unary and Binary Operators
- •Operator Precedence
- •Arithmetic Operators
- •Concatenation Operator
- •Set Operators
- •4 Expressions
- •About SQL Expressions
- •Simple Expressions
- •Compound Expressions
- •CASE Expressions
- •CURSOR Expressions
- •Datetime Expressions
- •Function Expressions
- •INTERVAL Expressions
- •Object Access Expressions
- •Scalar Subquery Expressions
- •Type Constructor Expressions
- •Variable Expressions
- •Expression Lists
- •5 Conditions
- •About SQL Conditions
- •Condition Precedence
- •Comparison Conditions
- •Simple Comparison Conditions
- •Group Comparison Conditions
- •Logical Conditions
- •Membership Conditions
- •Range Conditions
- •Null Conditions
- •EQUALS_PATH
- •EXISTS Conditions
- •LIKE Conditions
- •IS OF type Conditions
- •UNDER_PATH
- •Compound Conditions
- •6 Functions
- •SQL Functions
- •Single-Row Functions
- •Aggregate Functions
- •Analytic Functions
- •Object Reference Functions
- •Alphabetical Listing of SQL Functions
- •ACOS
- •ADD_MONTHS
- •ASCII
- •ASCIISTR
- •ASIN
- •ATAN
- •ATAN2
- •BFILENAME
- •BITAND
- •CAST
- •CEIL
- •CHARTOROWID
- •COALESCE
- •COMPOSE
- •CONCAT
- •CONVERT
- •CORR
- •COSH
- •COUNT
- •COVAR_POP
- •COVAR_SAMP
- •CUME_DIST
- •CURRENT_DATE
- •CURRENT_TIMESTAMP
- •DBTIMEZONE
- •DECODE
- •DECOMPOSE
- •DENSE_RANK
- •DEPTH
- •DEREF
- •DUMP
- •EMPTY_BLOB, EMPTY_CLOB
- •EXISTSNODE
- •EXTRACT (datetime)
- •EXTRACT (XML)
- •EXTRACTVALUE
- •FIRST
- •FIRST_VALUE
- •FLOOR
- •FROM_TZ
- •GREATEST
- •GROUP_ID
- •GROUPING
- •GROUPING_ID
- •HEXTORAW
- •INITCAP
- •INSTR
- •LAST
- •LAST_DAY
- •LAST_VALUE
- •LEAD
- •LEAST
- •LENGTH
- •LOCALTIMESTAMP
- •LOWER
- •LPAD
- •LTRIM
- •MAKE_REF
- •MONTHS_BETWEEN
- •NCHR
- •NEW_TIME
- •NEXT_DAY
- •NLS_CHARSET_DECL_LEN
- •NLS_CHARSET_ID
- •NLS_CHARSET_NAME
- •NLS_INITCAP
- •NLS_LOWER
- •NLSSORT
- •NLS_UPPER
- •NTILE
- •NULLIF
- •NUMTODSINTERVAL
- •NUMTOYMINTERVAL
- •PATH
- •PERCENT_RANK
- •PERCENTILE_CONT
- •PERCENTILE_DISC
- •POWER
- •RANK
- •RATIO_TO_REPORT
- •RAWTOHEX
- •RAWTONHEX
- •REFTOHEX
- •REGR_ (Linear Regression) Functions
- •REPLACE
- •ROUND (number)
- •ROUND (date)
- •ROW_NUMBER
- •ROWIDTOCHAR
- •ROWIDTONCHAR
- •RPAD
- •RTRIM
- •SESSIONTIMEZONE
- •SIGN
- •SINH
- •SOUNDEX
- •SQRT
- •STDDEV
- •STDDEV_POP
- •STDDEV_SAMP
- •SUBSTR
- •SYS_CONNECT_BY_PATH
- •SYS_CONTEXT
- •SYS_DBURIGEN
- •SYS_EXTRACT_UTC
- •SYS_GUID
- •SYS_TYPEID
- •SYS_XMLAGG
- •SYS_XMLGEN
- •SYSDATE
- •SYSTIMESTAMP
- •TANH
- •TO_CHAR (character)
- •TO_CHAR (datetime)
- •TO_CHAR (number)
- •TO_CLOB
- •TO_DATE
- •TO_DSINTERVAL
- •TO_MULTI_BYTE
- •TO_NCHAR (character)
- •TO_NCHAR (datetime)
- •TO_NCHAR (number)
- •TO_NCLOB
- •TO_NUMBER
- •TO_SINGLE_BYTE
- •TO_TIMESTAMP
- •TO_TIMESTAMP_TZ
- •TO_YMINTERVAL
- •TRANSLATE
- •TRANSLATE ... USING
- •TREAT
- •TRIM
- •TRUNC (number)
- •TRUNC (date)
- •TZ_OFFSET
- •UNISTR
- •UPDATEXML
- •UPPER
- •USER
- •USERENV
- •VALUE
- •VAR_SAMP
- •VARIANCE
- •VSIZE
- •WIDTH_BUCKET
- •XMLAGG
- •XMLCOLATTVAL
- •XMLCONCAT
- •XMLELEMENT
- •XMLFOREST
- •XMLSEQUENCE
- •XMLTRANSFORM
- •ROUND and TRUNC Date Functions
- •User-Defined Functions
- •Prerequisites
- •Name Precedence
- •7 Common SQL DDL Clauses
- •allocate_extent_clause
- •constraints
- •deallocate_unused_clause
- •file_specification
- •logging_clause
- •parallel_clause
- •physical_attributes_clause
- •storage_clause
- •8 SQL Queries and Subqueries
- •About Queries and Subqueries
- •Creating Simple Queries
- •Hierarchical Queries
- •The UNION [ALL], INTERSECT, MINUS Operators
- •Sorting Query Results
- •Joins
- •Using Subqueries
- •Unnesting of Nested Subqueries
- •Selecting from the DUAL Table
- •Distributed Queries
- •9 SQL Statements: ALTER CLUSTER to ALTER SEQUENCE
- •Types of SQL Statements
- •Organization of SQL Statements
- •ALTER CLUSTER
- •ALTER DATABASE
- •ALTER DIMENSION
- •ALTER FUNCTION
- •ALTER INDEX
- •ALTER INDEXTYPE
- •ALTER JAVA
- •ALTER MATERIALIZED VIEW
- •ALTER MATERIALIZED VIEW LOG
- •ALTER OPERATOR
- •ALTER OUTLINE
- •ALTER PACKAGE
- •ALTER PROCEDURE
- •ALTER PROFILE
- •ALTER RESOURCE COST
- •ALTER ROLE
- •ALTER ROLLBACK SEGMENT
- •ALTER SEQUENCE
- •10 SQL Statements: ALTER SESSION to ALTER SYSTEM
- •ALTER SESSION
- •ALTER SYSTEM
- •ALTER TABLE
- •ALTER TABLESPACE
- •ALTER TRIGGER
- •ALTER TYPE
- •ALTER USER
- •ALTER VIEW
- •ANALYZE
- •ASSOCIATE STATISTICS
- •AUDIT
- •CALL
- •COMMENT
- •COMMIT
- •13 SQL Statements: CREATE CLUSTER to CREATE JAVA
- •CREATE CLUSTER
- •CREATE CONTEXT
- •CREATE CONTROLFILE
- •CREATE DATABASE
- •CREATE DATABASE LINK
- •CREATE DIMENSION
- •CREATE DIRECTORY
- •CREATE FUNCTION
- •CREATE INDEX
- •CREATE INDEXTYPE
- •CREATE JAVA
- •14 SQL Statements: CREATE LIBRARY to CREATE SPFILE
- •CREATE LIBRARY
- •CREATE MATERIALIZED VIEW
- •CREATE MATERIALIZED VIEW LOG
- •CREATE OPERATOR
- •CREATE OUTLINE
- •CREATE PACKAGE
- •CREATE PACKAGE BODY
- •CREATE PFILE
- •CREATE PROCEDURE
- •CREATE PROFILE
- •CREATE ROLE
- •CREATE ROLLBACK SEGMENT
- •CREATE SCHEMA
- •CREATE SEQUENCE
- •CREATE SPFILE
- •15 SQL Statements: CREATE SYNONYM to CREATE TRIGGER
- •CREATE SYNONYM
- •CREATE TABLE
- •CREATE TABLESPACE
- •CREATE TEMPORARY TABLESPACE
- •CREATE TRIGGER
- •CREATE TYPE
- •CREATE TYPE BODY
- •CREATE USER
- •CREATE VIEW
- •DELETE
- •DISASSOCIATE STATISTICS
- •DROP CLUSTER
- •DROP CONTEXT
- •DROP DATABASE LINK
- •DROP DIMENSION
- •DROP DIRECTORY
- •DROP FUNCTION
- •DROP INDEX
- •DROP INDEXTYPE
- •DROP JAVA
- •DROP LIBRARY
- •DROP MATERIALIZED VIEW
- •DROP MATERIALIZED VIEW LOG
- •DROP OPERATOR
- •DROP OUTLINE
- •DROP PACKAGE
- •DROP PROCEDURE
- •DROP PROFILE
- •DROP ROLE
- •DROP ROLLBACK SEGMENT
- •17 SQL Statements: DROP SEQUENCE to ROLLBACK
- •DROP SEQUENCE
- •DROP SYNONYM
- •DROP TABLE
- •DROP TABLESPACE
- •DROP TRIGGER
- •DROP TYPE
- •DROP TYPE BODY
- •DROP USER
- •DROP VIEW
- •EXPLAIN PLAN
- •GRANT
- •INSERT
- •LOCK TABLE
- •MERGE
- •NOAUDIT
- •RENAME
- •REVOKE
- •ROLLBACK
- •18 SQL Statements: SAVEPOINT to UPDATE
- •SAVEPOINT
- •SELECT
- •SET CONSTRAINT[S]
- •SET ROLE
- •SET TRANSACTION
- •TRUNCATE
- •UPDATE
- •Required Keywords and Parameters
- •Optional Keywords and Parameters
- •Syntax Loops
- •Multipart Diagrams
- •Database Objects
- •ANSI Standards
- •ISO Standards
- •Oracle Compliance
- •FIPS Compliance
- •Oracle Extensions to Standard SQL
- •Character Set Support
- •Using Extensible Indexing
- •Using XML in SQL Statements
- •Index

LOWER
INSERT INTO local_test VALUES
(TO_TIMESTAMP(LOCALTIMESTAMP, ’DD-MON-RR HH.MI.SSXFF’));
The following statement uses the correct format mask to match the return type of
LOCALTIMESTAMP:
INSERT INTO local_test VALUES
(TO_TIMESTAMP(LOCALTIMESTAMP, ’DD-MON-RR HH.MI.SSXFF PM’));
LOG
Syntax log::=
LOG (
m
,
n
)
Purpose
LOG returns the logarithm, base m, of n. The base m can be any positive number other than 0 or 1 and n can be any positive number.
Examples
The following example returns the log of 100:
SELECT LOG(10,100) "Log base 10 of 100" FROM DUAL;
Log base 10 of 100
------------------
2
LOWER
Syntax lower::=
LOWER (
char
)
Functions 6-91

LPAD
Purpose
LOWER returns char, with all letters lowercase. char can be any of the datatypes
CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. The return value is the same datatype as char.
Examples
The following example returns a string in lowercase:
SELECT LOWER(’MR. SCOTT MCMILLAN’) "Lowercase"
FROM DUAL;
Lowercase
--------------------
mr. scott mcmillan
LPAD
Syntax lpad::=
|
|
|
|
, |
char2 |
LPAD |
( |
char1 |
, |
n |
) |
Purpose
LPAD returns char1, left-padded to length n with the sequence of characters in char2; char2 defaults to a single blank. If char1 is longer than n, then this function returns the portion of char1 that fits in n.
Both char1 and char2 can be any of the datatypes CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. The string returned is of VARCHAR2 datatype and is in the same character set as char1.
The argument n is the total length of the return value as it is displayed on your terminal screen. In most character sets, this is also the number of characters in the return value. However, in some multibyte character sets, the display length of a character string can differ from the number of characters in the string.
Examples
The following example left-pads a string with the characters "*" and ".":
6-92 Oracle9i SQL Reference

LTRIM
SELECT LPAD(’Page 1’,15,’*.’) "LPAD example"
FROM DUAL;
LPAD example
---------------
*.*.*.*.*Page 1
LTRIM
Syntax ltrim::=
|
|
, |
set |
LTRIM |
( |
char |
) |
Purpose
LTRIM removes characters from the left of char, with all the leftmost characters that appear in set removed; set defaults to a single blank. If char is a character literal, then you must enclose it in single quotes. Oracle begins scanning char from its first character and removes all characters that appear in set until reaching a character not in set and then returns the result.
Both char and set can be any of the datatypes CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. The string returned is of VARCHAR2 datatype and is in the same character set as char.
Examples
The following example trims all of the left-most x’s and y’s from a string:
SELECT LTRIM(’xyxXxyLAST WORD’,’xy’) "LTRIM example"
FROM DUAL;
LTRIM example
------------
XxyLAST WORD
Functions 6-93

MAKE_REF
MAKE_REF
Syntax make_ref::=
|
|
|
, |
|
|
|
table |
|
|
MAKE_REF |
( |
, |
key |
) |
|
|
view |
|
|
Purpose
MAKE_REF creates a REF to a row of an object view or a row in an object table whose object identifier is primary key based.
See Also:
■Oracle9i Application Developer’s Guide - Fundamentals for more information about object views
■DEREF on page 6-58
Examples
The sample schema oe contains an object view oc_inventories based on inventory_typ. The object identifier is product_id. The following example creates a REF to the row in the oc_inventories object view with a product_id of 3003:
SELECT MAKE_REF (oc_inventories, 3003) FROM DUAL;
MAKE_REF(OC_INVENTORIES,3003)
------------------------------------------------------------------
00004A038A0046857C14617141109EE03408002082543600000014260100010001
00290090606002A00078401FE0000000B03C21F040000000000000000000000000
0000000000
6-94 Oracle9i SQL Reference

MAX
MAX
Syntax max::=
|
DISTINCT |
|
|
|
|
|
|
ALL |
|
OVER |
( |
analytic_clause |
) |
MAX |
( |
expr |
) |
|
|
|
See Also: "Analytic Functions" on page 6-10 for information on syntax, semantics, and restrictions
Purpose
MAX returns maximum value of expr. You can use it as an aggregate or analytic function.
If you specify DISTINCT, then you can specify only the query_partition_ clause of the analytic_clause. The order_by_clause and windowing_ clause are not allowed.
See Also:
■
■
"Aggregate Functions" on page 6-8
"About SQL Expressions" on page 4-2 for information on valid forms of expr
Aggregate Example
The following example determines the highest salary in the hr.employees table:
SELECT MAX(salary) "Maximum" FROM employees;
Maximum
----------
24000
Analytic Example
The following example calculates, for each employee, the highest salary of the employees reporting to the same manager as the employee.
Functions 6-95

MAX
SELECT manager_id, last_name, salary,
MAX(salary) OVER (PARTITION BY manager_id) AS mgr_max FROM employees;
MANAGER_ID |
LAST_NAME |
SALARY |
MGR_MAX |
---------- |
------------------------- ---------- ---------- |
||
100 |
Kochhar |
17000 |
17000 |
100 |
De Haan |
17000 |
17000 |
100 |
Raphaely |
11000 |
17000 |
100 |
Kaufling |
7900 |
17000 |
100 |
Fripp |
8200 |
17000 |
100 |
Weiss |
8000 |
17000 |
... |
|
|
|
If you enclose this query in the parent query with a predicate, then you can determine the employee who makes the highest salary in each department:
SELECT manager_id, last_name, salary
FROM (SELECT manager_id, last_name, salary,
MAX(salary) OVER (PARTITION BY manager_id) AS rmax_sal FROM employees) WHERE salary = rmax_sal;
MANAGER_ID |
LAST_NAME |
SALARY |
---------- |
------------------------- ---------- |
|
100 |
Kochhar |
17000 |
100 |
De Haan |
17000 |
101 |
Greenberg |
12000 |
101 |
Higgens |
12000 |
102 |
Hunold |
9000 |
103 |
Ernst |
6000 |
108 |
Faviet |
9000 |
114 |
Khoo |
3100 |
120 |
Nayer |
3200 |
120 |
Taylor |
3200 |
121 |
Sarchand |
4200 |
122 |
Chung |
3800 |
123 |
Bell |
4000 |
124 |
Rajs |
3500 |
145 |
Tucker |
10000 |
146 |
King |
10000 |
147 |
Vishney |
10500 |
148 |
Ozer |
11500 |
149 |
Abel |
11000 |
201 |
Goyal |
6000 |
205 |
Gietz |
8300 |
|
King |
24000 |
6-96 Oracle9i SQL Reference

MIN
MIN
Syntax min::=
|
DISTINCT |
|
|
|
|
|
|
ALL |
|
OVER |
( |
analytic_clause |
) |
MIN |
( |
expr |
) |
|
|
|
See Also: "Analytic Functions" on page 6-10 for information on syntax, semantics, and restrictions
Purpose
MIN returns minimum value of expr. You can use it as an aggregate or analytic function.
If you specify DISTINCT, then you can specify only the query_partition_ clause of the analytic_clause. The order_by_clause and windowing_ clause are not allowed.
See Also:
■
■
"Aggregate Functions" on page 6-8
"About SQL Expressions" on page 4-2 for information on valid forms of expr
Aggregate Example
The following statement returns the earliest hire date in the hr.employees table:
SELECT MIN(hire_date) "Earliest" FROM employees;
Earliest
---------
17-JUN-87
Analytic Example
The following example determines, for each employee, the employees who were hired on or before the same date as the employee. It then determines the subset of employees reporting to the same manager as the employee, and returns the lowest salary in that subset.
Functions 6-97

MOD
SELECT manager_id, last_name, hire_date, salary,
MIN(salary) OVER(PARTITION BY manager_id ORDER BY hire_date
RANGE UNBOUNDED PRECEDING) as p_cmin
FROM employees;
MANAGER_ID |
LAST_NAME |
HIRE_DATE |
SALARY |
P_CMIN |
---------- |
------------------------- |
--------- ---------- ---------- |
||
100 |
Kochhar |
21-SEP-89 |
17000 |
17000 |
100 |
De Haan |
13-JAN-93 |
17000 |
17000 |
100 |
Raphaely |
07-DEC-94 |
11000 |
11000 |
100 |
Kaufling |
01-MAY-95 |
7900 |
7900 |
100 |
Hartstein |
17-FEB-96 |
13000 |
7900 |
100 |
Weiss |
18-JUL-96 |
8000 |
7900 |
100 |
Russell |
01-OCT-96 |
14000 |
7900 |
100 |
Partners |
05-JAN-97 |
13500 |
7900 |
100 |
Errazuriz |
10-MAR-97 |
12000 |
7900 |
. |
|
|
|
|
.
.
MOD
Syntax mod::=
MOD (
m
,
n
)
Purpose
MOD returns the remainder of m divided by n. Returns m if n is 0.
Examples
The following example returns the remainder of 11 divided by 4:
SELECT MOD(11,4) "Modulus" FROM DUAL;
Modulus
----------
3
This function behaves differently from the classical mathematical modulus function when m is negative. The classical modulus can be expressed using the MOD function with this formula:
6-98 Oracle9i SQL Reference