Pages
- Home
- Oracle APEX Change Management System | Yasser Hassan Portfolio
- Oracle Change Management System Demo
- SQL Fundamentals
- SQL Fundamentals II
- PHP
- SQL PLUS
- PL/SQL
- Forms and Reports
- HTML5
- JavaScript
- Java
- PL/SQL Collections and Records
- PL/SQL Transactions
- Ref Cursors
- PL/SQL Analytic Functions
- Problems and solves
- Oracle Indexes
- Work Samples
- Record & Collection
- Conditional statements
- LOOP
- Triggers
- Object Oriented
- NLS_LANG
- Oracle Interview Question
- About Me
- Contact Me
- Privacy Policy
Wednesday, October 30, 2019
Oracle Forms Triggers
Sunday, October 27, 2019
Oracle Courses for all levels
Oracle Official Academy Courses
Course Topics:
- Introduction
- Retrieving Data Using the SQL SELECT Statement
- Restricting and Sorting Data
- Using Single Row Functions to Customize Reports
- Reporting Aggregated Data Using the Group Functions
- Displaying Data From Multiple Tables
- Using Sub queries to Solve Queries
- Using the SET Operators
- Manipulating Data
- Using DDL Statements to Create and Manage Tables
- Creating Other Schema Objects
- Managing Objects with Data Dictionary Views
- Controlling User Access
- Manage Schema Objects
- Manipulating Large Data Sets
- Generating Reports by Grouping Related Data
- Searching Data Using Advanced Sub queries
- Hierarchical Data Retrieval
2-Oracle Database 10g: Program with PL/SQL
Course Topics:
- Introduction to PL/SQL
- Declaring PL/SQL Identifiers
- Writing Executable Statements
- Interacting with the Oracle Server
- Writing Control Structures
- Working with Composite Data Types
- Using Explicit Cursors
- Handling Exceptions
- Creating Stored Procedures
- Creating Stored Functions
- Creating Packages
- Using More Package Concepts
- Utilizing Oracle Supplied Packages in Application Development
- Dynamic SQL and Metadata
- Design Considerations for PL/SQL Code
- Managing Dependencies
- Creating Triggers
- Applications for Triggers
- Understanding and Influencing the PL/SQL Compiler
3-Oracle Forms Developer 10g: Build Internet Applications
Course Topics:
- Introducing Oracle Forms Developer and Forms Services
- Creating Forms Modules
- Working with Data Blocks and Frames
- Working with Input Items
- Working with Non Input Items
- Working with Windows and Canvases
- Producing Triggers
- Debugging Triggers
- Adding Functionality to Items
- Run-Time Messages and Alerts
- Query Triggers
- Validation
- Navigation
- Transaction Processing
- Writing Flexible Code
- Sharing Objects and Code
4-Oracle Reports Developer 10g: Build Reports
Course Topics:
- Introduction to Oracle Reports Developer
- Designing and Running Reports
- Working in Oracle Reports Developer
- Creating a Paper Report
- Enhancing a Basic Paper Report
- Managing Report Templates
- Creating a Web Report
- Enhancing Reports Using the Data Model: Queries and Groups
- Enhancing Reports Using the Data Model: Data Sources
- Enhancing Reports Using the Data Model: Creating Columns
- Enhancing Reports Using the Paper Layout
- Controlling the Paper Layout: Common Properties
- Controlling the Paper Layout: Specific Properties
- Web Reporting
- Creating and Using Report Parameters
- Enhancing Matrix Reports
- Coding PL/SQL Triggers
- Extending Functionality Using the SRW Package
Thursday, October 24, 2019
Oracle Index types
Thursday, September 26, 2019
Oracle salary of emp which > salary of his manager
where e.sal > (select m.sal from emp m where m.empno =(select mgr from emp mg where e.empno = mg.empno) and m.empno = e.mgr)
*
FROM
EMP EM
WHERE
SAL > (SELECT SAL FROM EMP WHERE EMPNO = EM.MGR)
--------------
SELECT E.SAL,E.EMPNO,E.ENAME,M.SAL MSAL
FROM EMP E,EMP M
WHERE M.EMPNO = E.MGR
AND E.SAL > M.SAL
Oracle SUM Analytic Function
This article gives an overview of the SUM analytic function. If you are new to analytic functions you should probably read this introduction to analytic functions first.
Setup
SUM as an Aggregate Function
SUM Analytic Function
Quick Links
Related articles.
Analytic Functions : All Articles
Setup
The examples in this article require the following table.
--DROP TABLE emp PURGE;
CREATE TABLE emp (
empno NUMBER(4) CONSTRAINT pk_emp PRIMARY KEY,
ename VARCHAR2(10),
job VARCHAR2(9),
mgr NUMBER(4),
hiredate DATE,
sal NUMBER(7,2),
comm NUMBER(7,2),
deptno NUMBER(2)
);
INSERT INTO emp VALUES (7369,'SMITH','CLERK',7902,to_date('17-12-1980','dd-mm-yyyy'),800,NULL,20);
INSERT INTO emp VALUES (7499,'ALLEN','SALESMAN',7698,to_date('20-2-1981','dd-mm-yyyy'),1600,300,30);
INSERT INTO emp VALUES (7521,'WARD','SALESMAN',7698,to_date('22-2-1981','dd-mm-yyyy'),1250,500,30);
INSERT INTO emp VALUES (7566,'JONES','MANAGER',7839,to_date('2-4-1981','dd-mm-yyyy'),2975,NULL,20);
INSERT INTO emp VALUES (7654,'MARTIN','SALESMAN',7698,to_date('28-9-1981','dd-mm-yyyy'),1250,1400,30);
INSERT INTO emp VALUES (7698,'BLAKE','MANAGER',7839,to_date('1-5-1981','dd-mm-yyyy'),2850,NULL,30);
INSERT INTO emp VALUES (7782,'CLARK','MANAGER',7839,to_date('9-6-1981','dd-mm-yyyy'),2450,NULL,10);
INSERT INTO emp VALUES (7788,'SCOTT','ANALYST',7566,to_date('13-JUL-87','dd-mm-rr')-85,3000,NULL,20);
INSERT INTO emp VALUES (7839,'KING','PRESIDENT',NULL,to_date('17-11-1981','dd-mm-yyyy'),5000,NULL,10);
INSERT INTO emp VALUES (7844,'TURNER','SALESMAN',7698,to_date('8-9-1981','dd-mm-yyyy'),1500,0,30);
INSERT INTO emp VALUES (7876,'ADAMS','CLERK',7788,to_date('13-JUL-87', 'dd-mm-rr')-51,1100,NULL,20);
INSERT INTO emp VALUES (7900,'JAMES','CLERK',7698,to_date('3-12-1981','dd-mm-yyyy'),950,NULL,30);
INSERT INTO emp VALUES (7902,'FORD','ANALYST',7566,to_date('3-12-1981','dd-mm-yyyy'),3000,NULL,20);
INSERT INTO emp VALUES (7934,'MILLER','CLERK',7782,to_date('23-1-1982','dd-mm-yyyy'),1300,NULL,10);
COMMIT;
SUM as an Aggregate Function
The SUM aggregate function returns the sum of the specified values in a set. As an aggregate function it reduces the number of rows, hence the term "aggregate". If the data isn't grouped we turn the 14 rows in the EMP table to a single row with the aggregated value. In the following example we see the total value of the salaries for all employees.
SELECT SUM(sal) AS sum_total
FROM emp;
SUM_TOTAL
----------
29025
SQL>
We can get more granularity of information by including a GROUP BY clause. In the following example we see the sum of the salaires on a per-department basis.
SELECT deptno,
SUM(sal) AS sum_total_by_dept
FROM emp
GROUP BY deptno
ORDER BY deptno;
DEPTNO SUM_TOTAL_BY_DEPT
---------- -----------------
10 8750
20 10875
30 9400
SQL>
In both cases we have aggregated the data to get the values, returning less rows than we started with. Analytic functions allow us to return these aggregate values while retaining the original row data.
SUM Analytic Function
The basic description for the SUM analytic function is shown below. The analytic clause is described in more detail here.
SUM([ DISTINCT | ALL ] expr) [ OVER (analytic_clause)
Omitting a partitioning clause from the OVER clause means the whole result set is treated as a single partition. In the following example we display the total salaries of all employees, as well as all the original data.
SELECT empno,
ename,
deptno,
sal,
SUM(sal) OVER () AS total_sal
FROM emp;
EMPNO ENAME DEPTNO SAL TOTAL_SAL
---------- ---------- ---------- ---------- ----------
7369 SMITH 20 800 29025
7499 ALLEN 30 1600 29025
7521 WARD 30 1250 29025
7566 JONES 20 2975 29025
7654 MARTIN 30 1250 29025
7698 BLAKE 30 2850 29025
7782 CLARK 10 2450 29025
7788 SCOTT 20 3000 29025
7839 KING 10 5000 29025
7844 TURNER 30 1500 29025
7876 ADAMS 20 1100 29025
7900 JAMES 30 950 29025
7902 FORD 20 3000 29025
7934 MILLER 10 1300 29025
SQL>
Adding the partitioning clause allows us to display total salary within a partition.
SELECT empno,
ename,
deptno,
sal,
SUM(sal) OVER (PARTITION BY deptno) AS total_sal_by_dept
FROM emp;
EMPNO ENAME DEPTNO SAL TOTAL_SAL_BY_DEPT
---------- ---------- ---------- ---------- -----------------
7782 CLARK 10 2450 8750
7839 KING 10 5000 8750
7934 MILLER 10 1300 8750
7566 JONES 20 2975 10875
7902 FORD 20 3000 10875
7876 ADAMS 20 1100 10875
7369 SMITH 20 800 10875
7788 SCOTT 20 3000 10875
7521 WARD 30 1250 9400
7844 TURNER 30 1500 9400
7499 ALLEN 30 1600 9400
7900 JAMES 30 950 9400
7698 BLAKE 30 2850 9400
7654 MARTIN 30 1250 9400
SQL>
Adding the ORDER BY clause allows us to display a running total salary within a partition. In the example below, the default windowing clause is used, as well as being specified explicitly.
SELECT empno,
ename,
deptno,
sal,
SUM(sal) OVER (PARTITION BY deptno ORDER BY sal) AS running_tot_sal_by_dept_1,
SUM(sal) OVER (PARTITION BY deptno ORDER BY sal
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_tot_sal_by_dept_2
FROM emp;
EMPNO ENAME DEPTNO SAL RUNNING_TOT_SAL_BY_DEPT_1 RUNNING_TOT_SAL_BY_DEPT_2
---------- ---------- ---------- ---------- ------------------------- -------------------------
7934 MILLER 10 1300 1300 1300
7782 CLARK 10 2450 3750 3750
7839 KING 10 5000 8750 8750
7369 SMITH 20 800 800 800
7876 ADAMS 20 1100 1900 1900
7566 JONES 20 2975 4875 4875
7788 SCOTT 20 3000 10875 10875
7902 FORD 20 3000 10875 10875
7900 JAMES 30 950 950 950
7654 MARTIN 30 1250 3450 3450
7521 WARD 30 1250 3450 3450
7844 TURNER 30 1500 4950 4950
7499 ALLEN 30 1600 6550 6550
7698 BLAKE 30 2850 9400 9400
SQL>
Monday, July 30, 2018
Oracle create View with Parameter
1-virtual private database context
2-global package variable
3-Lookup Tables
#1 Virtual Private Database Context
I will use in where clause SYS_CONTEXT function as parameter to filter data of the query of the view.
First step is creating context that will handle session variables
CREATE OR REPLACE CONTEXT MY_CTX USING CTX_PKG
ACCESSED GLOBALLY;
Second I will create CTX_PKG package that context will use it to handle session parameters.
The package contains three procedures
a-SET_SESSION_ID to set unique id for every session I will use in my context.
b-CLEAR_SESSION to clear session from my context.
c-SET_CONTEXT to set variable value in my context.
Package Specification
CREATE OR REPLACE PACKAGE CTX_PKG
IS
PROCEDURE SET_SESSION_ID (IN_SESSION_ID VARCHAR2);
PROCEDURE CLEAR_SESSION (IN_SESSION_ID VARCHAR2);
PROCEDURE SET_CONTEXT (IN_NAME VARCHAR2, IN_VALUE VARCHAR2);
END CTX_PKG;
Package Body
CREATE OR REPLACE PACKAGE BODY CTX_PKG
IS
GC$SESSION_ID VARCHAR2 (100);
PROCEDURE SET_SESSION_ID (IN_SESSION_ID VARCHAR2)
IS
BEGIN
GC$SESSION_ID := IN_SESSION_ID;
DBMS_SESSION.SET_IDENTIFIER (IN_SESSION_ID);
END;
PROCEDURE CLEAR_SESSION (IN_SESSION_ID VARCHAR2)
IS
BEGIN
DBMS_SESSION.SET_IDENTIFIER (IN_SESSION_ID);
DBMS_SESSION.CLEAR_IDENTIFIER;
END;
PROCEDURE SET_CONTEXT (IN_NAME VARCHAR2, IN_VALUE VARCHAR2)
IS
BEGIN
DBMS_SESSION.SET_CONTEXT ('MY_CTX',
IN_NAME,
IN_VALUE,
USER,
GC$SESSION_ID);
END;
END CTX_PKG;
Now let's test context and my package
BEGIN
CTX_PKG.SET_SESSION_ID (222);
CTX_PKG.SET_CONTEXT ('my_name', 'Mahmoud A. El-Sayed');
CTX_PKG.SET_CONTEXT ('my_age', '26 YO');
END;
Now I set two context variable my_name, my_age
to query this variable I will use SYS_CONTEXT function
SELECT SYS_CONTEXT ('MY_CTX', 'my_name'), SYS_CONTEXT ('MY_CTX', 'my_age')
FROM DUAL;
The output will be
After insuring that my context and package working true let's now create view on table EMP to get employee in department at SCOTT schema
CREATE OR REPLACE VIEW EMP_IN_DEPRATMENT
AS
SELECT *
FROM EMP
WHERE DEPTNO = SYS_CONTEXT ('MY_CTX', 'deptno');
to filter view by employees in department 20 only you should execute this PLSQL block first
BEGIN
CTX_PKG.SET_SESSION_ID (222);
CTX_PKG.SET_CONTEXT ('deptno', '20');
END;
Now lets create select statement against EMP_IN_DEPARTMENT view and see the result
SELECT * FROM EMP_IN_DEPRATMENT;
The output result is like below
#2 Global Package Variables
I will use in where clause global package variables as parameter to filter data of the query of the view.
I will create package that hold every global variables which I will use it as parameters in view.
Package Specification
CREATE OR REPLACE PACKAGE GLB_VARIABLES
IS
GN$DEPTNO EMP.DEPTNO%TYPE;
PROCEDURE SET_DEPTNO (
IN_DEPTNO EMP.DEPTNO%TYPE);
FUNCTION GET_DEPTNO
RETURN EMP.DEPTNO%TYPE;
END;
Package Body
CREATE OR REPLACE PACKAGE BODY GLB_VARIABLES
IS
PROCEDURE SET_DEPTNO (
IN_DEPTNO EMP.DEPTNO%TYPE)
IS
BEGIN
GN$DEPTNO := IN_DEPTNO;
END;
FUNCTION GET_DEPTNO
RETURN EMP.DEPTNO%TYPE
IS
BEGIN
RETURN GN$DEPTNO;
END;
END;
Let's now create view filter its data by global variables in GLBL_VARIABLES package
CREATE OR REPLACE VIEW EMP_IN_DEPRATMENT2
AS
SELECT *
FROM EMP
WHERE DEPTNO =GLB_VARIABLES.GET_DEPTNO;
Now lets create select statement against EMP_IN_DEPARTMENT2 view and see the result
EXEC GLB_VARIABLES.SET_DEPTNO(20);
SELECT * FROM EMP_IN_DEPRATMENT2;
The output result is like below
3-Lookup Tables
another solution is to create lockup table for storing view parameters on it and build view based on the data stored in lockup table.
Thursday, July 26, 2018
Analytic Function
PL/SQL Analytic Functions
Introduced in Oracle 8i, analytic functions, also known as windowing functions, allow developers to perform tasks in SQL that were previously confined to procedural languages (Oracle, 2026).
Introduction
Probably the easiest way to understand analytic functions is to start by looking at aggregate functions. An aggregate function, as the name suggests, aggregates data from several rows into a single result row (Elmasri & Navathe, 2017). For example, we might use the AVG aggregate function to give us an average of all the employee salaries in the EMP table.
SELECT AVG(sal)
FROM emp;
AVG(SAL)
----------
2073.21429
SQL>
The GROUP BY clause allows us to apply aggregate functions to subsets of rows. For example, we might want to display the average salary for each department (Elmasri & Navathe, 2017).
SELECT deptno, AVG(sal)
FROM emp
GROUP BY deptno
ORDER BY deptno;
DEPTNO AVG(SAL)
---------- ----------
10 2916.66667
20 2175
30 1566.66667
SQL>
In both cases, the aggregate function reduces the number of rows returned by the query.
Analytic functions also operate on subsets of rows, similar to aggregate functions in GROUP BY queries, but they do not reduce the number of rows returned by the query (Oracle, 2026). For example, the following query reports the salary for each employee, along with the average salary of the employees within the department.
SET PAGESIZE 50
BREAK ON deptno SKIP 1 DUPLICATES
SELECT empno, deptno, sal,
AVG(sal) OVER (PARTITION BY deptno) AS avg_dept_sal
FROM emp;
EMPNO DEPTNO SAL AVG_DEPT_SAL
---------- ---------- ---------- ------------
7782 10 2450 2916.66667
7839 10 5000 2916.66667
7934 10 1300 2916.66667
7566 20 2975 2175
7902 20 3000 2175
7876 20 1100 2175
7369 20 800 2175
7788 20 3000 2175
7521 30 1250 1566.66667
7844 30 1500 1566.66667
7499 30 1600 1566.66667
7900 30 950 1566.66667
7698 30 2850 1566.66667
7654 30 1250 1566.66667
14 rows selected.
SQL>
This time AVG is an analytic function, operating on the group of rows defined by the contents of the OVER clause. This group of rows is known as a window, which is why analytic functions are sometimes referred to as window[ing] functions (Oracle, 2026). Notice how the AVG function is still reporting the departmental average, like it did in the GROUP BY query, but the result is present in each row, rather than reducing the total number of rows returned. This is because analytic functions are performed on a result set after all join, WHERE, GROUP BY and HAVING clauses are complete, but before the final ORDER BY operation is performed (Oracle, 2026).
Analytic Function Syntax
There are some variations in the syntax of the individual analytic functions, but the basic syntax for an analytic function is as follows (Oracle, 2026).
analytic_function([ arguments ]) OVER (analytic_clause)
The analytic_clause breaks down into the following optional elements.
[ query_partition_clause ] [ order_by_clause [ windowing_clause ] ]
The sub-elements of the analytic_clause each have their own syntax diagrams, shown here. Rather than repeat the syntax diagrams, the following sections describe what each section of the analytic_clause is used for.
query_partition_clause
The query_partition_clause divides the result set into partitions, or groups, of data. The operation of the analytic function is restricted to the boundary imposed by these partitions, similar to the way a GROUP BY clause affects the action of an aggregate function (Oracle, 2026). If the query_partition_clause is omitted, the whole result set is treated as a single partition. The following query uses an empty OVER clause, so the average presented is based on all the rows of the result set.
CLEAR BREAKS
SELECT empno, deptno, sal,
AVG(sal) OVER () AS avg_sal
FROM emp;
EMPNO DEPTNO SAL AVG_SAL
---------- ---------- ---------- ----------
7369 20 800 2073.21429
7499 30 1600 2073.21429
7521 30 1250 2073.21429
7566 20 2975 2073.21429
7654 30 1250 2073.21429
7698 30 2850 2073.21429
7782 10 2450 2073.21429
7788 20 3000 2073.21429
7839 10 5000 2073.21429
7844 30 1500 2073.21429
7876 20 1100 2073.21429
7900 30 950 2073.21429
7902 20 3000 2073.21429
7934 10 1300 2073.21429
SQL>
If we change the OVER clause to include a query_partition_clause based on the department, the averages presented are specifically for the department the employee belongs too.
BREAK ON deptno SKIP 1 DUPLICATES
SELECT empno, deptno, sal,
AVG(sal) OVER (PARTITION BY deptno) AS avg_dept_sal
FROM emp;
EMPNO DEPTNO SAL AVG_DEPT_SAL
---------- ---------- ---------- ------------
7782 10 2450 2916.66667
7839 10 5000 2916.66667
7934 10 1300 2916.66667
7566 20 2975 2175
7902 20 3000 2175
7876 20 1100 2175
7369 20 800 2175
7788 20 3000 2175
7521 30 1250 1566.66667
7844 30 1500 1566.66667
7499 30 1600 1566.66667
7900 30 950 1566.66667
7698 30 2850 1566.66667
7654 30 1250 1566.66667
SQL>
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 (Oracle, 2026). The following query uses the FIRST_VALUE function to return the first salary reported in each department. Notice we have partitioned the result set by the department, but there is no order_by_clause.
BREAK ON deptno SKIP 1 DUPLICATES
SELECT empno, deptno, sal,
FIRST_VALUE(sal IGNORE NULLS) OVER (PARTITION BY deptno) AS first_sal_in_dept
FROM emp;
EMPNO DEPTNO SAL FIRST_SAL_IN_DEPT
---------- ---------- ---------- -----------------
7782 10 2450 2450
7839 10 5000 2450
7934 10 1300 2450
7566 20 2975 2975
7902 20 3000 2975
7876 20 1100 2975
7369 20 800 2975
7788 20 3000 2975
7521 30 1250 1250
7844 30 1500 1250
7499 30 1600 1250
7900 30 950 1250
7698 30 2850 1250
7654 30 1250 1250
SQL>
Now compare the values of the FIRST_SAL_IN_DEPT column when we include an order_by_clause to order the siblings by ascending salary.
SELECT empno, deptno, sal,
FIRST_VALUE(sal IGNORE NULLS) OVER (PARTITION BY deptno ORDER BY sal ASC NULLS LAST) AS first_val_in_dept
FROM emp;
EMPNO DEPTNO SAL FIRST_VAL_IN_DEPT
---------- ---------- ---------- -----------------
7934 10 1300 1300
7782 10 2450 1300
7839 10 5000 1300
7369 20 800 800
7876 20 1100 800
7566 20 2975 800
7788 20 3000 800
7902 20 3000 800
7900 30 950 950
7654 30 1250 950
7521 30 1250 950
7844 30 1500 950
7499 30 1600 950
7698 30 2850 950
SQL>
In this case the "ASC NULLS LAST" keywords are unnecessary as ASC is the default for an order_by_clause and NULLS LAST is the default for ASC orders. When ordering by DESC, the default is NULLS FIRST (Oracle, 2026).
It is important to understand how the order_by_clause affects display order. The order_by_clause is guaranteed to affect the order of the rows as they are processed by the analytic function, but it may not always affect the display order. As a result, you must always use a conventional ORDER BY clause in the query if display order is important. Do not rely on any implicit ordering done by the analytic function. Remember, the conventional ORDER BY clause is performed after the analytic processing, so it will always take precedence (Oracle, 2026).
windowing_clause
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, or whole result set if no partitioning clause is used. 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. The windowing_clause has two basic forms (Oracle, 2026).
RANGE BETWEEN start_point AND end_point
ROWS BETWEEN start_point AND end_point
When using ROWS BETWEEN, you are indicating a specific number of rows relative to the current row, either directly, or via an expression. Assuming you don't cross a partition boundary, that number of rows is fixed. In contrast, when you use RANGE BETWEEN you are referring to a range of values in a specific column relative to the value in the current row. As a result, Oracle doesn't know how many rows are included in the range until the ordered set is created (Oracle, 2026).
It is possible to omit the BETWEEN keyword and specify a single RANGE/ROWS endpoint. In this case, Oracle assumes your specified RANGE/ROWS is the start point and the end point it the current row. I would advise against using this syntax as it will be unclear to anyone who doesn't understand this default action.
Possible values for "start_point" and "end_point" are (Oracle, 2026):
- UNBOUNDED PRECEDING: The window starts at the first row of the partition, or the whole result set if no partitioning clause is used. Only available for start points.
- UNBOUNDED FOLLOWING: The window ends at the last row of the partition, or the whole result set if no partitioning clause is used. Only available for end points.
- CURRENT ROW: The window starts or ends at the current row. Can be used as start or end point.
- value_expr PRECEDING: A physical or logical offset before the current row using a constant or expression that evaluates to a positive numerical value. When used with
RANGE, it can also be an interval literal if theorder_by_clauseuses a DATE column. - value_expr FOLLOWING: As above, but an offset after the current row.
The documentation states the start point must always be before the end point, but this is not true, as demonstrated by this rather silly, but valid, query (Oracle, 2026).
SELECT empno, deptno, sal,
AVG(sal) OVER (PARTITION BY deptno ORDER BY sal ROWS BETWEEN 0 PRECEDING AND 0 PRECEDING) AS avg_of_current_sal
FROM emp;
EMPNO DEPTNO SAL AVG_OF_CURRENT_SAL
---------- ---------- ---------- ------------------
7934 10 1300 1300
7782 10 2450 2450
7839 10 5000 5000
7369 20 800 800
7876 20 1100 1100
7566 20 2975 2975
7788 20 3000 3000
7902 20 3000 3000
7900 30 950 950
7654 30 1250 1250
7521 30 1250 1250
7844 30 1500 1500
7499 30 1600 1600
7698 30 2850 2850
SQL>
In fact, the start point must be before or equal to the end point. In addition, the current row does not have to be part of the window. The window can be defined to start and end before or after the current row (Oracle, 2026).
For analytic functions that support the windowing_clause, the default action is "RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW". The following query is similar to one used previously to report the employee salary and average department salary, but now we have included an order_by_clause so we also get the default windowing_clause.
SELECT empno, deptno, sal,
AVG(sal) OVER (PARTITION BY deptno ORDER BY sal) AS avg_dept_sal_sofar
FROM emp;
EMPNO DEPTNO SAL AVG_DEPT_SAL_SOFAR
---------- ---------- ---------- ------------------
7934 10 1300 1300
7782 10 2450 1875
7839 10 5000 2916.66667
7369 20 800 800
7876 20 1100 950
7566 20 2975 1625
7788 20 3000 2175
7902 20 3000 2175
7900 30 950 950
7654 30 1250 1150
7521 30 1250 1150
7844 30 1500 1237.5
7499 30 1600 1310
7698 30 2850 1566.66667
SQL>
There are two things to notice here (Oracle, 2026):
- The addition of the
order_by_clausewithout awindowing_clausemeans the query is now returning a running average. - The default
windowing_clauseis "RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW", not "ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW". The fact it is RANGE, not ROWS, means it stops at the first occurrence of the value in the current row, even if that is several rows earlier. As a result, duplicate rows are only included in the average when the salary value changes. You can see this in the last two records of department 20 and in the second and third records of department 30.
In my opinion, the default windowing_clause should have been "ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING". This would make the accidental inclusion of the windowing_clause much less confusing.
The following query shows one method for accessing data from previous and following rows within the current row using the windowing_clause. This can also be accomplished with LAG and LEAD (Oracle, 2026).
CLEAR BREAKS
SELECT empno, deptno, sal,
FIRST_VALUE(sal) OVER (ORDER BY sal ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS previous_sal,
LAST_VALUE(sal) OVER (ORDER BY sal ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS next_sal
FROM emp;
EMPNO DEPTNO SAL PREVIOUS_SAL NEXT_SAL
---------- ---------- ---------- ------------ ----------
7369 20 800 800 950
7900 30 950 800 1100
7876 20 1100 950 1250
7521 30 1250 1100 1250
7654 30 1250 1250 1300
7934 10 1300 1250 1500
7844 30 1500 1300 1600
7499 30 1600 1500 2450
7782 10 2450 1600 2850
7698 30 2850 2450 2975
7566 20 2975 2850 3000
7788 20 3000 2975 3000
7902 20 3000 3000 5000
7839 10 5000 3000 5000
SQL>
Using Analytic Functions
The best way to understand what analytic functions are capable of is to play around with them. This article contains links to other articles I've written about specific analytic functions and the following documentation links list all analytic functions available in Oracle 12c Release 1 (Oracle, 2026). The "*" indicates that these functions allow for the full analytic syntax, including the windowing_clause.
AVG *
CLUSTER_DETAILS
CLUSTER_DISTANCE
CLUSTER_ID
CLUSTER_PROBABILITY
CLUSTER_SET
CORR *
COUNT *
COVAR_POP *
COVAR_SAMP *
CUME_DIST
DENSE_RANK
FEATURE_DETAILS
FEATURE_ID
FEATURE_SET
FEATURE_VALUE
FIRST
FIRST_VALUE *
LAG
LAST
LAST_VALUE *
LEAD
LISTAGG
MAX *
MEDIAN
MIN *
NTH_VALUE *
NTILE
PERCENT_RANK
PERCENTILE_CONT
PERCENTILE_DISC
PREDICTION
PREDICTION_COST
PREDICTION_DETAILS
PREDICTION_PROBABILITY
PREDICTION_SET
RANK
RATIO_TO_REPORT
REGR_ (Linear Regression) Functions *
ROW_NUMBER
STDDEV *
STDDEV_POP *
STDDEV_SAMP *
SUM *
VAR_POP *
VAR_SAMP *
VARIANCE *
References
Elmasri, R., & Navathe, S. B. (2017). Fundamentals of database systems (7th ed.). Pearson.
Oracle. (2026). Database SQL language reference: Analytic functions (26). Oracle Help Center. https://docs.oracle.com/en/database/oracle/oracle-database/26/sqlrf/Analytic-Functions.html