FIRST_VALUE()

SQL · Window Functions

Theory

FIRST_VALUE(column) returns the value of column from the first row of the window frame, using the window ORDER BY. With PARTITION BY dept ORDER BY salary DESC, the first row of each department is its highest earner, so FIRST_VALUE(name) puts the top earner's name on every row of that department.

The frame matters, but for FIRST_VALUE the default frame (from the start of the partition up to the current row) always contains the first row, so the default usually works. You can also pull a different column from the first row, which is the main advantage over MIN or MAX: MAX(salary) tells you the top salary, while FIRST_VALUE(name) ORDER BY salary DESC tells you who earns it. If there is a tie for first place, which tied row comes first is not defined unless you add a tie-breaker column.

Why we use it

Comparing every row with the 'best' or 'earliest' row of its group is common: gap to the top earner, a customer's first product, the opening price of the day. FIRST_VALUE brings that row's value onto every row without a self-join.

When to use it

Use FIRST_VALUE when you need a value (often a non-numeric one like a name) from the first row of an ordered group. If you only need the smallest or largest number, MIN or MAX OVER is simpler.

How it works

  1. Rows split by PARTITION BY and sorted by ORDER BY
  2. Build the frame for the current row
  3. Default frame starts at the first row of the partition
  4. Read the column value from the first row of the frame
  5. Same first value shown on each row of the group

Syntax

FIRST_VALUE(column) OVER (PARTITION BY col ORDER BY col)

Example

SELECT name, department, salary,
  FIRST_VALUE(name) OVER (PARTITION BY department ORDER BY salary DESC) AS top_earner
FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic