Lag Function SQL

Galaxy Glossary

How can I access data from the previous row in a SQL table?

The LAG() function in SQL allows you to access values from previous rows within a result set. This is particularly useful for tasks like calculating running totals, identifying trends, or comparing data points across consecutive rows.
Sign up for the latest in SQL knowledge from the Galaxy Team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Description

The LAG() function is a powerful tool in SQL for performing calculations and comparisons based on the values of preceding rows. Imagine you have a table of sales data, and you want to calculate the difference in sales between each day. Without LAG(), you'd need a self-join or a subquery, which can become complex and less readable. LAG() simplifies this process by allowing you to directly reference the value from the previous row. It's crucial for tasks involving time series analysis, data comparisons, and creating running totals. The function takes the column you want to reference from the previous row, the offset (how many rows back), and optionally a default value if the offset is beyond the first row. This makes it incredibly versatile for various data analysis scenarios.

Why Lag Function SQL is important

LAG() is essential for analyzing trends and patterns in data. It simplifies complex calculations, making queries more readable and maintainable. Its use in time-series analysis and data comparisons is widespread in various applications.

Example Usage


CREATE TABLE SalesData (
    Date DATE,
    Sales INT
);

INSERT INTO SalesData (Date, Sales) VALUES
('2023-10-26', 100),
('2023-10-27', 120),
('2023-10-28', 150),
('2023-10-29', 130);

SELECT
    Date,
    Sales,
    LAG(Sales, 1, 0) OVER (ORDER BY Date) AS PreviousSales
FROM
    SalesData;

Common Mistakes

Want to learn about other SQL terms?