{"id":108,"date":"2026-03-06T08:33:39","date_gmt":"2026-03-06T08:33:39","guid":{"rendered":"https:\/\/gigz.pk\/sql\/?post_type=lesson&#038;p=108"},"modified":"2026-03-16T18:54:25","modified_gmt":"2026-03-16T18:54:25","slug":"partition-by","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/sql\/lesson\/partition-by\/","title":{"rendered":"PARTITION BY"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>PARTITION BY<\/code> clause in SQL is used with <strong>window functions<\/strong> to divide the result set into partitions. Each partition is processed separately, allowing calculations to restart for each group of rows.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Unlike <code>GROUP BY<\/code>, which aggregates rows into a single result per group, <code>PARTITION BY<\/code> allows you to perform calculations across subsets of rows while keeping the individual rows visible.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">window_function() OVER (PARTITION BY column_name ORDER BY column_name)<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>window_function()<\/code>: Functions like <code>ROW_NUMBER()<\/code>, <code>RANK()<\/code>, <code>SUM()<\/code>, <code>AVG()<\/code>, etc.<\/li>\n\n\n\n<li><code>PARTITION BY column_name<\/code>: Divides the data into partitions based on the column.<\/li>\n\n\n\n<li><code>ORDER BY column_name<\/code>: (Optional) Defines the order within each partition.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Example 1: Row Number Per Partition<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose we have a table <code>Employees<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>EmployeeID<\/th><th>Department<\/th><th>Salary<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>Sales<\/td><td>5000<\/td><\/tr><tr><td>2<\/td><td>Sales<\/td><td>6000<\/td><\/tr><tr><td>3<\/td><td>HR<\/td><td>4500<\/td><\/tr><tr><td>4<\/td><td>HR<\/td><td>4700<\/td><\/tr><tr><td>5<\/td><td>IT<\/td><td>7000<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">We want to assign a row number for each employee within their department.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT <br>    EmployeeID,<br>    Department,<br>    Salary,<br>    ROW_NUMBER() OVER (PARTITION BY Department ORDER BY Salary DESC) AS RowNum<br>FROM Employees;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Result:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>EmployeeID<\/th><th>Department<\/th><th>Salary<\/th><th>RowNum<\/th><\/tr><\/thead><tbody><tr><td>2<\/td><td>Sales<\/td><td>6000<\/td><td>1<\/td><\/tr><tr><td>1<\/td><td>Sales<\/td><td>5000<\/td><td>2<\/td><\/tr><tr><td>4<\/td><td>HR<\/td><td>4700<\/td><td>1<\/td><\/tr><tr><td>3<\/td><td>HR<\/td><td>4500<\/td><td>2<\/td><\/tr><tr><td>5<\/td><td>IT<\/td><td>7000<\/td><td>1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Explanation: Each department starts counting rows independently because of <code>PARTITION BY Department<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Example 2: Sum Per Partition<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you want the total salary per department while keeping all individual rows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT <br>    EmployeeID,<br>    Department,<br>    Salary,<br>    SUM(Salary) OVER (PARTITION BY Department) AS TotalDeptSalary<br>FROM Employees;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Result:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>EmployeeID<\/th><th>Department<\/th><th>Salary<\/th><th>TotalDeptSalary<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>Sales<\/td><td>5000<\/td><td>11000<\/td><\/tr><tr><td>2<\/td><td>Sales<\/td><td>6000<\/td><td>11000<\/td><\/tr><tr><td>3<\/td><td>HR<\/td><td>4500<\/td><td>9200<\/td><\/tr><tr><td>4<\/td><td>HR<\/td><td>4700<\/td><td>9200<\/td><\/tr><tr><td>5<\/td><td>IT<\/td><td>7000<\/td><td>7000<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Explanation: <code>SUM()<\/code> calculates the total for each partition but does not remove individual rows.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Key Points<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>PARTITION BY<\/code> is used only with <strong>window functions<\/strong>.<\/li>\n\n\n\n<li>It does <strong>not reduce the number of rows<\/strong>; it just groups them logically.<\/li>\n\n\n\n<li>Use <code>ORDER BY<\/code> within <code>OVER()<\/code> to define row order in calculations.<\/li>\n\n\n\n<li>Common window functions include <code>ROW_NUMBER()<\/code>, <code>RANK()<\/code>, <code>DENSE_RANK()<\/code>, <code>SUM()<\/code>, <code>AVG()<\/code>, <code>COUNT()<\/code>, and <code>LEAD()\/LAG()<\/code>.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\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 > PARTITION BY<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773597505539\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":53,"template":"","class_list":["post-108","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>PARTITION BY - SQL Learning Hub<\/title>\n<meta name=\"description\" content=\"Learn SQL PARTITION BY with examples. Understand window functions, ROW_NUMBER, SUM OVER, and differences between PARTITION BY and GROUP BY\" \/>\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\/partition-by\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PARTITION BY - SQL Learning Hub\" \/>\n<meta property=\"og:description\" content=\"Learn SQL PARTITION BY with examples. Understand window functions, ROW_NUMBER, SUM OVER, and differences between PARTITION BY and GROUP BY\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/sql\/lesson\/partition-by\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Learning Hub\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-16T18:54:25+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\\\/partition-by\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/partition-by\\\/\",\"name\":\"PARTITION BY - SQL Learning Hub\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/#website\"},\"datePublished\":\"2026-03-06T08:33:39+00:00\",\"dateModified\":\"2026-03-16T18:54:25+00:00\",\"description\":\"Learn SQL PARTITION BY with examples. Understand window functions, ROW_NUMBER, SUM OVER, and differences between PARTITION BY and GROUP BY\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/partition-by\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/partition-by\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/partition-by\\\/#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 > PARTITION BY\"}]},{\"@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":"PARTITION BY - SQL Learning Hub","description":"Learn SQL PARTITION BY with examples. Understand window functions, ROW_NUMBER, SUM OVER, and differences between PARTITION BY and GROUP BY","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\/partition-by\/","og_locale":"en_US","og_type":"article","og_title":"PARTITION BY - SQL Learning Hub","og_description":"Learn SQL PARTITION BY with examples. Understand window functions, ROW_NUMBER, SUM OVER, and differences between PARTITION BY and GROUP BY","og_url":"https:\/\/gigz.pk\/sql\/lesson\/partition-by\/","og_site_name":"SQL Learning Hub","article_modified_time":"2026-03-16T18:54:25+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\/partition-by\/","url":"https:\/\/gigz.pk\/sql\/lesson\/partition-by\/","name":"PARTITION BY - SQL Learning Hub","isPartOf":{"@id":"https:\/\/gigz.pk\/sql\/#website"},"datePublished":"2026-03-06T08:33:39+00:00","dateModified":"2026-03-16T18:54:25+00:00","description":"Learn SQL PARTITION BY with examples. Understand window functions, ROW_NUMBER, SUM OVER, and differences between PARTITION BY and GROUP BY","breadcrumb":{"@id":"https:\/\/gigz.pk\/sql\/lesson\/partition-by\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/sql\/lesson\/partition-by\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/sql\/lesson\/partition-by\/#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 > PARTITION BY"}]},{"@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\/108","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=108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}