{"id":9849,"date":"2025-11-05T05:39:10","date_gmt":"2025-11-05T05:39:10","guid":{"rendered":"https:\/\/ubiq.co\/tech-blog\/?p=9849"},"modified":"2025-11-05T05:56:17","modified_gmt":"2025-11-05T05:56:17","slug":"unpivot-table-mysql","status":"publish","type":"post","link":"https:\/\/ubiq.co\/tech-blog\/unpivot-table-mysql\/","title":{"rendered":"How to Unpivot Table in MySQL"},"content":{"rendered":"\n<p>Sometimes, while working with database tables, MySQL developers need to convert one or more of its columns into rows. In other words, you need to transform a wide table into a long table, for easier analysis. This is known as Unpivot. Unfortunately, there is no direct UNPIVOT clause or function in MySQL. So we will need to construct an SQL query for this purpose. In this article, we will learn how to unpivot table 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\/unpivot-table-mysql\/#What_is_Unpivot_in_MySQL\" >What is Unpivot in MySQL<\/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\/unpivot-table-mysql\/#How_to_Unpivot_Table_in_MySQL\" >How to Unpivot Table in MySQL<\/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\/unpivot-table-mysql\/#Single_Column\" >Single Column<\/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\/unpivot-table-mysql\/#Multiple_Columns\" >Multiple Columns<\/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\/unpivot-table-mysql\/#Stored_Procedure\" >Stored Procedure<\/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\/unpivot-table-mysql\/#Dynamic_SQL\" >Dynamic SQL<\/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\/unpivot-table-mysql\/#Tips_Best_Practices\" >Tips &amp; Best Practices<\/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\/unpivot-table-mysql\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_Unpivot_in_MySQL\"><\/span>What is Unpivot in MySQL<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Unpivot table is a process where several columns of a table are converted into rows, changing a wide table into a long one. Here is an example of table where monthly sales values are stored as separate columns.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>Product<\/td><td>Jan_Sales<\/td><td>Feb_Sales<\/td><td>Mar_Sales<\/td><\/tr><tr><td>A<\/td><td>100<\/td><td>110<\/td><td>90<\/td><\/tr><tr><td>B<\/td><td>90<\/td><td>100<\/td><td>110<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>It is difficult to aggregate product sales using the above table. Also, the number of columns in table will keep increasing as we keep adding more sales data to it. This makes it difficult to manage data. Here is an example of the unpivoted version of above table.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>Product<\/td><td>Month<\/td><td>Sales<\/td><\/tr><tr><td>A<\/td><td>Jan<\/td><td>100<\/td><\/tr><tr><td>A<\/td><td>Feb<\/td><td>110<\/td><\/tr><tr><td>A<\/td><td>Mar<\/td><td>90<\/td><\/tr><tr><td>B<\/td><td>Jan<\/td><td>90<\/td><\/tr><tr><td>B<\/td><td>Feb<\/td><td>100<\/td><\/tr><tr><td>B<\/td><td>Mar<\/td><td>110<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Unpivoting table is commonly required in data analysis and reporting. For example, when you unpivot table, some of its column names become row values. This makes it easy to aggregate data and generate reports.<\/p>\n\n\n\n<p>For example, in the above table, you can easily calculate product wise sales.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>Product<\/td><td>Sales<\/td><\/tr><tr><td>A<\/td><td>300<\/td><\/tr><tr><td>B<\/td><td>300<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Unpivot_Table_in_MySQL\"><\/span>How to Unpivot Table in MySQL<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are several different ways to unpivot table in MySQL. Let us look at them one by one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Single_Column\"><\/span>Single Column<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The most basic way to unpivot table is to use UNION ALL. Here is the SQL query to unpivot our above table.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT Product, 'Jan' AS Month, Jan_Sales AS Sales<br>FROM sales<br>UNION ALL<br>SELECT Product, 'Feb' AS Month, Feb_Sales AS Sales<br>FROM sales<br>UNION ALL<br>SELECT Product, 'Mar' AS Month, Mar_Sales AS Sales<br>FROM sales;<\/pre>\n\n\n\n<p>Here is the output.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>Product<\/td><td>Month<\/td><td>Sales<\/td><\/tr><tr><td>A<\/td><td>Jan<\/td><td>100<\/td><\/tr><tr><td>A<\/td><td>Feb<\/td><td>110<\/td><\/tr><tr><td>A<\/td><td>Mar<\/td><td>90<\/td><\/tr><tr><td>B<\/td><td>Jan<\/td><td>90<\/td><\/tr><tr><td>B<\/td><td>Feb<\/td><td>100<\/td><\/tr><tr><td>B<\/td><td>Mar<\/td><td>110<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>In the above query, we create separate sub queries to get the sales result of each month and use UNION ALL clause to combine them. In each query, we store the month name under Month column and the sales value under Sales column.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Multiple_Columns\"><\/span>Multiple Columns<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In the above example, we are only unpivoting a single column, sales. You can also use the above approach to unpivot multiple columns. Let us say you have the following table with 2 columns sales and orders that are pivoted.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>Product<\/td><td>Jan_Sales<\/td><td>Feb_Sales<\/td><td>Mar_Sales<\/td><td>Jan_Orders<\/td><td>Feb_Orders<\/td><td>Mar_Orders<\/td><\/tr><tr><td>A<\/td><td>100<\/td><td>120<\/td><td>90<\/td><td>10<\/td><td>9<\/td><td>11<\/td><\/tr><tr><td>B<\/td><td>50<\/td><td>60<\/td><td>70<\/td><td>3<\/td><td>5<\/td><td>4<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Here is the SQL query to easily unpivot both sales as well as orders using a single query.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT Product, 'Jan' AS Month, Jan_Sales AS Sales, Jan_Orders as Orders<br>FROM sales<br>UNION ALL<br>SELECT Product, 'Feb' AS Month, Feb_Sales AS Sales, Feb_Orders as Orders<br>FROM sales<br>UNION ALL<br>SELECT Product, 'Mar' AS Month, Mar_Sales AS Sales, Mar_Orders as Orders<br>FROM sales;<\/pre>\n\n\n\n<p>Here is the output you will see.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>Product<\/td><td>Month<\/td><td>Sales<\/td><td>Orders<\/td><\/tr><tr><td>A<\/td><td>Jan<\/td><td>100<\/td><td>10<\/td><\/tr><tr><td>A<\/td><td>Feb<\/td><td>120<\/td><td>9<\/td><\/tr><tr><td>A<\/td><td>Mar<\/td><td>90<\/td><td>11<\/td><\/tr><tr><td>B<\/td><td>Jan<\/td><td>50<\/td><td>3<\/td><\/tr><tr><td>B<\/td><td>Feb<\/td><td>60<\/td><td>5<\/td><\/tr><tr><td>B<\/td><td>Mar<\/td><td>70<\/td><td>4<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>In the above query, we create separate sub queries to get each month&#8217;s sales and orders and use UNION ALL to append it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Stored_Procedure\"><\/span>Stored Procedure<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>If you need to unpivot data frequently, then you can create a stored procedure for this purpose. Here is an example to create stored procedure named unpivot_data().<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">DELIMITER $$<br><br>CREATE PROCEDURE unpivot_data()<br>BEGIN<br>    SELECT Product, 'Jan' AS Month, Jan_Sales AS Sales FROM sales<br>    UNION ALL<br>    SELECT Product, 'Feb' AS Month, Feb_Sales AS Sales FROM sales<br>    UNION ALL<br>    SELECT Product, 'Mar' AS Month, Mar_Sales AS Sales FROM sales;<br>END$$<br><br>DELIMITER ;<\/pre>\n\n\n\n<p>In the above query, we place our earlier SQL query within BEGIN&#8230;END block. We can call this procedure as shown.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">call unpivot_data();<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Dynamic_SQL\"><\/span>Dynamic SQL<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>All the above queries work when the number of columns and their names are fixed, and known in advance. But what if you do not know all column names, or if the number of columns keep increasing as more data gets added to the table? In such cases, we need to use a dynamic SQL query. Here is an example to use Dynamic SQL query to unpivot a varying number of columns.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SET @sql = NULL;<br>SELECT GROUP_CONCAT(DISTINCT<br>    CONCAT(<br>      'SELECT Product, ''', column_name, ''' AS Month, ', column_name, ' AS Sales FROM sales'<br>    )<br>  ) INTO @sql<br>FROM information_schema.columns<br>WHERE table_name = 'sales' AND column_name LIKE '%_Sales';<br><br>SET @sql = CONCAT(@sql, ' ORDER BY Product');<br>PREPARE stmt FROM @sql;<br>EXECUTE stmt;<br>DEALLOCATE PREPARE stmt;<\/pre>\n\n\n\n<p>In the above query, we use <a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-concatenate-multiple-mysql-rows-into-one-field\/\">group_concat<\/a> and concat functions to create dynamic SQL query to unpivot our tables. We get the list of column names from information_schema.columns table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Tips_Best_Practices\"><\/span>Tips &amp; Best Practices<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here are some of the things to keep in mind while unpivoting table.<\/p>\n\n\n\n<p>1. When you use UNION or UNION ALL clause, ensure that the columns being combined, have the same data type.<\/p>\n\n\n\n<p>2. Only Unpivot necessary columns. If unnecessarily unpivot all columns, then some of the column values will be duplicated as row values.<\/p>\n\n\n\n<p>3. Please note, unpivoting and pivoting are process-heavy operations. So the bigger your table, the longer it will take to unpivot it. In such cases, it is better to use indexes.<\/p>\n\n\n\n<p>4. When you unpivot table, it may result in new <a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-alter-column-from-null-to-not-null\/\">columns having NULLs<\/a> and duplicate values.<\/p>\n\n\n\n<p>5. Label your new columns and rows properly so that the data is aligned properly, while using UNION ALL. Otherwise, it will create separate columns for different column names.<\/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 several different ways to unpivot table in MySQL database. We learnt how to unpivot single as well as multiple columns using UNION ALL. We also learnt how to create STORED PROCEDURE to frequently call these queries without retyping them. You can use any of these methods to transform columns into rows and convert a wide table into a long one. Unpivoting a table is very useful in data aggregation and analysis. It is commonly required by business analysts and data scientists.<\/p>\n\n\n\n<p>Also read:<br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-alter-column-from-null-to-not-null\/\">How to Alter Column from Null to Not Null<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-import-sql-file-in-mysql\/\">How to Import SQL File in MySQL<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-concatenate-multiple-mysql-rows-into-one-field\/\">How to Concatenate Multiple SQL Rows in MySQL<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes MySQL developers need to unpivot table in MySQL. Here are the steps to unpivot MySQL database table.<\/p>\n","protected":false},"author":1,"featured_media":9876,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[324],"tags":[614],"class_list":["post-9849","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mysql","tag-unpivot"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Unpivot Table in MySQL - Ubiq BI<\/title>\n<meta name=\"description\" content=\"Sometimes MySQL developers need to unpivot table in MySQL. Here are the steps to unpivot MySQL database table.\" \/>\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\/unpivot-table-mysql\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Unpivot Table in MySQL - Ubiq BI\" \/>\n<meta property=\"og:description\" content=\"Sometimes MySQL developers need to unpivot table in MySQL. Here are the steps to unpivot MySQL database table.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ubiq.co\/tech-blog\/unpivot-table-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=\"2025-11-05T05:39:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-11-05T05:56:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/11\/unpivot-table-mysql.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"346\" \/>\n\t<meta property=\"og:image:height\" content=\"231\" \/>\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\\\/unpivot-table-mysql\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/unpivot-table-mysql\\\/\"},\"author\":{\"name\":\"Sreeram Sreenivasan\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"headline\":\"How to Unpivot Table in MySQL\",\"datePublished\":\"2025-11-05T05:39:10+00:00\",\"dateModified\":\"2025-11-05T05:56:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/unpivot-table-mysql\\\/\"},\"wordCount\":879,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/unpivot-table-mysql\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2025\\\/11\\\/unpivot-table-mysql.jpg?fit=346%2C231&ssl=1\",\"keywords\":[\"unpivot\"],\"articleSection\":[\"MySQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/unpivot-table-mysql\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/unpivot-table-mysql\\\/\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/unpivot-table-mysql\\\/\",\"name\":\"How to Unpivot Table in MySQL - Ubiq BI\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/unpivot-table-mysql\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/unpivot-table-mysql\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2025\\\/11\\\/unpivot-table-mysql.jpg?fit=346%2C231&ssl=1\",\"datePublished\":\"2025-11-05T05:39:10+00:00\",\"dateModified\":\"2025-11-05T05:56:17+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"description\":\"Sometimes MySQL developers need to unpivot table in MySQL. Here are the steps to unpivot MySQL database table.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/unpivot-table-mysql\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/unpivot-table-mysql\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/unpivot-table-mysql\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2025\\\/11\\\/unpivot-table-mysql.jpg?fit=346%2C231&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2025\\\/11\\\/unpivot-table-mysql.jpg?fit=346%2C231&ssl=1\",\"width\":346,\"height\":231,\"caption\":\"unpivot table in mysql\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/unpivot-table-mysql\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Unpivot Table 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":"How to Unpivot Table in MySQL - Ubiq BI","description":"Sometimes MySQL developers need to unpivot table in MySQL. Here are the steps to unpivot MySQL database table.","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\/unpivot-table-mysql\/","og_locale":"en_US","og_type":"article","og_title":"How to Unpivot Table in MySQL - Ubiq BI","og_description":"Sometimes MySQL developers need to unpivot table in MySQL. Here are the steps to unpivot MySQL database table.","og_url":"https:\/\/ubiq.co\/tech-blog\/unpivot-table-mysql\/","og_site_name":"Ubiq BI","article_publisher":"https:\/\/www.facebook.com\/ubiqbi","article_published_time":"2025-11-05T05:39:10+00:00","article_modified_time":"2025-11-05T05:56:17+00:00","og_image":[{"width":346,"height":231,"url":"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/11\/unpivot-table-mysql.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\/unpivot-table-mysql\/#article","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/unpivot-table-mysql\/"},"author":{"name":"Sreeram Sreenivasan","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"headline":"How to Unpivot Table in MySQL","datePublished":"2025-11-05T05:39:10+00:00","dateModified":"2025-11-05T05:56:17+00:00","mainEntityOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/unpivot-table-mysql\/"},"wordCount":879,"commentCount":0,"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/unpivot-table-mysql\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/11\/unpivot-table-mysql.jpg?fit=346%2C231&ssl=1","keywords":["unpivot"],"articleSection":["MySQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ubiq.co\/tech-blog\/unpivot-table-mysql\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ubiq.co\/tech-blog\/unpivot-table-mysql\/","url":"https:\/\/ubiq.co\/tech-blog\/unpivot-table-mysql\/","name":"How to Unpivot Table in MySQL - Ubiq BI","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/unpivot-table-mysql\/#primaryimage"},"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/unpivot-table-mysql\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/11\/unpivot-table-mysql.jpg?fit=346%2C231&ssl=1","datePublished":"2025-11-05T05:39:10+00:00","dateModified":"2025-11-05T05:56:17+00:00","author":{"@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"description":"Sometimes MySQL developers need to unpivot table in MySQL. Here are the steps to unpivot MySQL database table.","breadcrumb":{"@id":"https:\/\/ubiq.co\/tech-blog\/unpivot-table-mysql\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ubiq.co\/tech-blog\/unpivot-table-mysql\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ubiq.co\/tech-blog\/unpivot-table-mysql\/#primaryimage","url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/11\/unpivot-table-mysql.jpg?fit=346%2C231&ssl=1","contentUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/11\/unpivot-table-mysql.jpg?fit=346%2C231&ssl=1","width":346,"height":231,"caption":"unpivot table in mysql"},{"@type":"BreadcrumbList","@id":"https:\/\/ubiq.co\/tech-blog\/unpivot-table-mysql\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ubiq.co\/tech-blog\/"},{"@type":"ListItem","position":2,"name":"How to Unpivot Table 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\/11\/unpivot-table-mysql.jpg?fit=346%2C231&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pbGGTT-2yR","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/9849","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=9849"}],"version-history":[{"count":30,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/9849\/revisions"}],"predecessor-version":[{"id":9880,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/9849\/revisions\/9880"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media\/9876"}],"wp:attachment":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media?parent=9849"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/categories?post=9849"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/tags?post=9849"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}