{"id":53433,"date":"2019-10-15T11:19:57","date_gmt":"2019-10-15T11:19:57","guid":{"rendered":"https:\/\/www.sqlshack.com\/?p=53433"},"modified":"2019-12-09T10:11:06","modified_gmt":"2019-12-09T10:11:06","slug":"sql-lag-function-overview-and-examples","status":"publish","type":"post","link":"https:\/\/www.sqlshack.com\/sql-lag-function-overview-and-examples\/","title":{"rendered":"SQL Lag function overview and examples"},"content":{"rendered":"<p>\n  In the article <a href=\"https:\/\/www.sqlshack.com\/overview-and-examples-of-sql-server-lead-function\/\">SQL Server Lead function overview and examples<\/a>, we explored Lead function for performing computational operations on data. This article gives an overview of the SQL Lag function and its comparison with the SQL Lead function.\n<\/p>\n<p><!--more--><\/p>\n<h2>Overview of SQL Lag function<\/h2>\n<p>\n  We use a Lag() function to access previous rows data as per defined offset value. It is a window function available from SQL Server 2012 onwards. It works similar to a Lead function. In the lead function, we access subsequent rows, but in lag function, we access previous rows. It is a useful function in comparing the current row value from the previous row value.\n<\/p>\n<h3>Syntax of Lag function<\/h3>\n<pre lang=\"tsql\">LAG (scalar_expression [,offset] [,default])  \r\nOVER ( [ partition_by_clause ] order_by_clause )<\/pre>\n<p>\n  It uses following arguments.\n<\/p>\n<ul>\n<li>\n    Scalar_expression: We define a column name or expression in this argument. The lag function does calculations on this column. It is a mandatory argument, and we cannot execute the lag function without this\n  <\/li>\n<li>\n    Offset: We define an integer number in this argument. The lag function uses this argument forgo behind the number of rows (offset). The default value for this argument is one. It is an optional argument\n  <\/li>\n<li>\n    Default: Suppose we define an offset, value that does not lie in the boundary of the data. For example, we specified offset value 3 for the first row. A lag function cannot go three rows behind. It displays the default value if specified. If we do not specify any value for this, the lag function displays NULL in the output for out of range values\n  <\/li>\n<li>\n    PARTITION BY: It creates a logical boundary of data. Suppose we have an extensive data set and we require calculations on a smaller set of data, we can define partitions for it. For example, sales data for an organization might contain data for several years. We can create a partition quarterly and do the computation. It is as well an optional argument\n  <\/li>\n<li>\n    ORDER BY: We can sort data in ascending or descending order using ORDER by clause. By default, it uses ascending order to sort data\n  <\/li>\n<\/ul>\n<p>\n  We will use data from the previous article for demonstration of SQL Server Lag function as well:\n<\/p>\n<pre lang=\"tsql\">DECLARE   @Employee TABLE\r\n  (\r\n       EmpCode VARCHAR(10),\r\n       EmpName   VARCHAR(10),\r\n       JoiningDate  DATE\r\n    )\r\nINSERT INTO @Employee VALUES ('1', 'Rajendra', '1-Sep-2018')\r\nINSERT INTO @Employee VALUES ('2', 'Manoj', '1-Oct-2018')\r\nINSERT INTO @Employee VALUES ('3', 'Sonu', '10-Mar-2018')\r\nINSERT INTO @Employee VALUES ('4', 'Kashish', '25-Oct-2018')\r\nINSERT INTO @Employee VALUES ('5', 'Tim', '1-Dec-2018')\r\nINSERT INTO @Employee VALUES ('6', 'Akshita', '1-Nov-2018')\r\nGO\r\nSELECT * FROM   @Employee;<\/pre>\n<p>\n  We have the following data in the Employee table:\n<\/p>\n<p>\n  <img decoding=\"async\" style=\"margin: 0px auto; display: block;\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" data-src=\"\/wp-content\/uploads\/2019\/10\/sample-data.png\" alt=\"Sample data\" \/><noscript><img decoding=\"async\" style=\"margin: 0px auto; display: block;\" src=\"\/wp-content\/uploads\/2019\/10\/sample-data.png\" alt=\"Sample data\" \/><\/noscript>\n<\/p>\n<h3>Example 1: SQL Lag function without a default value<\/h3>\n<p>\n  Execute the following query to use the Lag function on the JoiningDate column with offset one. We did not specify any default value in this query.\n<\/p>\n<p>\n  Execute the following query (we require to run the complete query along with defining a variable, its value):\n<\/p>\n<pre lang=\"tsql\">SELECT *, \r\n       Lag(JoiningDate, 1) OVER(.\r\n       ORDER BY JoiningDate ASC) AS EndDate\r\nFROM @Employee;<\/pre>\n<p>\n  In the output, we can note the following:\n<\/p>\n<ul>\n<li>\n    The first row shows NULL value for the EndDate column because it does not have any previous rows\n  <\/li>\n<li>\n    The second row contains previous row value in the EndDate column. It takes value from the previous row due to offset value 1\n  <\/li>\n<\/ul>\n<p>\n  <img decoding=\"async\" style=\"margin: 0px auto; display: block;\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" data-src=\"\/wp-content\/uploads\/2019\/10\/image2.png\" alt=\"Lag function without a default value\" \/><noscript><img decoding=\"async\" style=\"margin: 0px auto; display: block;\" src=\"\/wp-content\/uploads\/2019\/10\/image2.png\" alt=\"Lag function without a default value\" \/><\/noscript>\n<\/p>\n<h3>Example 2: SQL Lag function with a default value<\/h3>\n<p>\n  In the previous example, we get NULL value as a default value. Let&#8217;s use a default end date in the lag function. This example also uses offset value 1 in the lag function:\n<\/p>\n<pre lang=\"tsql\">SELECT *, \r\n       Lag(JoiningDate, 1,'1999-09-01') OVER(\r\n       ORDER BY JoiningDate ASC) AS EndDate\r\nFROM @Employee;<\/pre>\n<p>\n  In the output, we can see a default value instead of NULL in the first row:\n<\/p>\n<p>\n  <img decoding=\"async\" style=\"margin: 0px auto; display: block;\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" data-src=\"\/wp-content\/uploads\/2019\/10\/image3.png\" alt=\"Lag function with a default value\" \/><noscript><img decoding=\"async\" style=\"margin: 0px auto; display: block;\" src=\"\/wp-content\/uploads\/2019\/10\/image3.png\" alt=\"Lag function with a default value\" \/><\/noscript>\n<\/p>\n<p>\n  We can use compatible data types in the default value column. If we use incompatible data types, we get the following error message:\n<\/p>\n<p>\n  <img decoding=\"async\" style=\"margin: 0px auto; display: block;\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" data-src=\"\/wp-content\/uploads\/2019\/10\/image4.png\" alt=\"Error message\" \/><noscript><img decoding=\"async\" style=\"margin: 0px auto; display: block;\" src=\"\/wp-content\/uploads\/2019\/10\/image4.png\" alt=\"Error message\" \/><\/noscript>\n<\/p>\n<h3>Example 3: SQL Lag function with OFFSET value 2<\/h3>\n<p>\n  Previously, we used default offset 1 in Lag function, and it takes value from the previous row. In the example, we use offset value 2. In the output, you can see we have a default value for row 1 and 2. In row 3, it takes value from row 1:\n<\/p>\n<pre lang=\"tsql\">SELECT *, \r\n       Lag(JoiningDate, 2,'1999-09-01') OVER(\r\n       ORDER BY JoiningDate ASC) AS EndDate\r\nFROM @Employee;<\/pre>\n<p>\n  <img decoding=\"async\" style=\"margin: 0px auto; display: block;\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" data-src=\"\/wp-content\/uploads\/2019\/10\/image5.png\" alt=\"Lag function with OFFSET value 2\" \/><noscript><img decoding=\"async\" style=\"margin: 0px auto; display: block;\" src=\"\/wp-content\/uploads\/2019\/10\/image5.png\" alt=\"Lag function with OFFSET value 2\" \/><\/noscript>\n<\/p>\n<h3>Example 4: SQL Lag function with PARTITION BY clause<\/h3>\n<p>\n  As discussed earlier, we use the PARTITION BY clause to create a logical subset of data. Let&#8217;s use this PARTITION function on the ProductSales table. You can refer to the SQL Server Lead function to create this table:\n<\/p>\n<p>\n  <img decoding=\"async\" style=\"margin: 0px auto; display: block;\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" data-src=\"\/wp-content\/uploads\/2019\/10\/image6.png\" alt=\"SQL Server Lag function with PARTITION BY clause\" \/><noscript><img decoding=\"async\" style=\"margin: 0px auto; display: block;\" src=\"\/wp-content\/uploads\/2019\/10\/image6.png\" alt=\"SQL Server Lag function with PARTITION BY clause\" \/><\/noscript>\n<\/p>\n<p>\n  In the following query, we use SQL Server Lag function and view the output:\n<\/p>\n<pre lang=\"tsql\">SELECT [Year], \r\n       [Quarter], \r\n       Sales, \r\n       LAG(Sales, 1, 0) OVER(\r\n       ORDER BY [Year], \r\n                [Quarter] ASC) AS [NextQuarterSales]\r\nFROM dbo.ProductSales;<\/pre>\n<p>\n  In the output, the lag function considers all rows as a single data set and applies Lag function:\n<\/p>\n<p>\n  <img decoding=\"async\" style=\"margin: 0px auto; display: block;\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" data-src=\"\/wp-content\/uploads\/2019\/10\/image7.png\" alt=\"SQL Server Lag function with PARTITION BY clause\" \/><noscript><img decoding=\"async\" style=\"margin: 0px auto; display: block;\" src=\"\/wp-content\/uploads\/2019\/10\/image7.png\" alt=\"SQL Server Lag function with PARTITION BY clause\" \/><\/noscript>\n<\/p>\n<p>\n  In the ProductSales table, we have data for the years of 2017, 2018 and 2019. We want to use a lag function on a yearly basis. We use the PARTITION BY clause on the Year column and define the logical subset of data on a yearly basis. We use the Order by clause on year and quarter columns to sort data first on a yearly basis and then monthly:\n<\/p>\n<pre lang=\"tsql\">SELECT [Year], \r\n       [Quarter], \r\n       Sales, \r\n       LAG(Sales, 1, 0) OVER(PARTITION BY [Year]\r\n       ORDER BY [Year], \r\n                [Quarter] ASC) AS [NextQuarterSales]\r\nFROM dbo.ProductSales;<\/pre>\n<p>\n  In the following screenshot, we can see three partitions of data for 2017,2018 and 2019 year. The Lag function individually works on each partition and calculates the required data:\n<\/p>\n<p>\n  <img decoding=\"async\" style=\"margin: 0px auto; display: block;\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" data-src=\"\/wp-content\/uploads\/2019\/10\/image8.png\" alt=\"Lag function with partition\" \/><noscript><img decoding=\"async\" style=\"margin: 0px auto; display: block;\" src=\"\/wp-content\/uploads\/2019\/10\/image8.png\" alt=\"Lag function with partition\" \/><\/noscript>\n<\/p>\n<h2>Conclusion<\/h2>\n<p>\n  In this article, we learned the SQL Lag function and its usage to retrieve a value from previous rows. Here is the quick summary of the lag function:\n<\/p>\n<ul>\n<li>\n    Lag function fetches the value from the previous rows based on the offset defined\n  <\/li>\n<li>\n    Offset one is the default offset value, and in this Lag, the function retrieves a value from the previous row\n  <\/li>\n<li>\n    PARTITION BY clause defines a logical boundary of data based on the specified condition\n  <\/li>\n<li>\n    The lag function uses default value NULL for out-of-range data\n  <\/li>\n<li>\n    We can use the Lag function with common table expression, stored procedures, and functions for computation purposes\n  <\/li>\n<\/ul>\n<div id=\"see_more\"><\/div>\n<p><script>\ndisplay_see_more(text='complete', video='complete', banner='complete', banner_link='complete');\n<\/script><\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>In the article SQL Server Lead function overview and examples, we explored Lead function for performing computational operations on data. This article gives an overview of the SQL Lag function and its comparison with the SQL Lead function.<!-- AddThis Advanced Settings generic via filter on wp_trim_excerpt --><!-- AddThis Share Buttons generic via filter on wp_trim_excerpt --><\/p>\n","protected":false},"author":48,"featured_media":53440,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[434,25,194],"tags":[],"class_list":["post-53433","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","category-sql-commands","category-t-sql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL Lag function overview and examples<\/title>\n<meta name=\"description\" content=\"In this blog post, we are going to give an overview of the SQL Lag function and its comparison with the SQL Lead function.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlshack.com\/sql-lag-function-overview-and-examples\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Lag function overview and examples\" \/>\n<meta property=\"og:description\" content=\"In this blog post, we are going to give an overview of the SQL Lag function and its comparison with the SQL Lead function.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlshack.com\/sql-lag-function-overview-and-examples\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Shack - articles about database auditing, server performance, data recovery, and more\" \/>\n<meta property=\"article:published_time\" content=\"2019-10-15T11:19:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-12-09T10:11:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.sqlshack.com\/wp-content\/uploads\/2019\/10\/image8.png\" \/>\n\t<meta property=\"og:image:width\" content=\"386\" \/>\n\t<meta property=\"og:image:height\" content=\"266\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Rajendra Gupta\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rajendra Gupta\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.sqlshack.com\\\/sql-lag-function-overview-and-examples\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.sqlshack.com\\\/sql-lag-function-overview-and-examples\\\/\"},\"author\":{\"name\":\"Rajendra Gupta\",\"@id\":\"https:\\\/\\\/www.sqlshack.com\\\/#\\\/schema\\\/person\\\/e89dad17f72ffe88a2f28ef1bc03faa9\"},\"headline\":\"SQL Lag function overview and examples\",\"datePublished\":\"2019-10-15T11:19:57+00:00\",\"dateModified\":\"2019-12-09T10:11:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.sqlshack.com\\\/sql-lag-function-overview-and-examples\\\/\"},\"wordCount\":873,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.sqlshack.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.sqlshack.com\\\/sql-lag-function-overview-and-examples\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.sqlshack.com\\\/wp-content\\\/uploads\\\/2019\\\/10\\\/image8.png\",\"articleSection\":[\"Development\",\"SQL commands\",\"T-SQL\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.sqlshack.com\\\/sql-lag-function-overview-and-examples\\\/\",\"url\":\"https:\\\/\\\/www.sqlshack.com\\\/sql-lag-function-overview-and-examples\\\/\",\"name\":\"SQL Lag function overview and examples\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.sqlshack.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.sqlshack.com\\\/sql-lag-function-overview-and-examples\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.sqlshack.com\\\/sql-lag-function-overview-and-examples\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.sqlshack.com\\\/wp-content\\\/uploads\\\/2019\\\/10\\\/image8.png\",\"datePublished\":\"2019-10-15T11:19:57+00:00\",\"dateModified\":\"2019-12-09T10:11:06+00:00\",\"description\":\"In this blog post, we are going to give an overview of the SQL Lag function and its comparison with the SQL Lead function.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.sqlshack.com\\\/sql-lag-function-overview-and-examples\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.sqlshack.com\\\/sql-lag-function-overview-and-examples\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.sqlshack.com\\\/wp-content\\\/uploads\\\/2019\\\/10\\\/image8.png\",\"contentUrl\":\"https:\\\/\\\/www.sqlshack.com\\\/wp-content\\\/uploads\\\/2019\\\/10\\\/image8.png\",\"width\":386,\"height\":266},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.sqlshack.com\\\/#website\",\"url\":\"https:\\\/\\\/www.sqlshack.com\\\/\",\"name\":\"SQL Shack - articles about database auditing, server performance, data recovery, and more\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.sqlshack.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.sqlshack.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.sqlshack.com\\\/#organization\",\"name\":\"SQL Shack\",\"url\":\"https:\\\/\\\/www.sqlshack.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.sqlshack.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.sqlshack.com\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/sqlshack-default.png\",\"contentUrl\":\"https:\\\/\\\/www.sqlshack.com\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/sqlshack-default.png\",\"width\":1200,\"height\":630,\"caption\":\"SQL Shack\"},\"image\":{\"@id\":\"https:\\\/\\\/www.sqlshack.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.sqlshack.com\\\/#\\\/schema\\\/person\\\/e89dad17f72ffe88a2f28ef1bc03faa9\",\"name\":\"Rajendra Gupta\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/bfc3d06e67ce655a515c889e0cb14acb5cb807327b25f5dbdefcf02924a22bec?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/bfc3d06e67ce655a515c889e0cb14acb5cb807327b25f5dbdefcf02924a22bec?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/bfc3d06e67ce655a515c889e0cb14acb5cb807327b25f5dbdefcf02924a22bec?s=96&d=mm&r=g\",\"caption\":\"Rajendra Gupta\"},\"description\":\"Hi! I am Rajendra Gupta, Database Specialist and Architect, helping organizations implement Microsoft SQL Server, Azure, Couchbase, AWS solutions fast and efficiently, fix related issues, and Performance Tuning with over 14 years of experience. I am the author of the book \\\"DP-300 Administering Relational Database on Microsoft Azure\\\". I published more than 650 technical articles on MSSQLTips, SQLShack, Quest, CodingSight, and SeveralNines. I am the creator of one of the biggest free online collections of articles on a single topic, with his 50-part series on SQL Server Always On Availability Groups. Based on my contribution to the SQL Server community, I have been recognized as the prestigious Best Author of the Year continuously in 2019, 2020, and 2021 (2nd Rank) at SQLShack and the MSSQLTIPS champions award in 2020. Personal Blog: https:\\\/\\\/www.dbblogger.com I am always interested in new challenges so if you need consulting help, reach me at rajendra.gupta16@gmail.com View all posts by Rajendra Gupta\",\"url\":\"https:\\\/\\\/www.sqlshack.com\\\/author\\\/rajendra-gupta\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Lag function overview and examples","description":"In this blog post, we are going to give an overview of the SQL Lag function and its comparison with the SQL Lead function.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlshack.com\/sql-lag-function-overview-and-examples\/","og_locale":"en_US","og_type":"article","og_title":"SQL Lag function overview and examples","og_description":"In this blog post, we are going to give an overview of the SQL Lag function and its comparison with the SQL Lead function.","og_url":"https:\/\/www.sqlshack.com\/sql-lag-function-overview-and-examples\/","og_site_name":"SQL Shack - articles about database auditing, server performance, data recovery, and more","article_published_time":"2019-10-15T11:19:57+00:00","article_modified_time":"2019-12-09T10:11:06+00:00","og_image":[{"width":386,"height":266,"url":"https:\/\/www.sqlshack.com\/wp-content\/uploads\/2019\/10\/image8.png","type":"image\/png"}],"author":"Rajendra Gupta","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Rajendra Gupta","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.sqlshack.com\/sql-lag-function-overview-and-examples\/#article","isPartOf":{"@id":"https:\/\/www.sqlshack.com\/sql-lag-function-overview-and-examples\/"},"author":{"name":"Rajendra Gupta","@id":"https:\/\/www.sqlshack.com\/#\/schema\/person\/e89dad17f72ffe88a2f28ef1bc03faa9"},"headline":"SQL Lag function overview and examples","datePublished":"2019-10-15T11:19:57+00:00","dateModified":"2019-12-09T10:11:06+00:00","mainEntityOfPage":{"@id":"https:\/\/www.sqlshack.com\/sql-lag-function-overview-and-examples\/"},"wordCount":873,"commentCount":0,"publisher":{"@id":"https:\/\/www.sqlshack.com\/#organization"},"image":{"@id":"https:\/\/www.sqlshack.com\/sql-lag-function-overview-and-examples\/#primaryimage"},"thumbnailUrl":"https:\/\/www.sqlshack.com\/wp-content\/uploads\/2019\/10\/image8.png","articleSection":["Development","SQL commands","T-SQL"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.sqlshack.com\/sql-lag-function-overview-and-examples\/","url":"https:\/\/www.sqlshack.com\/sql-lag-function-overview-and-examples\/","name":"SQL Lag function overview and examples","isPartOf":{"@id":"https:\/\/www.sqlshack.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.sqlshack.com\/sql-lag-function-overview-and-examples\/#primaryimage"},"image":{"@id":"https:\/\/www.sqlshack.com\/sql-lag-function-overview-and-examples\/#primaryimage"},"thumbnailUrl":"https:\/\/www.sqlshack.com\/wp-content\/uploads\/2019\/10\/image8.png","datePublished":"2019-10-15T11:19:57+00:00","dateModified":"2019-12-09T10:11:06+00:00","description":"In this blog post, we are going to give an overview of the SQL Lag function and its comparison with the SQL Lead function.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlshack.com\/sql-lag-function-overview-and-examples\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlshack.com\/sql-lag-function-overview-and-examples\/#primaryimage","url":"https:\/\/www.sqlshack.com\/wp-content\/uploads\/2019\/10\/image8.png","contentUrl":"https:\/\/www.sqlshack.com\/wp-content\/uploads\/2019\/10\/image8.png","width":386,"height":266},{"@type":"WebSite","@id":"https:\/\/www.sqlshack.com\/#website","url":"https:\/\/www.sqlshack.com\/","name":"SQL Shack - articles about database auditing, server performance, data recovery, and more","description":"","publisher":{"@id":"https:\/\/www.sqlshack.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlshack.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.sqlshack.com\/#organization","name":"SQL Shack","url":"https:\/\/www.sqlshack.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlshack.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.sqlshack.com\/wp-content\/uploads\/2019\/03\/sqlshack-default.png","contentUrl":"https:\/\/www.sqlshack.com\/wp-content\/uploads\/2019\/03\/sqlshack-default.png","width":1200,"height":630,"caption":"SQL Shack"},"image":{"@id":"https:\/\/www.sqlshack.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.sqlshack.com\/#\/schema\/person\/e89dad17f72ffe88a2f28ef1bc03faa9","name":"Rajendra Gupta","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/bfc3d06e67ce655a515c889e0cb14acb5cb807327b25f5dbdefcf02924a22bec?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/bfc3d06e67ce655a515c889e0cb14acb5cb807327b25f5dbdefcf02924a22bec?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/bfc3d06e67ce655a515c889e0cb14acb5cb807327b25f5dbdefcf02924a22bec?s=96&d=mm&r=g","caption":"Rajendra Gupta"},"description":"Hi! I am Rajendra Gupta, Database Specialist and Architect, helping organizations implement Microsoft SQL Server, Azure, Couchbase, AWS solutions fast and efficiently, fix related issues, and Performance Tuning with over 14 years of experience. I am the author of the book \"DP-300 Administering Relational Database on Microsoft Azure\". I published more than 650 technical articles on MSSQLTips, SQLShack, Quest, CodingSight, and SeveralNines. I am the creator of one of the biggest free online collections of articles on a single topic, with his 50-part series on SQL Server Always On Availability Groups. Based on my contribution to the SQL Server community, I have been recognized as the prestigious Best Author of the Year continuously in 2019, 2020, and 2021 (2nd Rank) at SQLShack and the MSSQLTIPS champions award in 2020. Personal Blog: https:\/\/www.dbblogger.com I am always interested in new challenges so if you need consulting help, reach me at rajendra.gupta16@gmail.com View all posts by Rajendra Gupta","url":"https:\/\/www.sqlshack.com\/author\/rajendra-gupta\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlshack.com\/wp-json\/wp\/v2\/posts\/53433","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlshack.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlshack.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlshack.com\/wp-json\/wp\/v2\/users\/48"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlshack.com\/wp-json\/wp\/v2\/comments?post=53433"}],"version-history":[{"count":4,"href":"https:\/\/www.sqlshack.com\/wp-json\/wp\/v2\/posts\/53433\/revisions"}],"predecessor-version":[{"id":53453,"href":"https:\/\/www.sqlshack.com\/wp-json\/wp\/v2\/posts\/53433\/revisions\/53453"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sqlshack.com\/wp-json\/wp\/v2\/media\/53440"}],"wp:attachment":[{"href":"https:\/\/www.sqlshack.com\/wp-json\/wp\/v2\/media?parent=53433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlshack.com\/wp-json\/wp\/v2\/categories?post=53433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlshack.com\/wp-json\/wp\/v2\/tags?post=53433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}