Preamble
The Oracle/PLSQL FIRST_VALUE function returns the first value in an ordered set of values from the analytical window. It shares some similarities with FIRST_VALUE and NTH_VALUE.
Oracle/PLSQL syntax of FIRST_VALUE function
FIRST_VALUE (_id)
[RESPECT NULLS | IGNORE NULLS]
OVER ([query_partition_clause_id] [order_by_clause_id])
The following syntax is also the accepted format:
FIRST_VALUE (_id [RESPECT NULLS | IGNORE NULLS])
OVER ([query_partition_clause_id] [order_by_clause_id])
where:
- expression_id – The column or expression whose first value you wish to return.
- If this parameter is omitted, the default value is RESPECT NULLS, which includes NULL values, and it controls whether NULL values are enabled or ignored in the analysis window.
- The optional query_partition_clause_id allows you to categorize results based on one or more expressions.
- Ordering the data in each section is accomplished using the optional order_by_clause_id field.
- The rows in the analysis window for evaluation are specified by the optional windowing_clause_id; it is essential to use the correct windowing_clause to prevent getting unexpected results.
- Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i
| windowing_clause | Description |
| RANGING FROM UNBOUNDED PREVIOUS TO CURRENT ROW | The last line in the window updates automatically by default along with the current line. |
| RANGE INTO UNBOUNDED FOLLOWING AND CURRENT ROW | The second line changes as the first line in the window does. |
| RANGING FROM UNBOUNDED PRIOR TO UNBOUNDED PRECEDING | Every line, regardless of which one is currently selected, is visible in the window. |
DDL / DML for examples
If you want to follow this guide, use the DDL to create the table and DML to fill in the data. Then try the examples in your own database!
DDL / DML for FIRST_VALUE
If you have an Oracle database and want to see what the FIRST_VALUE analytics function can do, you can find the DDL and DML you need below.
Simply adhere to the directions to finish your database. Then click the link to go back to the FIRST_VALUE training materials and practice the examples there.
Example DDL
The instructions required to create the tables used in the FIRST_VALUE example are known as DDL, or data definition language.
Observe the DDL guidelines below in your Oracle database:
CREATE TABLE empls
( empl_id number(6) NOT NULL,
f_name varchar2(25) NOT NULL,
l_name varchar2(30) NOT NULL,
salary_id number(8,2),
depart_id number(4),
CONSTRAINT emp_id_pk1 PRIMARY KEY (empl_id);
DML example
A DML is a language used for data manipulation. You will need to run these INSERT commands in your Oracle database to add the data:
In your Oracle database, execute the following DML operations:
insert into empls (empl_id, f_name, l_name, salary_id, depart_id)
(100, 'Anita', 'Borg', 2500.10);
insert into empls (empl_id, f_name, l_name, salary_id, depart_id)
(200, "Alfred", "Aho", 3200.10);
insert into empls (empl_id, f_name, l_name, salary_id, depart_id)
(300, "Bill", "Gates", 2100.10);
insert into empls (empl_id, f_name, l_name, salary_id, depart_id)
(400, 'Linus', 'Torvalds', 3700.20);
insert into empls (empl_id, f_name, l_name, salary_id, depart_id)
(500, "Michael", "Dell", 3100.20);
insert into empls (empl_id, f_name, l_name, salary_id, depart_id)
(600, "Nello", "Cristianini", 2950, 20);
insert into empls (empl_id, f_name, l_name, salary_id, depart_id)
(700, "Rasmus", "Lerdorf", 4900.20);
insert into empls (empl_id, f_name, l_name, salary_id, depart_id)
(800, "Steve", "Jobs", 2600.30);
insert into empls (empl_id, f_name, l_name, salary_id, depart_id)
900, 'Thomas', 'Kyte', 5000.30;
Example
Let’s examine some FIRST_VALUE function examples from Oracle and learn how to use FIRST_VALUE in PLSQL.
highest wage for all personnel
Use the FIRST_VALUE function to return the highest salary in the employee table by starting with a straightforward example. Because we are analyzing the entire employees table in this example, we are not using the query_partition_clause.
In this illustration, a table of employees contains the information below:
| EMPLOYEE_ID | FIRST_NAME | LAST_NAME | SALARY | DEPARTMENT_ID |
| 100 | Anita | Borg | 2500 | 10 |
| 200 | Alfred | Aho | 3200 | 10 |
| 300 | Bill | Gates | 2100 | 10 |
| 400 | Linus | Torvalds | 3700 | 20 |
| 500 | Michael | Dell | 3100 | 20 |
| 600 | Nello | Cristianini | 2950 | 20 |
| 700 | Rasmus | Lerdorf | 4900 | 20 |
| 800 | Steve | Jobs | 2600 | 30 |
| 900 | Thomas | Kyte | 5000 | 30 |
To find the highest salary, enter the following SELECT:
SELECT DISTINCT FIRST_VALUE(salary_id)
OVER (ORDER BY salary_id DESC)
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
AS "HIGH"
FROM empls;
Your desired result should be:
| HIGH |
| 5000 |
In this case, FIRST_VALUE (salary_id) returns the highest salary value that was specified. ORDER BY salary DESC tells the analysis window to sort the salary data in descending order. This is done in the analysis window. To ensure that all rows are included regardless of the current row, use the parameter windowing_clause = RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.
Additionally, we did not need to enable query_partition_clause_id to separate the data because we only wanted the highest salary in the table.
The depart_id employee earning the most
Let’s now demonstrate how to use query_partition_clause_id with FIRST_VALUE. Let’s return the highest salary for depart_id 10 and 20 in the example below.
Enter the following SQL query using the same table:
SELECT DISTINCT depart_id, FIRST_VALUE(salary_id)
OVER (PARTITION BY depart_id ORDER BY salary_id DESC)
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
AS "HIGH"
FROM empls
WHERE depart_id in (10,20)
ORDER BY depart_id;
Here are the results you should get:
| DEPART_ID | HIGH |
| 10 | 3200 |
| 20 | 4900 |
In this case, FIRST_VALUE (salary_id) returns the highest salary value that was specified. When you type PARTITION BY DEPARTMENT_ID ORDER BY DESC salary in the analysis window, the results will be split up by DEPARTMENT_ID and the salary data will be put in descending order.
Lowest salary by department_id
Let’s now demonstrate how to retrieve the lowest salaries for department_ids 10 and 20 using the FIRST_VALUE function.
Again, utilizing the data in the table, enter the following SQL query:
SELECT DISTINCT depart_id, FIRST_VALUE(salary_id)
OVER (PARTITION BY depart_id ORDER BY salary_id ASC)
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
AS "LOW"
FROM empls
WHERE depart_id in (10,20)
ORDER BY depart_id;
The outcomes you should see are as follows:
| DEPART_ID | LOW |
| 10 | 2100 |
| 20 | 2950 |
The sorting order by section in this example has been changed to PARTITION BY DEPART_ID ORDER BY salary ASC, and as a result, we now obtain the lowest salary based on DEPART_ID.
FIRST_VALUE and LAST_VALUE : Problem Solving using Analytic Functions
About Enteros
Enteros offers a patented database performance management SaaS platform. It finds the root causes of complex database scalability and performance problems that affect business across a growing number of cloud, RDBMS, NoSQL, and machine learning database platforms.
The views expressed on this blog are those of the author and do not necessarily reflect the opinions of Enteros Inc. This blog may contain links to the content of third-party sites. By providing such links, Enteros Inc. does not adopt, guarantee, approve, or endorse the information, views, or products available on such sites.
Are you interested in writing for Enteros’ Blog? Please send us a pitch!
RELATED POSTS
$11B at Stake — How Supply-Chain Delays Are Forcing Airlines to Fly Older Planes
- 7 November 2025
- Software Engineering
Introduction The aviation industry is entering 2025 at a critical inflection point. According to a joint report by the International Air Transport Association (IATA) and Oliver Wyman, global airlines could face over $11 billion in additional costs this year due to persistent supply-chain disruptions. The most visible consequence is the forced reliance on older, less … Continue reading “$11B at Stake — How Supply-Chain Delays Are Forcing Airlines to Fly Older Planes”
Enhancing Cost Estimation and Attribution in the Technology Sector: How Enteros Uses AI Performance Management to Drive Financial and Operational Efficiency
- 6 November 2025
- Database Performance Management
Introduction In the dynamic world of the technology sector, cost estimation and attribution have become as critical as innovation itself. As companies expand their cloud infrastructures, deploy AI-driven workloads, and manage vast databases across multi-cloud ecosystems, the challenge lies in maintaining both financial precision and performance optimization. Technology enterprises need a clear understanding of where … Continue reading “Enhancing Cost Estimation and Attribution in the Technology Sector: How Enteros Uses AI Performance Management to Drive Financial and Operational Efficiency”
Optimizing Retail Budgeting and Performance: How Enteros Combines AI SQL and AI Performance Management to Transform Database Efficiency
Introduction In the fast-paced retail sector, success depends on delivering seamless customer experiences, managing inventory efficiently, and controlling operational costs — all while keeping up with dynamic market demands. Retailers today rely on a digital ecosystem powered by databases, SaaS platforms, and AI technologies to manage everything from transactions and supply chains to personalized recommendations. … Continue reading “Optimizing Retail Budgeting and Performance: How Enteros Combines AI SQL and AI Performance Management to Transform Database Efficiency”
Revolutionizing the Fashion Sector: How Enteros Leverages Generative AI and AI Performance Management to Optimize SaaS Database Efficiency
- 5 November 2025
- Database Performance Management
Introduction The global fashion industry has always been a beacon of creativity, speed, and transformation. From runway collections to e-commerce platforms, the sector thrives on rapid innovation and data-driven decision-making. In today’s digital-first world, fashion enterprises—from luxury retailers to fast-fashion brands—are evolving into technology-driven organizations, heavily dependent on SaaS platforms, AI tools, and cloud databases … Continue reading “Revolutionizing the Fashion Sector: How Enteros Leverages Generative AI and AI Performance Management to Optimize SaaS Database Efficiency”