{"id":105,"date":"2026-03-06T08:27:44","date_gmt":"2026-03-06T08:27:44","guid":{"rendered":"https:\/\/gigz.pk\/sql\/?post_type=lesson&#038;p=105"},"modified":"2026-03-16T18:53:56","modified_gmt":"2026-03-16T18:53:56","slug":"window-functions","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/sql\/lesson\/window-functions\/","title":{"rendered":"Window Functions"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Window functions are a type of SQL function that perform calculations across a set of table rows related to the current row. Unlike aggregate functions, window functions do not collapse rows; they preserve the original row structure while providing additional insights.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Key Concepts<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Window<\/strong>\n<ul class=\"wp-block-list\">\n<li>A window defines the set of rows over which the window function operates.<\/li>\n\n\n\n<li>The window can be defined using <code>PARTITION BY<\/code> and <code>ORDER BY<\/code> clauses.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Partitioning<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>PARTITION BY<\/code> divides the dataset into subsets (partitions) and the window function is applied within each subset.<\/li>\n\n\n\n<li>Example: <code>PARTITION BY department<\/code> calculates results per department.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Ordering<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>ORDER BY<\/code> defines the sequence of rows within each partition for calculation.<\/li>\n\n\n\n<li>Useful for functions like ranking or running totals.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Common Window Functions<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>ROW_NUMBER()<\/strong>\n<ul class=\"wp-block-list\">\n<li>Assigns a unique sequential number to rows within a partition.<\/li>\n\n\n\n<li>Example: Identify top-performing employees per department.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>RANK()<\/strong>\n<ul class=\"wp-block-list\">\n<li>Assigns a rank to each row within a partition, with gaps for ties.<\/li>\n\n\n\n<li>Useful for competitive ranking scenarios.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>DENSE_RANK()<\/strong>\n<ul class=\"wp-block-list\">\n<li>Similar to <code>RANK()<\/code> but without gaps for ties.<\/li>\n\n\n\n<li>Example: Assign continuous ranks to sales figures.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>SUM() OVER()<\/strong>\n<ul class=\"wp-block-list\">\n<li>Calculates cumulative sums without collapsing rows.<\/li>\n\n\n\n<li>Example: Running total of sales.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>AVG() OVER()<\/strong>\n<ul class=\"wp-block-list\">\n<li>Calculates the average of a column over a window.<\/li>\n\n\n\n<li>Example: Average salary per department.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>LEAD() and LAG()<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>LEAD()<\/code> accesses data from the next row in the window.<\/li>\n\n\n\n<li><code>LAG()<\/code> accesses data from the previous row.<\/li>\n\n\n\n<li>Useful for comparisons and trend analysis.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax Example<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT<br>    employee_id,<br>    department,<br>    salary,<br>    ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS row_num,<br>    SUM(salary) OVER (PARTITION BY department ORDER BY salary) AS cumulative_salary,<br>    LAG(salary, 1) OVER (PARTITION BY department ORDER BY salary) AS prev_salary<br>FROM<br>    employees;<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practices<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Always define a clear window using <code>PARTITION BY<\/code> and <code>ORDER BY<\/code> to avoid unexpected results.<\/li>\n\n\n\n<li>Use window functions when you need row-level insights along with aggregate calculations.<\/li>\n\n\n\n<li>Avoid overusing window functions on very large datasets as they can impact performance.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Use Cases<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Ranking top sales performers by region.<\/li>\n\n\n\n<li>Calculating running totals or cumulative averages.<\/li>\n\n\n\n<li>Comparing current row values with previous or next rows.<\/li>\n\n\n\n<li>Detecting trends and patterns within partitions of data.<\/li>\n<\/ul>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/sql\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Learn Advanced SQL &#038; Database Engineering (SQL-301) > Advanced Querying > Window Functions<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773595954061\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":50,"template":"","class_list":["post-105","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Window Functions - SQL Learning Hub<\/title>\n<meta name=\"description\" content=\"Learn SQL window functions including ROW_NUMBER, RANK, LEAD, LAG, PARTITION BY and running totals with practical examples for data analysis\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/sql\/lesson\/window-functions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Window Functions - SQL Learning Hub\" \/>\n<meta property=\"og:description\" content=\"Learn SQL window functions including ROW_NUMBER, RANK, LEAD, LAG, PARTITION BY and running totals with practical examples for data analysis\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/sql\/lesson\/window-functions\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Learning Hub\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-16T18:53:56+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/window-functions\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/window-functions\\\/\",\"name\":\"Window Functions - SQL Learning Hub\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/#website\"},\"datePublished\":\"2026-03-06T08:27:44+00:00\",\"dateModified\":\"2026-03-16T18:53:56+00:00\",\"description\":\"Learn SQL window functions including ROW_NUMBER, RANK, LEAD, LAG, PARTITION BY and running totals with practical examples for data analysis\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/window-functions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/window-functions\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/window-functions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Learn Advanced SQL & Database Engineering (SQL-301) > Advanced Querying > Window Functions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/\",\"name\":\"SQL Learning Hub\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Window Functions - SQL Learning Hub","description":"Learn SQL window functions including ROW_NUMBER, RANK, LEAD, LAG, PARTITION BY and running totals with practical examples for data analysis","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:\/\/gigz.pk\/sql\/lesson\/window-functions\/","og_locale":"en_US","og_type":"article","og_title":"Window Functions - SQL Learning Hub","og_description":"Learn SQL window functions including ROW_NUMBER, RANK, LEAD, LAG, PARTITION BY and running totals with practical examples for data analysis","og_url":"https:\/\/gigz.pk\/sql\/lesson\/window-functions\/","og_site_name":"SQL Learning Hub","article_modified_time":"2026-03-16T18:53:56+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/sql\/lesson\/window-functions\/","url":"https:\/\/gigz.pk\/sql\/lesson\/window-functions\/","name":"Window Functions - SQL Learning Hub","isPartOf":{"@id":"https:\/\/gigz.pk\/sql\/#website"},"datePublished":"2026-03-06T08:27:44+00:00","dateModified":"2026-03-16T18:53:56+00:00","description":"Learn SQL window functions including ROW_NUMBER, RANK, LEAD, LAG, PARTITION BY and running totals with practical examples for data analysis","breadcrumb":{"@id":"https:\/\/gigz.pk\/sql\/lesson\/window-functions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/sql\/lesson\/window-functions\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/sql\/lesson\/window-functions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/sql\/"},{"@type":"ListItem","position":2,"name":"Learn Advanced SQL & Database Engineering (SQL-301) > Advanced Querying > Window Functions"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/sql\/#website","url":"https:\/\/gigz.pk\/sql\/","name":"SQL Learning Hub","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/sql\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/lesson\/105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/media?parent=105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}