{"id":107,"date":"2026-03-06T08:31:58","date_gmt":"2026-03-06T08:31:58","guid":{"rendered":"https:\/\/gigz.pk\/sql\/?post_type=lesson&#038;p=107"},"modified":"2026-03-16T18:54:10","modified_gmt":"2026-03-16T18:54:10","slug":"row_number","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/sql\/lesson\/row_number\/","title":{"rendered":"\u00a0ROW_NUMBER"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>ROW_NUMBER()<\/code> function is a window function in SQL that assigns a unique sequential integer to rows within a result set. Each row in the partition gets a number starting from 1, based on the order you specify.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Basic Syntax<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">ROW_NUMBER() OVER (<br>    [PARTITION BY column_name]<br>    ORDER BY column_name [ASC|DESC]<br>)<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>PARTITION BY<\/strong>: Optional. Divides the result set into partitions and resets the row number for each partition.<\/li>\n\n\n\n<li><strong>ORDER BY<\/strong>: Required. Specifies the order in which the row numbers are assigned.<\/li>\n\n\n\n<li><strong>ASC \/ DESC<\/strong>: Determines ascending or descending order.<\/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\"><strong>Simple Example<\/strong><\/h3>\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>Name<\/th><th>Department<\/th><\/tr><\/thead><tbody><tr><td>101<\/td><td>Alice<\/td><td>Sales<\/td><\/tr><tr><td>102<\/td><td>Bob<\/td><td>Sales<\/td><\/tr><tr><td>103<\/td><td>Carol<\/td><td>HR<\/td><\/tr><tr><td>104<\/td><td>David<\/td><td>HR<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Assign row numbers to all employees:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT<br>    Name,<br>    Department,<br>    ROW_NUMBER() OVER (ORDER BY Name) 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>Name<\/th><th>Department<\/th><th>RowNum<\/th><\/tr><\/thead><tbody><tr><td>Alice<\/td><td>Sales<\/td><td>1<\/td><\/tr><tr><td>Bob<\/td><td>Sales<\/td><td>2<\/td><\/tr><tr><td>Carol<\/td><td>HR<\/td><td>3<\/td><\/tr><tr><td>David<\/td><td>HR<\/td><td>4<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Using PARTITION BY<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Assign row numbers <strong>per department<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT<br>    Name,<br>    Department,<br>    ROW_NUMBER() OVER (PARTITION BY Department ORDER BY Name) 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>Name<\/th><th>Department<\/th><th>RowNum<\/th><\/tr><\/thead><tbody><tr><td>Alice<\/td><td>Sales<\/td><td>1<\/td><\/tr><tr><td>Bob<\/td><td>Sales<\/td><td>2<\/td><\/tr><tr><td>Carol<\/td><td>HR<\/td><td>1<\/td><\/tr><tr><td>David<\/td><td>HR<\/td><td>2<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Common Use Cases<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Finding duplicates<\/strong> \u2013 Keep only the first occurrence.<\/li>\n\n\n\n<li><strong>Paging results<\/strong> \u2013 Retrieve rows in specific batches for web pages.<\/li>\n\n\n\n<li><strong>Ranking<\/strong> \u2013 Assign sequential ranks without gaps.<\/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\"><strong>Key Points<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ROW_NUMBER()<\/code> always starts at 1 for each partition.<\/li>\n\n\n\n<li>Requires <code>ORDER BY<\/code>; otherwise, the numbering will be unpredictable.<\/li>\n\n\n\n<li>Can be combined with <code>WHERE<\/code> or <code>CTE<\/code> to filter specific rows.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\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 > ROW_NUMBER<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773597199168\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":52,"template":"","class_list":["post-107","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>\u00a0ROW_NUMBER - SQL Learning Hub<\/title>\n<meta name=\"description\" content=\"Learn how to use SQL ROW_NUMBER() window function with examples. Understand PARTITION BY, ORDER BY, ranking, and pagination techniques.\" \/>\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\/row_number\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u00a0ROW_NUMBER - SQL Learning Hub\" \/>\n<meta property=\"og:description\" content=\"Learn how to use SQL ROW_NUMBER() window function with examples. Understand PARTITION BY, ORDER BY, ranking, and pagination techniques.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/sql\/lesson\/row_number\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Learning Hub\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-16T18:54:10+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=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/row_number\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/row_number\\\/\",\"name\":\"\u00a0ROW_NUMBER - SQL Learning Hub\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/#website\"},\"datePublished\":\"2026-03-06T08:31:58+00:00\",\"dateModified\":\"2026-03-16T18:54:10+00:00\",\"description\":\"Learn how to use SQL ROW_NUMBER() window function with examples. Understand PARTITION BY, ORDER BY, ranking, and pagination techniques.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/row_number\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/row_number\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/row_number\\\/#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 > ROW_NUMBER\"}]},{\"@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":"\u00a0ROW_NUMBER - SQL Learning Hub","description":"Learn how to use SQL ROW_NUMBER() window function with examples. Understand PARTITION BY, ORDER BY, ranking, and pagination techniques.","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\/row_number\/","og_locale":"en_US","og_type":"article","og_title":"\u00a0ROW_NUMBER - SQL Learning Hub","og_description":"Learn how to use SQL ROW_NUMBER() window function with examples. Understand PARTITION BY, ORDER BY, ranking, and pagination techniques.","og_url":"https:\/\/gigz.pk\/sql\/lesson\/row_number\/","og_site_name":"SQL Learning Hub","article_modified_time":"2026-03-16T18:54:10+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/sql\/lesson\/row_number\/","url":"https:\/\/gigz.pk\/sql\/lesson\/row_number\/","name":"\u00a0ROW_NUMBER - SQL Learning Hub","isPartOf":{"@id":"https:\/\/gigz.pk\/sql\/#website"},"datePublished":"2026-03-06T08:31:58+00:00","dateModified":"2026-03-16T18:54:10+00:00","description":"Learn how to use SQL ROW_NUMBER() window function with examples. Understand PARTITION BY, ORDER BY, ranking, and pagination techniques.","breadcrumb":{"@id":"https:\/\/gigz.pk\/sql\/lesson\/row_number\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/sql\/lesson\/row_number\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/sql\/lesson\/row_number\/#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 > ROW_NUMBER"}]},{"@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\/107","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=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}