{
    "url": "https://delaparra-services.nl/knowledge-hub/lag-lead",
    "locale": "en",
    "title": "LAG / LEAD: Knowledge hub | DeLaParra Services",
    "description": "Read a value from a previous (LAG) or following (LEAD) row in the window — perfect for diffs over time.",
    "alternates": {
        "en": "https://delaparra-services.nl/knowledge-hub/lag-lead",
        "nl": "https://delaparra-services.nl/nl/kennisbank/lag-lead"
    },
    "formats": {
        "csv": "https://delaparra-services.nl/knowledge-hub/lag-lead.csv",
        "md": "https://delaparra-services.nl/knowledge-hub/lag-lead.md",
        "json": "https://delaparra-services.nl/knowledge-hub/lag-lead.json",
        "zip": "https://delaparra-services.nl/knowledge-hub/lag-lead.zip",
        "pdf": "https://delaparra-services.nl/knowledge-hub/lag-lead.pdf"
    },
    "downloads": [
        {
            "url": "https://delaparra-services.nl/knowledge-hub/lag-lead.csv",
            "encodingFormat": "text/csv",
            "name": "lag-lead.csv"
        },
        {
            "url": "https://delaparra-services.nl/knowledge-hub/lag-lead.json",
            "encodingFormat": "application/json",
            "name": "lag-lead.json"
        },
        {
            "url": "https://delaparra-services.nl/knowledge-hub/lag-lead.md",
            "encodingFormat": "text/markdown",
            "name": "lag-lead.md"
        },
        {
            "url": "https://delaparra-services.nl/knowledge-hub/lag-lead.pdf",
            "encodingFormat": "application/pdf",
            "name": "lag-lead.pdf"
        },
        {
            "url": "https://delaparra-services.nl/knowledge-hub/lag-lead.zip",
            "encodingFormat": "application/zip",
            "name": "lag-lead.zip"
        },
        {
            "url": "https://delaparra-services.nl/knowledge-hub/lag-lead/skills.zip",
            "encodingFormat": "application/zip",
            "name": "lag-lead-skills.zip"
        }
    ],
    "markdown": "1. [Home](<https://delaparra-services.nl/>) \n1. [Knowledge hub](<https://delaparra-services.nl/knowledge-hub>) \n1. [LAG / LEAD](<https://delaparra-services.nl/knowledge-hub/lag-lead>)\n\n  \n\nCategory: SQL \n\n# LAG / LEAD\n\n  Explained by [Reinier de la Parra](<https://delaparra-services.nl/about>)  Updated November 25, 2026    \n\n## What is LAG / LEAD?\n\n \n\nLAG / LEAD: Read a value from a previous (LAG) or following (LEAD) row in the window — perfect for diffs over time. ` LAG ` and ` LEAD ` are window functions that read a value from a previous or following row. ` revenue - LAG(revenue) OVER (ORDER BY day) ` calculates the change from the day before, for every day, in one query.\n\n \n\nDefinition source [Coders Lingo](<https://coderslingo.com/resources/open-dataset/>), [CC BY 4.0](<https://creativecommons.org/licenses/by/4.0/>)\n\n \n\n## Frequently asked questions about LAG / LEAD\n\n  \n\n### How is LAG / LEAD used in practice?\n\n \n\nThey make comparisons over time simple: growth per month, time between a customer's orders, or the next scheduled appointment. Without them, you would need self-joins or post-processing in application code.\n\n    \n\n### What should you keep in mind with LAG / LEAD?\n\n \n\nThe ` OVER ` clause controls the order, and ` PARTITION BY ` restarts the comparison per group, such as per customer. The first row has no previous row, so ` LAG ` returns ` NULL `; you can pass a default as the third argument. MySQL 8, PostgreSQL and SQLite all support these functions, so most modern stacks can use them.\n\n    \n\n### What does an example of LAG / LEAD look like?\n\n \n\n```\nSELECT day, revenue, revenue - LAG(revenue) OVER (ORDER BY day) AS delta FROM daily_sales;\n```\n\n \n\n## At a glance\n\n   CategorySQL Explained by[Reinier de la Parra](<https://delaparra-services.nl/about>) UpdatedNovember 25, 2026\n\n \n\n## More SQL terms\n\n \n\n- [hash index](<https://delaparra-services.nl/knowledge-hub/hash-index>) \n- [HAVING](<https://delaparra-services.nl/knowledge-hub/having>) \n- [INNER JOIN](<https://delaparra-services.nl/knowledge-hub/inner-join>) \n- [LEFT (OUTER) JOIN](<https://delaparra-services.nl/knowledge-hub/left-outer-join>) \n- [OVER](<https://delaparra-services.nl/knowledge-hub/over>) \n- [PARTITION BY](<https://delaparra-services.nl/knowledge-hub/partition-by>)\n\n \n\n[All terms starting with L](<https://delaparra-services.nl/knowledge-hub#letter-l>)\n\n \n\n## Skills\n\n \n\n- ### SQL Performance Optimization Assistant\n\n \n\n'Universal SQL performance optimization assistant for comprehensive query tuning, indexing strategies, and database performance analysis across all SQL databases (MySQL, PostgreSQL, SQL Server, Oracle). Provides execution plan analysis, pagination optimization, batch operations, and performance monitoring guidance.'\n\n [View skill](<https://delaparra-services.nl/skills/sql-optimization>) [Download zip](<https://delaparra-services.nl/skills/sql-optimization/download>) \n- ### SQL Code Review\n\n \n\n'Universal SQL code review assistant that performs comprehensive security, maintainability, and code quality analysis across all SQL databases (MySQL, PostgreSQL, SQL Server, Oracle). Focuses on SQL injection prevention, access control, code standards, and anti-pattern detection. Complements SQL optimization prompt for complete development coverage.'\n\n [View skill](<https://delaparra-services.nl/skills/sql-code-review>) [Download zip](<https://delaparra-services.nl/skills/sql-code-review/download>) \n- ### Laravel Database Optimization\n\n \n\nLaravel database optimization patterns. Use when writing Eloquent queries, creating migrations, configuring caching, debugging slow queries, or optimizing database performance. Triggers on tasks involving N+1 queries, indexing, Redis caching, pagination, or database transactions.\n\n [View skill](<https://delaparra-services.nl/skills/laravel-database-optimization>) [Download zip](<https://delaparra-services.nl/skills/laravel-database-optimization/download>)\n\n [Download all skills (zip)](<https://delaparra-services.nl/knowledge-hub/lag-lead/skills.zip>)\n\n \n\n## Manuals\n\n \n\n- [Writing SQL queries and migrations](<https://delaparra-services.nl/manuals/sql-queries-en-migraties>) \n- [PHP and Composer](<https://delaparra-services.nl/manuals/php-en-composer>)\n\n \n\n## Related services\n\n \n\n- [Custom software and web applications](<https://delaparra-services.nl/services#service-03>)\n\nPerhaps your customers need somewhere to download their invoices, or your staff are managing bookings in a spreadsheet. \n- [AI code review](<https://delaparra-services.nl/services#service-05>)\n\nWhen you push your new change, I will review your code, help your agent avoid the same mistake again, and get your app ready for deployment.\n\n \n\n## Need any help?\n\n \n\nStuck on one of these in your own project? Tell me what you are working on and I will tell you how I would approach it.\n\n  [Get in touch](<https://delaparra-services.nl/availability>)\n\n[HTML version](https://delaparra-services.nl/knowledge-hub/lag-lead)\n",
    "structured_data": {
        "@context": "https://schema.org",
        "@graph": [
            {
                "@type": "Organization",
                "@id": "https://delaparra-services.nl/#practice",
                "logo": "https://delaparra-services.nl/logo.png",
                "name": "DeLaParra Services",
                "legalName": "DeLaParra Services",
                "url": "https://delaparra-services.nl/",
                "description": "Bespoke software, web applications and mobile apps, developed in Laravel by a single developer in Groningen. Fixed price agreed in advance, full source code provided upon delivery.",
                "email": "info@delaparra-services.nl",
                "address": {
                    "@type": "PostalAddress",
                    "addressLocality": "Groningen",
                    "addressRegion": "Groningen",
                    "addressCountry": "NL"
                },
                "areaServed": "Groningen, the Netherlands",
                "knowsLanguage": [
                    "en",
                    "nl"
                ],
                "knowsAbout": [
                    "Web design and development",
                    "App development",
                    "Custom software and web applications",
                    "OpenSpec",
                    "AI code review",
                    "Your own coding agent"
                ],
                "founder": {
                    "@id": "https://delaparra-services.nl/#person"
                }
            },
            {
                "@type": "FAQPage",
                "@id": "https://delaparra-services.nl/knowledge-hub/lag-lead#webpage",
                "url": "https://delaparra-services.nl/knowledge-hub/lag-lead",
                "name": "LAG / LEAD: Knowledge hub | DeLaParra Services",
                "description": "Read a value from a previous (LAG) or following (LEAD) row in the window — perfect for diffs over time.",
                "isPartOf": {
                    "@id": "https://delaparra-services.nl/#website"
                },
                "about": {
                    "@type": "DefinedTerm",
                    "@id": "https://delaparra-services.nl/knowledge-hub/lag-lead#term",
                    "name": "LAG / LEAD",
                    "description": "Read a value from a previous (LAG) or following (LEAD) row in the window — perfect for diffs over time.",
                    "termCode": "lag-lead",
                    "url": "https://delaparra-services.nl/knowledge-hub/lag-lead",
                    "inLanguage": "en",
                    "keywords": "SQL",
                    "dateModified": "2026-11-25T17:46:27+00:00",
                    "inDefinedTermSet": {
                        "@id": "https://delaparra-services.nl/knowledge-hub#terms"
                    },
                    "isPartOf": {
                        "@id": "https://delaparra-services.nl/knowledge-hub#dataset"
                    },
                    "subjectOf": [
                        {
                            "@id": "https://delaparra-services.nl/knowledge-hub/lag-lead#webpage"
                        },
                        {
                            "@id": "https://delaparra-services.nl/knowledge-hub/lag-lead#dataset"
                        }
                    ]
                },
                "inLanguage": "en",
                "breadcrumb": {
                    "@id": "https://delaparra-services.nl/knowledge-hub/lag-lead#breadcrumb"
                },
                "mainEntity": [
                    {
                        "@type": "Question",
                        "name": "What is LAG / LEAD?",
                        "acceptedAnswer": {
                            "@type": "Answer",
                            "text": "Read a value from a previous (LAG) or following (LEAD) row in the window — perfect for diffs over time. LAG and LEAD are window functions that read a value from a previous or following row. revenue - LAG(revenue) OVER (ORDER BY day) calculates the change from the day before, for every day, in one query."
                        }
                    },
                    {
                        "@type": "Question",
                        "name": "How is LAG / LEAD used in practice?",
                        "acceptedAnswer": {
                            "@type": "Answer",
                            "text": "They make comparisons over time simple: growth per month, time between a customer's orders, or the next scheduled appointment. Without them, you would need self-joins or post-processing in application code."
                        }
                    },
                    {
                        "@type": "Question",
                        "name": "What should you keep in mind with LAG / LEAD?",
                        "acceptedAnswer": {
                            "@type": "Answer",
                            "text": "The OVER clause controls the order, and PARTITION BY restarts the comparison per group, such as per customer. The first row has no previous row, so LAG returns NULL; you can pass a default as the third argument. MySQL 8, PostgreSQL and SQLite all support these functions, so most modern stacks can use them."
                        }
                    },
                    {
                        "@type": "Question",
                        "name": "What does an example of LAG / LEAD look like?",
                        "acceptedAnswer": {
                            "@type": "Answer",
                            "text": "SELECT day, revenue, revenue - LAG(revenue) OVER (ORDER BY day) AS delta FROM daily_sales;"
                        }
                    }
                ],
                "author": {
                    "@id": "https://delaparra-services.nl/#person"
                },
                "mentions": [
                    {
                        "@id": "https://delaparra-services.nl/skills/sql-optimization#skill"
                    },
                    {
                        "@id": "https://delaparra-services.nl/skills/sql-code-review#skill"
                    },
                    {
                        "@id": "https://delaparra-services.nl/skills/laravel-database-optimization#skill"
                    },
                    {
                        "@id": "https://delaparra-services.nl/manuals/sql-queries-en-migraties#article"
                    },
                    {
                        "@id": "https://delaparra-services.nl/manuals/php-en-composer#article"
                    }
                ]
            },
            {
                "@type": "BreadcrumbList",
                "@id": "https://delaparra-services.nl/knowledge-hub/lag-lead#breadcrumb",
                "itemListElement": [
                    {
                        "@type": "ListItem",
                        "position": 1,
                        "name": "Home",
                        "item": "https://delaparra-services.nl/"
                    },
                    {
                        "@type": "ListItem",
                        "position": 2,
                        "name": "Knowledge hub",
                        "item": "https://delaparra-services.nl/knowledge-hub"
                    },
                    {
                        "@type": "ListItem",
                        "position": 3,
                        "name": "LAG / LEAD"
                    }
                ]
            },
            {
                "@type": "Person",
                "@id": "https://delaparra-services.nl/#person",
                "name": "Reinier de la Parra",
                "url": "https://delaparra-services.nl/about"
            },
            {
                "@type": "Dataset",
                "@id": "https://delaparra-services.nl/knowledge-hub/lag-lead#dataset",
                "name": "LAG / LEAD",
                "description": "Read a value from a previous (LAG) or following (LEAD) row in the window — perfect for diffs over time.",
                "url": "https://delaparra-services.nl/knowledge-hub/lag-lead",
                "identifier": "https://delaparra-services.nl/knowledge-hub/lag-lead#dataset",
                "about": {
                    "@id": "https://delaparra-services.nl/knowledge-hub/lag-lead#term"
                },
                "creator": {
                    "@id": "https://delaparra-services.nl/#practice"
                },
                "publisher": {
                    "@id": "https://delaparra-services.nl/#practice"
                },
                "license": "https://creativecommons.org/licenses/by/4.0/",
                "creditText": "Coders Lingo (https://coderslingo.com/resources/open-dataset/), CC BY 4.0",
                "isAccessibleForFree": true,
                "inLanguage": "en",
                "keywords": "SQL",
                "version": "2026-11-25",
                "dateModified": "2026-11-25T17:46:27+00:00",
                "isPartOf": {
                    "@type": "Dataset",
                    "@id": "https://delaparra-services.nl/knowledge-hub#dataset",
                    "name": "IT terms from A to Z",
                    "url": "https://delaparra-services.nl/knowledge-hub"
                },
                "includedInDataCatalog": {
                    "@type": "DataCatalog",
                    "@id": "https://delaparra-services.nl/knowledge-hub#catalog",
                    "name": "Knowledge hub data catalog",
                    "url": "https://delaparra-services.nl/knowledge-hub"
                },
                "distribution": [
                    {
                        "@type": "DataDownload",
                        "name": "lag-lead.csv",
                        "encodingFormat": "text/csv",
                        "contentUrl": "https://delaparra-services.nl/knowledge-hub/lag-lead.csv",
                        "inLanguage": "en",
                        "dateModified": "2026-11-25T17:46:27+00:00",
                        "license": "https://creativecommons.org/licenses/by/4.0/"
                    },
                    {
                        "@type": "DataDownload",
                        "name": "lag-lead.json",
                        "encodingFormat": "application/json",
                        "contentUrl": "https://delaparra-services.nl/knowledge-hub/lag-lead.json",
                        "inLanguage": "en",
                        "dateModified": "2026-11-25T17:46:27+00:00",
                        "license": "https://creativecommons.org/licenses/by/4.0/"
                    },
                    {
                        "@type": "DataDownload",
                        "name": "lag-lead.md",
                        "encodingFormat": "text/markdown",
                        "contentUrl": "https://delaparra-services.nl/knowledge-hub/lag-lead.md",
                        "inLanguage": "en",
                        "dateModified": "2026-11-25T17:46:27+00:00",
                        "license": "https://creativecommons.org/licenses/by/4.0/"
                    },
                    {
                        "@type": "DataDownload",
                        "name": "lag-lead.pdf",
                        "encodingFormat": "application/pdf",
                        "contentUrl": "https://delaparra-services.nl/knowledge-hub/lag-lead.pdf",
                        "inLanguage": "en",
                        "dateModified": "2026-11-25T17:46:27+00:00",
                        "license": "https://creativecommons.org/licenses/by/4.0/"
                    },
                    {
                        "@type": "DataDownload",
                        "name": "lag-lead.zip",
                        "encodingFormat": "application/zip",
                        "contentUrl": "https://delaparra-services.nl/knowledge-hub/lag-lead.zip",
                        "inLanguage": "en",
                        "dateModified": "2026-11-25T17:46:27+00:00",
                        "license": "https://creativecommons.org/licenses/by/4.0/"
                    },
                    {
                        "@type": "DataDownload",
                        "name": "lag-lead-skills.zip",
                        "encodingFormat": "application/zip",
                        "contentUrl": "https://delaparra-services.nl/knowledge-hub/lag-lead/skills.zip",
                        "inLanguage": "en",
                        "dateModified": "2026-11-25T17:46:27+00:00",
                        "license": "https://creativecommons.org/licenses/by/4.0/"
                    }
                ]
            }
        ]
    }
}