The FIRST Function in Oracle SQL / PLSQL is used to get the FIRST value in an ordered set of records.
The FIRST function can be used with the following functions.
- STDDEV() and
Syntax for using the FIRST function in Oracle SQL / PLSQL is ;
AGGREGATE_FUNCTION() KEEP (RANK_FUNCTION FIRST ORDER BY AGGREGATE_FUNCTION(column))
GROUP BY column(s);
Suppose we have a table named ‘employee’ as shown below:
Now, suppose we want to get the name of the department where the total salary being given is lesser than the total of salary in any other department, then we can achieve the same using FIRST function as:
SELECT MAX(department) KEEP (DENSE_RANK FIRST ORDER BY SUM(salary)) Min_Sal_Department FROM employee GROUP BY department;
We will get the following result:
Here we can see that we have successfully fetched the name of the department in which the total salary is lesser than total salary of any other department.