{"id":8390,"date":"2021-01-29T04:24:00","date_gmt":"2021-01-29T04:24:00","guid":{"rendered":"https:\/\/ubiq.co\/tech-blog\/?p=8390"},"modified":"2026-01-22T04:50:11","modified_gmt":"2026-01-22T04:50:11","slug":"select-top-10-records-for-each-category-in-mysql","status":"publish","type":"post","link":"https:\/\/ubiq.co\/tech-blog\/select-top-10-records-for-each-category-in-mysql\/","title":{"rendered":"Select Top 10 Records for Each Category in MySQL"},"content":{"rendered":"\n<p>When you run an ecommerce business or online store, you may need to select top 10 records for each category of products, brands, etc. for reporting and analysis. MySQL database makes it very easy to carry out this kind of querying. In fact, many web developers need to be able to <a href=\"https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/\">fetch top N results in each group<\/a> of their data. In this article, we will look at how to select top 10 records for each category 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\/select-top-10-records-for-each-category-in-mysql\/#Select_Top_10_Records_for_Each_Category\" >Select Top 10 Records for Each Category<\/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\/select-top-10-records-for-each-category-in-mysql\/#1_Using_User-Defined_Variable\" >1. Using User-Defined Variable<\/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\/select-top-10-records-for-each-category-in-mysql\/#2_Using_Row_number\" >2. Using Row_number()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ubiq.co\/tech-blog\/select-top-10-records-for-each-category-in-mysql\/#3_Using_Rank\" >3. Using Rank()<\/a><\/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\/select-top-10-records-for-each-category-in-mysql\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Select_Top_10_Records_for_Each_Category\"><\/span>Select Top 10 Records for Each Category<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here are the steps to select top 10 records for each category. Let us say you have the following table <em>sales(id, product, order_date, amount)<\/em>. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; create table sales(id int, \n        product  varchar(255), \n        order_date date, \n        amount int);\n\nmysql&gt; insert into sales(id, product, order_date, amount)\n      values(1,'A','2021-01-01',125),\n      (2,'A','2021-01-02',225),\n      (3,'A','2021-01-03',325),\n      (4,'A','2021-01-04',105),\n      (5,'A','2021-01-05',150),\n      (6,'A','2021-01-06',215),\n      (7,'A','2021-01-07',205),\n      (8,'A','2021-01-08',125),\n      (9,'A','2021-01-09',350),\n      (10,'A','2021-01-10',235),\n      (11,'A','2021-01-11',325),\n      (12,'B','2021-01-01',150),\n      (13,'B','2021-01-02',165),\n      (14,'B','2021-01-03',145),\n      (15,'B','2021-01-04',115),\n      (16,'B','2021-01-05',105),\n      (17,'B','2021-01-06',195),\n      (18,'B','2021-01-07',185),\n      (19,'B','2021-01-08',175),\n      (20,'B','2021-01-09',165),\n      (21,'B','2021-01-10',115),\n      (22,'B','2021-01-11',125);\n\n\n mysql&gt; select * from sales;\n +------+---------+------------+--------+\n | id   | product | order_date | amount |\n +------+---------+------------+--------+\n |    1 | A       | 2021-01-01 |    125 |\n |    2 | A       | 2021-01-02 |    225 |\n |    3 | A       | 2021-01-03 |    325 |\n |    4 | A       | 2021-01-04 |    105 |\n |    5 | A       | 2021-01-05 |    150 |\n |    6 | A       | 2021-01-06 |    215 |\n |    7 | A       | 2021-01-07 |    205 |\n |    8 | A       | 2021-01-08 |    125 |\n |    9 | A       | 2021-01-09 |    350 |\n |   10 | A       | 2021-01-10 |    235 |\n |   11 | A       | 2021-01-11 |    325 |\n |   12 | B       | 2021-01-01 |    150 |\n |   13 | B       | 2021-01-02 |    165 |\n |   14 | B       | 2021-01-03 |    145 |\n |   15 | B       | 2021-01-04 |    115 |\n |   16 | B       | 2021-01-05 |    105 |\n |   17 | B       | 2021-01-06 |    195 |\n |   18 | B       | 2021-01-07 |    185 |\n |   19 | B       | 2021-01-08 |    175 |\n |   20 | B       | 2021-01-09 |    165 |\n |   21 | B       | 2021-01-10 |    115 |\n |   22 | B       | 2021-01-11 |    125 |\n +------+---------+------------+--------+\n<\/pre>\n\n\n\n<p>There are several ways to pick top N records from each group in a table. We will look at the most common solutions to this problem.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Using_User-Defined_Variable\"><\/span>1. Using User-Defined Variable<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We will select top 10 records by <em>amount<\/em> column for each category (product). First, we will rank each row within its group (product). You can do this with the following SQL query.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; SELECT id, product, amount, \n@product_rank := IF(@current_product = product, @product_rank + 1, 1) AS product_rank, \n@current_product := product as current_product \n      FROM product_orders \n      ORDER BY product, amount desc;\n\n+------+---------+--------+--------------+-----------------+\n | id   | product | amount | product_rank | current_product |\n +------+---------+--------+--------------+-----------------+\n |    9 | A       |    350 |            1 | A               |\n |    3 | A       |    325 |            2 | A               |\n |   11 | A       |    325 |            3 | A               |\n |   10 | A       |    235 |            4 | A               |\n |    2 | A       |    225 |            5 | A               |\n |    6 | A       |    215 |            6 | A               |\n |    7 | A       |    205 |            7 | A               |\n |    5 | A       |    150 |            8 | A               |\n |    1 | A       |    125 |            9 | A               |\n |    8 | A       |    125 |           10 | A               |\n |    4 | A       |    105 |           11 | A               |\n |   17 | B       |    195 |            1 | B               |\n |   18 | B       |    185 |            2 | B               |\n |   19 | B       |    175 |            3 | B               |\n |   20 | B       |    165 |            4 | B               |\n |   13 | B       |    165 |            5 | B               |\n |   12 | B       |    150 |            6 | B               |\n |   14 | B       |    145 |            7 | B               |\n |   22 | B       |    125 |            8 | B               |\n |   15 | B       |    115 |            9 | B               |\n |   21 | B       |    115 |           10 | B               |\n |   16 | B       |    105 |           11 | B               |\n +------+---------+--------+--------------+-----------------+<\/pre>\n\n\n\n<p>In above query, we use temporary variables <em>product_rank<\/em> and <em>current_product<\/em> to keep track of latest rank value and product value.<\/p>\n\n\n\n<p>Next, we will use the above query as a subquery, to select top 10 records for each product.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; select id, product, amount \n       from ( <strong>SELECT id, product, amount, <\/strong>\n<strong>@product_rank := IF(@current_product = product, @product_rank + 1, 1) AS product_rank, <\/strong>\n<strong>@current_product := product FROM sales ORDER BY product, amount desc<\/strong> ) \nranked_rows \nwhere <strong>product_rank&lt;=10<\/strong>;\n +------+---------+--------+\n | id   | product | amount |\n +------+---------+--------+\n |    9 | A       |    350 |\n |    3 | A       |    325 |\n |   11 | A       |    325 |\n |   10 | A       |    235 |\n |    2 | A       |    225 |\n |    6 | A       |    215 |\n |    7 | A       |    205 |\n |    5 | A       |    150 |\n |    1 | A       |    125 |\n |    8 | A       |    125 |\n |   17 | B       |    195 |\n |   18 | B       |    185 |\n |   19 | B       |    175 |\n |   20 | B       |    165 |\n |   13 | B       |    165 |\n |   12 | B       |    150 |\n |   14 | B       |    145 |\n |   22 | B       |    125 |\n |   15 | B       |    115 |\n |   21 | B       |    115 |\n +------+---------+--------+<\/pre>\n\n\n\n<p>We have used the condition <em>where product_rank&lt;=10<\/em> to select only top 10 records. You can modify it to select as many rows as you want.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Using_Row_number\"><\/span>2. Using Row_number()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Starting MySQL 8+, you can use Row_number() for this purpose. It is a powerful <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.4\/en\/window-function-descriptions.html\" target=\"_blank\" rel=\"noreferrer noopener\">window function<\/a> that allows you to assign a sequential number for each row in a partition\/group.<\/p>\n\n\n\n<p>Here is the basic SQL query to get top N rows per group in a table. Here we group the table using grouping_column and assign row number for each row within each group and store it in row_num column. We use this result in subquery to select rows where row_num&lt;=N.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT * FROM (<br><br>SELECT column_names,<br>ROW_NUMBER() OVER (PARTITION BY grouping_column ORDER BY ordering_column DESC) AS row_num<br><br>FROM table_name<br>) temp<br>WHERE row_num &lt;= N;<\/pre>\n\n\n\n<p>We use the above query to assign row number for each row for product groups. We store the row numbers in row_num column.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT id, product, amount,<br>ROW_NUMBER() OVER (PARTITION BY product ORDER BY amount DESC) AS row_num<br>FROM sales<\/pre>\n\n\n\n<p>We use the result of above query as a subquery to get top N rows for each group, that is, product.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT * <br>FROM (<br><br>SELECT id, product, amount,<br>ROW_NUMBER() OVER (PARTITION BY grouping_column ORDER BY ordering_column DESC) AS row_num<br>FROM sales<br><br>) temp<br>WHERE row_num &lt;= 10;<br><br> +------+---------+--------+<br> | id   | product | amount |<br> +------+---------+--------+<br> |    9 | A       |    350 |<br> |    3 | A       |    325 |<br> |   11 | A       |    325 |<br> |   10 | A       |    235 |<br> |    2 | A       |    225 |<br> |    6 | A       |    215 |<br> |    7 | A       |    205 |<br> |    5 | A       |    150 |<br> |    1 | A       |    125 |<br> |    8 | A       |    125 |<br> |   17 | B       |    195 |<br> |   18 | B       |    185 |<br> |   19 | B       |    175 |<br> |   20 | B       |    165 |<br> |   13 | B       |    165 |<br> |   12 | B       |    150 |<br> |   14 | B       |    145 |<br> |   22 | B       |    125 |<br> |   15 | B       |    115 |<br> |   21 | B       |    115 |<br> +------+---------+--------+<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Using_Rank\"><\/span>3. Using Rank()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>MySQL &gt;= 8 also supports rank() and dense_rank() window functions that are similar to row_number() function mentioned above. Both basically let you group rows in table and rank rows within each group. Rank() returns ranking with gaps whereas dense_rank() returns ranking without gaps, that is, in case of ties.<\/p>\n\n\n\n<p>Here is the basic SQL query to rank your table.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT column_names,<br>    DENSE_RANK() OVER (PARTITION BY grouping_column ORDER BY ordering_column DESC) AS rank<br>FROM<br>    table_name;<br><\/pre>\n\n\n\n<p>We will use this query to group our table by product and rank rows within each group.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT id, product, amount,<br>    DENSE_RANK() OVER (PARTITION BY product ORDER BY amount DESC) AS rank<br>FROM<br>    sales;<\/pre>\n\n\n\n<p>We use the above query as a subquery and select top N rows(N=10) in it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT * FROM (<br><br>SELECT id, product, amount,<br>    DENSE_RANK() OVER (PARTITION BY product ORDER BY amount DESC) AS rank<br>FROM<br>    sales;<br><br>) temp<br>WHERE row_num &lt;= 10;<br><br>+------+---------+--------+<br> | id   | product | amount |<br> +------+---------+--------+<br> |    9 | A       |    350 |<br> |    3 | A       |    325 |<br> |   11 | A       |    325 |<br> |   10 | A       |    235 |<br> |    2 | A       |    225 |<br> |    6 | A       |    215 |<br> |    7 | A       |    205 |<br> |    5 | A       |    150 |<br> |    1 | A       |    125 |<br> |    8 | A       |    125 |<br> |   17 | B       |    195 |<br> |   18 | B       |    185 |<br> |   19 | B       |    175 |<br> |   20 | B       |    165 |<br> |   13 | B       |    165 |<br> |   12 | B       |    150 |<br> |   14 | B       |    145 |<br> |   22 | B       |    125 |<br> |   15 | B       |    115 |<br> |   21 | B       |    115 |<br> +------+---------+--------+<\/pre>\n\n\n\n<p>When we use dense_rank(), in case of ties, the next rank will not be skipped. In case of rank() function, it will be skipped. In other words, when you use dense_rank() and there is a tie, then all those rows will be assigned the same rank and the next row will have a rank 1 more than previous rank. In case of rank() function, when there is a tie, all those rows will be assigned the same rank and the next row will have rank=previous rank + no. of duplicates. Depending on your requirement, you can use rank() or dense_rank() function.<\/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 10 records from each group in a table. If you are using MySQL &gt;=8.0 then you can use rank(), dense_rank() or row_number() functions to quickly partition your table, and rank rows within each group. For older versions, you can use user-defined variables as mentioned in solution #1.<\/p>\n\n\n\n<p>This is a commonly required analysis in almost every organization. Typically, teams need to generate regular reports on top N products, categories, brands, territories, and more. You can use any of the above methods to easily generate such reports.<\/p>\n\n\n\n<p>Need a reporting tool for MySQL?&nbsp;<a href=\"http:\/\/ubiq.co\/\">Ubiq<\/a>&nbsp;makes it easy to visualize data in minutes, and monitor in real-time dashboards.&nbsp;<a href=\"http:\/\/ubiq.co\/accounts\/register\">Try it<\/a>&nbsp;Today!<\/p>\n\n\n\n<p>Also read:<br><a href=\"https:\/\/ubiq.co\/tech-blog\/common-table-expression-in-mysql\/\">Common Table Expression (CTEs) in MySQL<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-calculate-margin-in-mysql\/\">How to Calculate Margin in MySQL<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/mysql-query-to-get-top-selling-products\/\">How to Get Top Selling Products in MySQL<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you may need to select top n records for each category. Here is how to select top 10 records for each category in MySQL.<\/p>\n","protected":false},"author":1,"featured_media":8391,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[324],"tags":[441],"class_list":["post-8390","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mysql","tag-top-10"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Select Top 10 Records for Each Category in MySQL - Ubiq BI<\/title>\n<meta name=\"description\" content=\"Sometimes you may need to select top n records for each category. Here is how to select top 10 records for each category 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\/select-top-10-records-for-each-category-in-mysql\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Select Top 10 Records for Each Category in MySQL - Ubiq BI\" \/>\n<meta property=\"og:description\" content=\"Sometimes you may need to select top n records for each category. Here is how to select top 10 records for each category in MySQL.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ubiq.co\/tech-blog\/select-top-10-records-for-each-category-in-mysql\/\" \/>\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=\"2021-01-29T04:24:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-01-22T04:50:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/06\/select-top-10-records.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"730\" \/>\n\t<meta property=\"og:image:height\" content=\"410\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\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=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/select-top-10-records-for-each-category-in-mysql\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/select-top-10-records-for-each-category-in-mysql\\\/\"},\"author\":{\"name\":\"Sreeram Sreenivasan\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"headline\":\"Select Top 10 Records for Each Category in MySQL\",\"datePublished\":\"2021-01-29T04:24:00+00:00\",\"dateModified\":\"2026-01-22T04:50:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/select-top-10-records-for-each-category-in-mysql\\\/\"},\"wordCount\":720,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/select-top-10-records-for-each-category-in-mysql\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2025\\\/06\\\/select-top-10-records.webp?fit=730%2C410&ssl=1\",\"keywords\":[\"top 10\"],\"articleSection\":[\"MySQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/select-top-10-records-for-each-category-in-mysql\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/select-top-10-records-for-each-category-in-mysql\\\/\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/select-top-10-records-for-each-category-in-mysql\\\/\",\"name\":\"Select Top 10 Records for Each Category in MySQL - Ubiq BI\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/select-top-10-records-for-each-category-in-mysql\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/select-top-10-records-for-each-category-in-mysql\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2025\\\/06\\\/select-top-10-records.webp?fit=730%2C410&ssl=1\",\"datePublished\":\"2021-01-29T04:24:00+00:00\",\"dateModified\":\"2026-01-22T04:50:11+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"description\":\"Sometimes you may need to select top n records for each category. Here is how to select top 10 records for each category in MySQL.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/select-top-10-records-for-each-category-in-mysql\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/select-top-10-records-for-each-category-in-mysql\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/select-top-10-records-for-each-category-in-mysql\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2025\\\/06\\\/select-top-10-records.webp?fit=730%2C410&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2025\\\/06\\\/select-top-10-records.webp?fit=730%2C410&ssl=1\",\"width\":730,\"height\":410,\"caption\":\"select top 10 records\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/select-top-10-records-for-each-category-in-mysql\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Select Top 10 Records for Each Category in MySQL\"}]},{\"@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":"Select Top 10 Records for Each Category in MySQL - Ubiq BI","description":"Sometimes you may need to select top n records for each category. Here is how to select top 10 records for each category 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\/select-top-10-records-for-each-category-in-mysql\/","og_locale":"en_US","og_type":"article","og_title":"Select Top 10 Records for Each Category in MySQL - Ubiq BI","og_description":"Sometimes you may need to select top n records for each category. Here is how to select top 10 records for each category in MySQL.","og_url":"https:\/\/ubiq.co\/tech-blog\/select-top-10-records-for-each-category-in-mysql\/","og_site_name":"Ubiq BI","article_publisher":"https:\/\/www.facebook.com\/ubiqbi","article_published_time":"2021-01-29T04:24:00+00:00","article_modified_time":"2026-01-22T04:50:11+00:00","og_image":[{"width":730,"height":410,"url":"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/06\/select-top-10-records.webp","type":"image\/webp"}],"author":"Sreeram Sreenivasan","twitter_card":"summary_large_image","twitter_creator":"@UbiqBI","twitter_site":"@UbiqBI","twitter_misc":{"Written by":"Sreeram Sreenivasan","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ubiq.co\/tech-blog\/select-top-10-records-for-each-category-in-mysql\/#article","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/select-top-10-records-for-each-category-in-mysql\/"},"author":{"name":"Sreeram Sreenivasan","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"headline":"Select Top 10 Records for Each Category in MySQL","datePublished":"2021-01-29T04:24:00+00:00","dateModified":"2026-01-22T04:50:11+00:00","mainEntityOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/select-top-10-records-for-each-category-in-mysql\/"},"wordCount":720,"commentCount":0,"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/select-top-10-records-for-each-category-in-mysql\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/06\/select-top-10-records.webp?fit=730%2C410&ssl=1","keywords":["top 10"],"articleSection":["MySQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ubiq.co\/tech-blog\/select-top-10-records-for-each-category-in-mysql\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ubiq.co\/tech-blog\/select-top-10-records-for-each-category-in-mysql\/","url":"https:\/\/ubiq.co\/tech-blog\/select-top-10-records-for-each-category-in-mysql\/","name":"Select Top 10 Records for Each Category in MySQL - Ubiq BI","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/select-top-10-records-for-each-category-in-mysql\/#primaryimage"},"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/select-top-10-records-for-each-category-in-mysql\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/06\/select-top-10-records.webp?fit=730%2C410&ssl=1","datePublished":"2021-01-29T04:24:00+00:00","dateModified":"2026-01-22T04:50:11+00:00","author":{"@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"description":"Sometimes you may need to select top n records for each category. Here is how to select top 10 records for each category in MySQL.","breadcrumb":{"@id":"https:\/\/ubiq.co\/tech-blog\/select-top-10-records-for-each-category-in-mysql\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ubiq.co\/tech-blog\/select-top-10-records-for-each-category-in-mysql\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ubiq.co\/tech-blog\/select-top-10-records-for-each-category-in-mysql\/#primaryimage","url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/06\/select-top-10-records.webp?fit=730%2C410&ssl=1","contentUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/06\/select-top-10-records.webp?fit=730%2C410&ssl=1","width":730,"height":410,"caption":"select top 10 records"},{"@type":"BreadcrumbList","@id":"https:\/\/ubiq.co\/tech-blog\/select-top-10-records-for-each-category-in-mysql\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ubiq.co\/tech-blog\/"},{"@type":"ListItem","position":2,"name":"Select Top 10 Records for Each Category in MySQL"}]},{"@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\/2025\/06\/select-top-10-records.webp?fit=730%2C410&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pbGGTT-2bk","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/8390","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=8390"}],"version-history":[{"count":3,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/8390\/revisions"}],"predecessor-version":[{"id":10394,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/8390\/revisions\/10394"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media\/8391"}],"wp:attachment":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media?parent=8390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/categories?post=8390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/tags?post=8390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}