Smart SQL Tools for Every Database

PostgreSQL Function Reference

PostgreSQL LAG() Function

Learn how to use the PostgreSQL LAG window function for ranking, offsets, and analytical queries.

Back to SQL Function Explorer

Syntax

LAG(column, offset) OVER (ORDER BY column)

Basic Example

SELECT name, salary,
       LAG(column, offset) OVER (ORDER BY column) AS result
FROM employees;

When to use it

Use LAG when you need a window operation in PostgreSQL. This page is designed as a quick reference for developers, analysts, and students comparing SQL dialects.

Common mistake

A common mistake is copying syntax from another SQL dialect. Always verify function name, argument order, and formatting tokens for PostgreSQL.

Related SQL Functions

FAQ

Is LAG available in every SQL database?

Not always. Function names and argument behavior can differ across SQL engines.

Can I use this in SELECT queries?

Yes. Most SQL functions are commonly used in SELECT and often in WHERE, ORDER BY, and GROUP BY depending on the function.