The document provides examples of using analytical (window) functions in an Oracle database with the employee table (EMP). It explains various functions such as ROW_NUMBER, RANK, DENSE_RANK, NTILE, SUM, AVG, LEAD, and LAG, along with SQL queries demonstrating their usage. Additionally, it includes a query example that combines ranking and cumulative salary calculations within departments.
The document provides examples of using analytical (window) functions in an Oracle database with the employee table (EMP). It explains various functions such as ROW_NUMBER, RANK, DENSE_RANK, NTILE, SUM, AVG, LEAD, and LAG, along with SQL queries demonstrating their usage. Additionally, it includes a query example that combines ranking and cumulative salary calculations within departments.
Sure! Below is an example of how you can use analytical (window) functions in an Oracle database with
the enp table, which is a common sample table used for demonstration purposes. The enP table
typically contains information about employees, such as
their EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, and DEPTNO
Example of the Emp table schema:
EMPNO | ENAME JOB MGR | HIREDATE SAL comm =| DEPTNO
7839 KING PRESIDENT NULL | 1981-06-09 so00 | NULL 10
7566 JONES MANAGER 7639 | 1981-06-13 2975 | NULL 20
7698 BLAKE MANAGER 7839 | 1981-06-15 2850 | NULL 30
7782 CLARK MANAGER 7839 | 1981-06-14 2450 | NULL 10
7788 Scott ANALYST 7566 | 1981-06-13 3000 | NULL 20
7902 FORD ANALYST 7566 | 1981-06-20 3000 | NULL 20
7844 TURNER — | SALESMAN. 7698 | 1981-06-04 1500 | 0.00 30
7900 JAMES CLERK 7698 | 1981-06-23 950 NULL 30
7654 MARTIN SALESMAN. 7698 | 1981-06-07 1250 | 1400 30
7499 ALLEN SALESMAN. 7698 =| 1981-06-01 1600 | 300 30
7521 WARD SALESMAN. 7698 | 1981-06-08 1250 | 500 30
7934 MILLER CLERK 7782 | 1981-06-21 1300 | NULL 10
< >
Analytical (Window) Functions Examples
1. ROW_NUMBERO - Assigns a unique sequential number to rows within a result set, based on a
specified order.
st Bony
SELECT EMPNO, ENAME, JOB, SAL, ROW_NUNBER() OVER (ORDER BY SAL DESC) AS ROW_NUM FROM EMP;
This will return the employees with a row number assigned based on their salary in descending order.
2. RANK( - Assigns a rank to each row within a partition of a result set, with gaps between ranks if
there are ties,
7 DowSELECT EMPNO, ENAME, JOB, SAL, RANK() OVER (ORDER BY SAL DESC) AS SALARY_RANK FROM EMP;
This will assign a rank to each employee based on their salary. If two employees have the same salary,
they will receive the same rank, but the next rank will have a gap.
3. DENSE_RANK( - Similar to RaNK() , but does not leave gaps between ranks if there are ties.
a Bow
SELECT EMPNO, ENAME, JOB, SAL, DENSE_RANK() OVER (ORDER BY SAL DESC) AS DENSE_SALARY_RANK
FROM EMP 5
This will assign ranks without gaps for employees with the same salary.
4, NTILE( - Divides the result set into a specified number of roughly equal parts and assigns a
bucket number to each row.
i Bows
SELECT EMPNO, ENAME, JOB, SAL, NTILE(4) OVER (ORDER BY SAL DESC) AS QUARTILE FROM EMP;
This divides the employees into 4 quartiles based on their salary.
5. SUMO - Calculates a cumulative sum within the window (partition).
sa O cory
SELECT EMPNO, ENAME, 303, SAL, SUM(SAL) OVER (ORDER BY HIREDATE) AS CUMULATIVE_SALARY
FROM. EMP;
This calculates a running total of salary in the order of hire date.
6. AVGQ) - Computes the average salary within a partition or window.
ol Bow
SELECT EMPNO, ENAME, JOB, SAL, AVG(SAL) OVER (PARTITION BY DEPTNO) AS AVG_SALARY_PER_DEPT
FROM EMP;
This calculates the average salary for each department, partitioned by DEPTNO
7. LEAD( - Accesses the value of a row that comes after the current row, based on a specified order.
Pe O conySELECT EMPNO, ENAME, SAL, LEAD(SAL) OVER (ORDER BY SAL DESC) AS NEXT_SALARY FROM EMP;
This returns the salary of the next employee in the salary order.
8. LAG( - Accesses the value of a row that comes before the current row.
“4 O cory
SELECT EMPNO, ENAME, SAL, LAG(SAL) OVER (ORDER BY SAL DESC) AS PREV SALARY FROM EMP;
This returns the salary of the previous employee in the salary order.
Example with Partitioning and Ordering
Let's say you want to find the cumulative salary for each department and also get the rank of
employees within each department:
sal O cory
SELECT EMPNO, ENAME, JOB, SAL, DEPTNO, RANK() OVER (PARTITION BY DEPTNO ORDER BY SAL
DESC) AS DEPT_RANK, SUM(SAL) OVER (PARTITION BY DEPTNO ORDER BY HIREDATE) AS
CUMULATIVE_SALARY FROM EMP
This query will return:
* Employee ranks based on salary within each department ( DEPT_RANK .
* A cumulative salary sum for each department in the order of hire date ( CUMULATIVE_SALARY ).