{"id":10354,"date":"2026-01-22T04:49:47","date_gmt":"2026-01-22T04:49:47","guid":{"rendered":"https:\/\/ubiq.co\/tech-blog\/?p=10354"},"modified":"2026-01-26T06:13:31","modified_gmt":"2026-01-26T06:13:31","slug":"mysql-select-top-n-rows-per-group","status":"publish","type":"post","link":"https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/","title":{"rendered":"MySQL Select Top N Rows Per Group"},"content":{"rendered":"\n<p>MySQL allows you to easily organize data using GROUP BY clause. It is commonly used to summarize data by product, category, geography, etc. and other group variables. But sometimes, database developers may need to pick top N rows in each group. There is no built-in function or clause to support this requirement. So we will need to construct one. In this article, we will learn how to select top N rows per group in MySQL.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/#Why_Select_Top_Rows_Per_Group\" >Why Select Top Rows Per Group<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/#How_to_Select_Top_N_Rows_Per_Group\" >How to Select Top N Rows Per Group<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/#1_Using_row_number\" >1. Using row_number()<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/#What_is_a_window_function\" >What is a window function<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/#2_Using_Sub_Query\" >2. Using Sub Query<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/#Final_Verdict\" >Final Verdict<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/#FAQs\" >FAQs<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Why_Select_Top_Rows_Per_Group\"><\/span>Why Select Top Rows Per Group<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this case, the requirement is to select top N rows for each distinct value of another column (GROUP BY), based on one or more criteria. For example, you may need to select top 3 sales reps for each region (GROUP) in sales data table. You may need to select top 3 products for each product category in product sales data.<\/p>\n\n\n\n<p>As you can see, it is very important to be able to pick top N rows in each group from a table, based on a criteria. It is applicable in different business areas such as sales, orders, marketing, etc. Since there is no direct function or clause to support this, you need to construct an SQL query for this purpose.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Select_Top_N_Rows_Per_Group\"><\/span>How to Select Top N Rows Per Group<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are two ways to select top N rows per group in MySQL &#8211; using windows function and using sub query.<\/p>\n\n\n\n<p>Let us say you have the following sales data table which lists quantity ordered per brand for 3 categories &#8211; laptop, mobile and car.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">CREATE TABLE sales (<br>  id INTEGER PRIMARY KEY auto_increment,<br>  category varchar(255),<br>  brand varchar(255),<br>  quantity int<br>);<br><br>insert into sales(category,brand, quantity)<br>values('Laptop','Apple',100),<br>('Laptop','Samsung',200),<br>('Laptop','IBM',150),<br>('Mobile','Apple',900),<br>('Mobile','Samsung',500),<br>('Mobile','Sony',100),<br>('Car','Toyota',100),<br>('Car','Honda',250),<br>('Car','Hyundai',120);<br><br>select * from sales;<br><br>+----+----------+---------+----------+<br>| id | category | brand   | quantity |<br>+----+----------+---------+----------+<br>|  1 | Laptop   | Apple   |      100 |<br>|  2 | Laptop   | Samsung |      200 |<br>|  3 | Laptop   | IBM     |      150 |<br>|  4 | Mobile   | Apple   |      900 |<br>|  5 | Mobile   | Samsung |      500 |<br>|  6 | Mobile   | Sony    |      100 |<br>|  7 | Car      | Toyota  |      100 |<br>|  8 | Car      | Honda   |      250 |<br>|  9 | Car      | Hyundai |      120 |<br>+----+----------+---------+----------+<\/pre>\n\n\n\n<p>Let us say you want to get top 2 brands for each product category based on quantity column. Here are the two ways to do this.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Using_row_number\"><\/span>1. Using row_number()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Row_number() is a window function. Please note, this window function is available only in MySQL 8+. For older MySQL versions, use the next solution.<\/p>\n\n\n\n<p>Before we proceed further, it is important to understand what a window function is.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_a_window_function\"><\/span>What is a window function<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>A windows function is used to perform certain calculation for rows that are related to the present row. It sequentially traverses the table, one row at a time, and in each iteration performs calculations across rows related to this present row. These calculations are generally aggregations or some other statistical function. Unlike GROUP BY clause, that results in a single row of result, window function stores the result of calculation for each row. It is commonly used to calculate running total and rank values. You can learn more about window functions <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.4\/en\/window-functions-usage.html\">here<\/a>.<\/p>\n\n\n\n<p>Here is the SQL query to select top 2 brands per category, based on quantity column.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">WITH RankedSales AS (<br>        SELECT<br>            category,<br>            brand,<br>            quantity,<br>            ROW_NUMBER() OVER(PARTITION BY category ORDER BY quantity DESC) AS rn<br>        FROM sales<br>    )<br><br>    SELECT category,brand,  quantity<br>    FROM RankedSales<br>    WHERE rn &lt;= 2;<br><br>+----------+---------+----------+<br>| category | brand   | quantity |<br>+----------+---------+----------+<br>| Car      | Honda   |      250 |<br>| Car      | Hyundai |      120 |<br>| Laptop   | Samsung |      200 |<br>| Laptop   | IBM     |      150 |<br>| Mobile   | Apple   |      900 |<br>| Mobile   | Samsung |      500 |<br>+----------+---------+----------+<\/pre>\n\n\n\n<p>Let us look at the above queries in detail. We use 2 SQL queries for our purpose.<\/p>\n\n\n\n<p>First of all, we use a common table expression (CTE) named RankedSale to simplify the query. It is optional though.<\/p>\n\n\n\n<p>Next, we use the following line to assign a unique sequential integer to each row, within a partition.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ROW_NUMBER() OVER(PARTITION BY category ORDER BY quantity DESC<\/pre>\n\n\n\n<p>Lastly, we use an SQL query to filter only those rows from RankedSale where rn&lt;=2, that is, where row number &lt;=2.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Using_Sub_Query\"><\/span>2. Using Sub Query<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here is an SQL query to determine the top 2 brands in each category based on quantity field.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT category,brand,quantity<br>   FROM<br>     (SELECT category,brand,quantity, <br>                  @category_rank := IF(@current_category = category, @category_rank + 1, 1) AS category_rank,<br>                  @current_category := category<br>       FROM sales<br>       ORDER BY category,quantity DESC<br>     ) ranked<br>   WHERE category_rank &lt;= 2;<br><br>+----------+---------+----------+<br>| category | brand   | quantity |<br>+----------+---------+----------+<br>| Car      | Honda   |      250 |<br>| Car      | Hyundai |      120 |<br>| Laptop   | Samsung |      200 |<br>| Laptop   | IBM     |      150 |<br>| Mobile   | Apple   |      900 |<br>| Mobile   | Samsung |      500 |<br>+----------+---------+----------+<\/pre>\n\n\n\n<p>Let us look at the above query in detail. Here we use a nested SQL sub query. The outer SQL query iterates over sales table.<\/p>\n\n\n\n<p>The inner query counts the number of records where quantity column has a value greater than or equal to that of the present row. It calculates and stores the category_rank and current_category value for each row.<\/p>\n\n\n\n<p>The condition &lt;=2 will limit the result to only top 2 rows for each category.<\/p>\n\n\n\n<p>In the above query, we use @current_category:=category session variable to store the present ranking category of each row. For each row, if the current_category variable&#8217;s value is same as category column&#8217;s value, we increment category_rank by 1. If it is the first row, we set it to 1.<\/p>\n\n\n\n<p>If you only run inner SQL query, it will return the following result.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT category,brand,quantity, <br>                  @category_rank := IF(@current_category = category, @category_rank + 1, 1) AS category_rank,<br>                  @current_category := category<br>       FROM sales<br>       ORDER BY category,quantity DESC;<br><br>+----------+---------+----------+---------------+-------------------------------+<br>| category | brand   | quantity | category_rank | @current_category := category |<br>+----------+---------+----------+---------------+-------------------------------+<br>| Car      | Honda   |      250 |             1 | Car                           |<br>| Car      | Hyundai |      120 |             2 | Car                           |<br>| Car      | Toyota  |      100 |             3 | Car                           |<br>| Laptop   | Samsung |      200 |             1 | Laptop                        |<br>| Laptop   | IBM     |      150 |             2 | Laptop                        |<br>| Laptop   | Apple   |      100 |             3 | Laptop                        |<br>| Mobile   | Apple   |      900 |             1 | Mobile                        |<br>| Mobile   | Samsung |      500 |             2 | Mobile                        |<br>| Mobile   | Sony    |      100 |             3 | Mobile                        |<br>+----------+---------+----------+---------------+-------------------------------+<\/pre>\n\n\n\n<p>From the above table, we simply select rows where category_rank&lt;=2.<\/p>\n\n\n\n<p>The main problem of this solution is that the inner query is executed for each row of the table and can be inefficient for large tables. It is useful for older versions of MySQL since it does not use window function or session variables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Final_Verdict\"><\/span>Final Verdict<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>If you are using MySQL 8+, then use solution #1 with window functions since it is more efficient for large data sets. Also it is easier to understand.<\/p>\n\n\n\n<p>If you are using MySQL&lt;8, then you need to use solution #2 with sub query.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this article, we have learnt how to select top N rows per group in MySQL. It is an important concept to understand when you <a href=\"https:\/\/ubiq.co\/tech-blog\/top-5-online-resources-to-learn-mysql\/\">learn MySQL<\/a>. We have learnt how to do this using window function as well as using sub query. Using row_number() function with common table expression or sub query is the most efficient way to do this. However, using sub query is suitable and probably your only option in older MySQL (&lt;8.0) systems. You can use either of these solutions as per your requirement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"FAQs\"><\/span>FAQs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>1. When to use Window Function<\/strong><\/p>\n\n\n\n<p>If you use newer MySQL versions (8.0+), then you can use window function since it is supported and more efficient than using sub queries.<\/p>\n\n\n\n<p><strong>2. When to use Sub query<\/strong><\/p>\n\n\n\n<p>If you use older version of MySQL (&lt;8.0), then you should use sub query since it does not support window functions like row_number().<\/p>\n\n\n\n<p>Also read:<br><a href=\"https:\/\/ubiq.co\/tech-blog\/enable-remote-access-mysql\/\">How to Enable Remote Access in MySQL<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/create-read-only-mysql-user\/\">How to Create Read Only MySQL User<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-increase-max-connections-in-mysql\/\">How to Increase Max Connections in MySQL<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Often database developers need to select top n rows per group in a table. Here are different ways to get top rows per group in MySQL.<\/p>\n","protected":false},"author":1,"featured_media":10386,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[324],"tags":[625],"class_list":["post-10354","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mysql","tag-top-n-rows-per-group"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MySQL Select Top N Rows Per Group - Ubiq BI<\/title>\n<meta name=\"description\" content=\"Often database developers need to select top n rows per group in a table. Here are different ways to get top rows per group in MySQL.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL Select Top N Rows Per Group - Ubiq BI\" \/>\n<meta property=\"og:description\" content=\"Often database developers need to select top n rows per group in a table. Here are different ways to get top rows per group in MySQL.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/\" \/>\n<meta property=\"og:site_name\" content=\"Ubiq BI\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ubiqbi\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-22T04:49:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-01-26T06:13:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2026\/01\/mysql-select-top-n-rows-per-group.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"303\" \/>\n\t<meta property=\"og:image:height\" content=\"202\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sreeram Sreenivasan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:site\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sreeram Sreenivasan\" \/>\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:\\\/\\\/ubiq.co\\\/tech-blog\\\/mysql-select-top-n-rows-per-group\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/mysql-select-top-n-rows-per-group\\\/\"},\"author\":{\"name\":\"Sreeram Sreenivasan\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"headline\":\"MySQL Select Top N Rows Per Group\",\"datePublished\":\"2026-01-22T04:49:47+00:00\",\"dateModified\":\"2026-01-26T06:13:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/mysql-select-top-n-rows-per-group\\\/\"},\"wordCount\":943,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/mysql-select-top-n-rows-per-group\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2026\\\/01\\\/mysql-select-top-n-rows-per-group.jpg?fit=303%2C202&ssl=1\",\"keywords\":[\"top n rows per group\"],\"articleSection\":[\"MySQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/mysql-select-top-n-rows-per-group\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/mysql-select-top-n-rows-per-group\\\/\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/mysql-select-top-n-rows-per-group\\\/\",\"name\":\"MySQL Select Top N Rows Per Group - Ubiq BI\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/mysql-select-top-n-rows-per-group\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/mysql-select-top-n-rows-per-group\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2026\\\/01\\\/mysql-select-top-n-rows-per-group.jpg?fit=303%2C202&ssl=1\",\"datePublished\":\"2026-01-22T04:49:47+00:00\",\"dateModified\":\"2026-01-26T06:13:31+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"description\":\"Often database developers need to select top n rows per group in a table. Here are different ways to get top rows per group in MySQL.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/mysql-select-top-n-rows-per-group\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/mysql-select-top-n-rows-per-group\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/mysql-select-top-n-rows-per-group\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2026\\\/01\\\/mysql-select-top-n-rows-per-group.jpg?fit=303%2C202&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2026\\\/01\\\/mysql-select-top-n-rows-per-group.jpg?fit=303%2C202&ssl=1\",\"width\":303,\"height\":202,\"caption\":\"mysql select top n rows per group\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/mysql-select-top-n-rows-per-group\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Select Top N Rows Per Group\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\",\"name\":\"Ubiq BI\",\"description\":\"Build dashboards &amp; reports in minutes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\",\"name\":\"Sreeram Sreenivasan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"caption\":\"Sreeram Sreenivasan\"},\"description\":\"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/sreeram-sreenivasan\\\/\"],\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/author\\\/wordpress\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MySQL Select Top N Rows Per Group - Ubiq BI","description":"Often database developers need to select top n rows per group in a table. Here are different ways to get top rows per group in MySQL.","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:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/","og_locale":"en_US","og_type":"article","og_title":"MySQL Select Top N Rows Per Group - Ubiq BI","og_description":"Often database developers need to select top n rows per group in a table. Here are different ways to get top rows per group in MySQL.","og_url":"https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/","og_site_name":"Ubiq BI","article_publisher":"https:\/\/www.facebook.com\/ubiqbi","article_published_time":"2026-01-22T04:49:47+00:00","article_modified_time":"2026-01-26T06:13:31+00:00","og_image":[{"width":303,"height":202,"url":"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2026\/01\/mysql-select-top-n-rows-per-group.jpg","type":"image\/jpeg"}],"author":"Sreeram Sreenivasan","twitter_card":"summary_large_image","twitter_creator":"@UbiqBI","twitter_site":"@UbiqBI","twitter_misc":{"Written by":"Sreeram Sreenivasan","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/#article","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/"},"author":{"name":"Sreeram Sreenivasan","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"headline":"MySQL Select Top N Rows Per Group","datePublished":"2026-01-22T04:49:47+00:00","dateModified":"2026-01-26T06:13:31+00:00","mainEntityOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/"},"wordCount":943,"commentCount":0,"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2026\/01\/mysql-select-top-n-rows-per-group.jpg?fit=303%2C202&ssl=1","keywords":["top n rows per group"],"articleSection":["MySQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/","url":"https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/","name":"MySQL Select Top N Rows Per Group - Ubiq BI","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/#primaryimage"},"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2026\/01\/mysql-select-top-n-rows-per-group.jpg?fit=303%2C202&ssl=1","datePublished":"2026-01-22T04:49:47+00:00","dateModified":"2026-01-26T06:13:31+00:00","author":{"@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"description":"Often database developers need to select top n rows per group in a table. Here are different ways to get top rows per group in MySQL.","breadcrumb":{"@id":"https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/#primaryimage","url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2026\/01\/mysql-select-top-n-rows-per-group.jpg?fit=303%2C202&ssl=1","contentUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2026\/01\/mysql-select-top-n-rows-per-group.jpg?fit=303%2C202&ssl=1","width":303,"height":202,"caption":"mysql select top n rows per group"},{"@type":"BreadcrumbList","@id":"https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ubiq.co\/tech-blog\/"},{"@type":"ListItem","position":2,"name":"MySQL Select Top N Rows Per Group"}]},{"@type":"WebSite","@id":"https:\/\/ubiq.co\/tech-blog\/#website","url":"https:\/\/ubiq.co\/tech-blog\/","name":"Ubiq BI","description":"Build dashboards &amp; reports in minutes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc","name":"Sreeram Sreenivasan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","caption":"Sreeram Sreenivasan"},"description":"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.","sameAs":["https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/"],"url":"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2026\/01\/mysql-select-top-n-rows-per-group.jpg?fit=303%2C202&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pbGGTT-2H0","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/10354","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/comments?post=10354"}],"version-history":[{"count":38,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/10354\/revisions"}],"predecessor-version":[{"id":10430,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/10354\/revisions\/10430"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media\/10386"}],"wp:attachment":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media?parent=10354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/categories?post=10354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/tags?post=10354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}