{"id":13747,"date":"2023-12-29T00:47:09","date_gmt":"2023-12-29T07:47:09","guid":{"rendered":"https:\/\/www.mysqltutorial.org\/?page_id=13747"},"modified":"2023-12-29T00:49:41","modified_gmt":"2023-12-29T07:49:41","slug":"mysql-union","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-union\/","title":{"rendered":"MySQL UNION"},"content":{"rendered":"\n<p><strong>Summary<\/strong><em>: <\/em>in this tutorial, you will learn how to use MySQL <code>UNION<\/code> operator to combine two or more result sets from multiple <code>SELECT<\/code> statements into a single result set.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL UNION&nbsp;operator<\/h2>\n\n\n\n<p>MySQL <code>UNION<\/code> operator allows you to combine two or more result sets of queries into a single result set. The following illustrates the syntax of the <code>UNION<\/code> operator:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> column_list\n<span class=\"hljs-keyword\">UNION<\/span> &#91;<span class=\"hljs-keyword\">DISTINCT<\/span> | <span class=\"hljs-keyword\">ALL<\/span>]\n<span class=\"hljs-keyword\">SELECT<\/span> column_list\n<span class=\"hljs-keyword\">UNION<\/span> &#91;<span class=\"hljs-keyword\">DISTINCT<\/span> | <span class=\"hljs-keyword\">ALL<\/span>]\n<span class=\"hljs-keyword\">SELECT<\/span> column_list\n...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To combine result set of two or more queries using the <code>UNION<\/code> operator, these are the basic rules that you must follow:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, the number and the orders of columns that appear&nbsp;in all&nbsp;<code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-select-from\/\">SELECT<\/a><\/code> statements must be the same.<\/li><li>Second, the <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-data-types\/\">data types<\/a> of columns must be the same or compatible.<\/li><\/ul>\n\n\n\n<p>By default, the <code>UNION<\/code> operator removes&nbsp;<a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-find-duplicate-values\/\">duplicate rows<\/a>&nbsp;even if you don&#8217;t specify&nbsp;the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-distinct\/\">DISTINCT<\/a><\/code> operator explicitly.<\/p>\n\n\n\n<p>Let&#8217;s see the following sample tables: <code>t1<\/code> and <code>t2<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-keyword\">IF<\/span> <span class=\"hljs-keyword\">EXISTS<\/span> t1;\n<span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-keyword\">IF<\/span> <span class=\"hljs-keyword\">EXISTS<\/span> t2;\n\n<span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> t1 (\n    <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">INT<\/span> PRIMARY <span class=\"hljs-keyword\">KEY<\/span>\n);\n\n<span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> t2 (\n    <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">INT<\/span> PRIMARY <span class=\"hljs-keyword\">KEY<\/span>\n);\n\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> t1 <span class=\"hljs-keyword\">VALUES<\/span> (<span class=\"hljs-number\">1<\/span>),(<span class=\"hljs-number\">2<\/span>),(<span class=\"hljs-number\">3<\/span>);\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> t2 <span class=\"hljs-keyword\">VALUES<\/span> (<span class=\"hljs-number\">2<\/span>),(<span class=\"hljs-number\">3<\/span>),(<span class=\"hljs-number\">4<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following statement combines result sets returned from <code>t1<\/code> and <code>t2<\/code> tables:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">id<\/span>\n<span class=\"hljs-keyword\">FROM<\/span> t1\n<span class=\"hljs-keyword\">UNION<\/span>\n<span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">id<\/span>\n<span class=\"hljs-keyword\">FROM<\/span> t2;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The final result set contains the distinct values from separate result sets returned by the queries:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">+<span class=\"hljs-comment\">----+<\/span>\n| id |\n+<span class=\"hljs-comment\">----+<\/span>\n|  1 |\n|  2 |\n|  3 |\n|  4 |\n+<span class=\"hljs-comment\">----+<\/span>\n4 rows in <span class=\"hljs-keyword\">set<\/span> (<span class=\"hljs-number\">0.00<\/span> sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Because the rows with value 2 and 3 are duplicates, the <code>UNION<\/code> removed them and kept only unique values.<\/p>\n\n\n\n<p>The following Venn diagram illustrates the union of two result sets that come from <code>t1<\/code> and <code>t2<\/code> tables:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"409\" height=\"275\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/MySQL-UNION.png\" alt=\"MySQL UNION\" class=\"wp-image-6372\" title=\"MySQL UNION\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/MySQL-UNION.png 409w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/MySQL-UNION-300x202.png 300w\" sizes=\"auto, (max-width: 409px) 100vw, 409px\" \/><\/figure><\/div>\n\n\n\n<p>If you use the <code> UNION ALL<\/code> explicitly, the duplicate rows, if available, remain in the result. Because <code>UNION ALL<\/code> does not need to handle duplicates, it performs faster than&nbsp;<code>UNION DISTINCT<\/code>&nbsp;.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">id<\/span>\n<span class=\"hljs-keyword\">FROM<\/span> t1\n<span class=\"hljs-keyword\">UNION<\/span> <span class=\"hljs-keyword\">ALL<\/span>\n<span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">id<\/span>\n<span class=\"hljs-keyword\">FROM<\/span> t2;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">+<span class=\"hljs-comment\">----+<\/span>\n| id |\n+<span class=\"hljs-comment\">----+<\/span>\n|  1 |\n|  2 |\n|  3 |\n|  2 |\n|  3 |\n|  4 |\n+<span class=\"hljs-comment\">----+<\/span>\n6 rows in <span class=\"hljs-keyword\">set<\/span> (<span class=\"hljs-number\">0.00<\/span> sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>As you can see, the duplicates appear in the combined result set because of the&nbsp;<code>UNION ALL<\/code> operation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">&nbsp;UNION vs. JOIN<\/h2>\n\n\n\n<p>A&nbsp;<code>JOIN<\/code> combines result sets horizontally, a&nbsp;<code>UNION<\/code> appends result set vertically. The following picture illustrates the difference between <code>UNION<\/code> and <code>JOIN<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"632\" height=\"291\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/MySQL-UNION-vs-JOIN.png\" alt=\"MySQL UNION vs JOIN\" class=\"wp-image-6373\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/MySQL-UNION-vs-JOIN.png 632w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/MySQL-UNION-vs-JOIN-300x138.png 300w\" sizes=\"auto, (max-width: 632px) 100vw, 632px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL UNION and column alias examples<\/h2>\n\n\n\n<p>We&#8217;ll use the <code>customers<\/code> and <code>employees<\/code> tables in the <a href=\"https:\/\/www.mysqltutorial.org\/getting-started-with-mysql\/mysql-sample-database\/\">sample database<\/a>&nbsp;for the demonstration:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"433\" height=\"304\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/customers-employees.png\" alt=\"\" class=\"wp-image-8371\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/customers-employees.png 433w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/customers-employees-200x140.png 200w\" sizes=\"auto, (max-width: 433px) 100vw, 433px\" \/><\/figure>\n\n\n\n<p>Suppose that you want to combine the first name and last name of employees and customers into a single result set, you can use the <code>UNION<\/code> operator as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    firstName, \n    lastName\n<span class=\"hljs-keyword\">FROM<\/span>\n    employees \n<span class=\"hljs-keyword\">UNION<\/span> \n<span class=\"hljs-keyword\">SELECT<\/span> \n    contactFirstName, \n    contactLastName\n<span class=\"hljs-keyword\">FROM<\/span>\n    customers;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"151\" height=\"179\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/MySQL-UNION-example.png\" alt=\"MySQL UNION example\" class=\"wp-image-6374\" title=\"MySQL UNION example\"\/><\/figure>\n\n\n\n<p>As you can see from the output, the MySQL <code>UNION<\/code> uses the column names of the first <code>SELECT<\/code> statement for the column headings of the output.<\/p>\n\n\n\n<p>If you want to use other column headings, you need to use <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-alias\/\">column aliases<\/a> explicitly in the first <code>SELECT<\/code> statement as shown in the following example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">CONCAT<\/span>(firstName,<span class=\"hljs-string\">' '<\/span>,lastName) fullname\n<span class=\"hljs-keyword\">FROM<\/span>\n    employees \n<span class=\"hljs-keyword\">UNION<\/span> <span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">CONCAT<\/span>(contactFirstName,<span class=\"hljs-string\">' '<\/span>,contactLastName)\n<span class=\"hljs-keyword\">FROM<\/span>\n    customers;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6375\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/MySQL-UNION-with-column-alias-example.png\" alt=\"MySQL UNION with column alias example\" width=\"118\" height=\"215\"><\/h2>\n\n\n\n<p>This example uses the column heading of the first query for the output. It uses the <code>CONCAT()<\/code> function to concatenate first name, space, and last name into a full name.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL UNION and ORDER BY<\/h2>\n\n\n\n<p>If you want to sort the result set of a union, you use an <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-order-by\/\"><code>ORDER BY<\/code><\/a> clause in the last&nbsp;<code>SELECT<\/code> statement as shown in the following example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">concat<\/span>(firstName,<span class=\"hljs-string\">' '<\/span>,lastName) fullname\n<span class=\"hljs-keyword\">FROM<\/span>\n    employees \n<span class=\"hljs-keyword\">UNION<\/span> <span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">concat<\/span>(contactFirstName,<span class=\"hljs-string\">' '<\/span>,contactLastName)\n<span class=\"hljs-keyword\">FROM<\/span>\n    customers\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> fullname;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"137\" height=\"214\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/MySQL-UNION-and-ORDER-BY-example.png\" alt=\"MySQL UNION and ORDER BY example\" class=\"wp-image-6376\" title=\"MySQL UNION and ORDER BY example\"\/><\/figure>\n\n\n\n<p>Notice that if you place the <code>ORDER BY<\/code> clause in each <code>SELECT<\/code> statement, it will not affect the order of the rows in the final result set.<\/p>\n\n\n\n<p>To differentiate between employees and customers, you can add a column as shown in the following query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">CONCAT<\/span>(firstName, <span class=\"hljs-string\">' '<\/span>, lastName) fullname, \n    <span class=\"hljs-string\">'Employee'<\/span> <span class=\"hljs-keyword\">as<\/span> contactType\n<span class=\"hljs-keyword\">FROM<\/span>\n    employees \n<span class=\"hljs-keyword\">UNION<\/span> <span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">CONCAT<\/span>(contactFirstName, <span class=\"hljs-string\">' '<\/span>, contactLastName),\n    <span class=\"hljs-string\">'Customer'<\/span> <span class=\"hljs-keyword\">as<\/span> contactType\n<span class=\"hljs-keyword\">FROM<\/span>\n    customers\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> \n    fullname<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"223\" height=\"299\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MysQL-Union-with-a-custom-column.png\" alt=\"MysQL Union with a custom column\" class=\"wp-image-8372\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MysQL-Union-with-a-custom-column.png 223w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MysQL-Union-with-a-custom-column-149x200.png 149w\" sizes=\"auto, (max-width: 223px) 100vw, 223px\" \/><\/figure>\n\n\n\n<p>MySQL also provides you with an alternative option to sort a result set based on column position using <code>ORDER BY<\/code> clause as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">CONCAT<\/span>(firstName,<span class=\"hljs-string\">' '<\/span>,lastName) fullname\n<span class=\"hljs-keyword\">FROM<\/span>\n    employees \n<span class=\"hljs-keyword\">UNION<\/span> <span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">CONCAT<\/span>(contactFirstName,<span class=\"hljs-string\">' '<\/span>,contactLastName)\n<span class=\"hljs-keyword\">FROM<\/span>\n    customers\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> <span class=\"hljs-number\">1<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>However, it is not a good practice to sort the result set by column position.<\/p>\n\n\n\n<p>In this tutorial, you have learned how to use MySQL <code>UNION<\/code> statement to combine data from multiple queries into a single result set.<\/p>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful? <\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"13747\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-union\/\"\n\t\t\t\tdata-post-title=\"MySQL UNION\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"13747\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-union\/\"\n\t\t\t\tdata-post-title=\"MySQL UNION\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you will learn how to use MySQL UNION operator to combine two or more result sets from multiple SELECT statements into a single result set. MySQL UNION&nbsp;operator MySQL UNION operator allows you to combine two or more result sets of queries into a single result set. The following illustrates the syntax [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":174,"menu_order":27,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-13747","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MySQL UNION<\/title>\n<meta name=\"description\" content=\"This tutorial shows you how to use the MySQL UNION operator to combine results of two or more queries into a single result set.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-union\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL UNION\" \/>\n<meta property=\"og:description\" content=\"This tutorial shows you how to use the MySQL UNION operator to combine results of two or more queries into a single result set.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-union\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-29T07:49:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/MySQL-UNION.png\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-union\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-union\\\/\",\"name\":\"MySQL UNION\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-union\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-union\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2009\\\/12\\\/MySQL-UNION.png\",\"datePublished\":\"2023-12-29T07:47:09+00:00\",\"dateModified\":\"2023-12-29T07:49:41+00:00\",\"description\":\"This tutorial shows you how to use the MySQL UNION operator to combine results of two or more queries into a single result set.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-union\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-union\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-union\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2009\\\/12\\\/MySQL-UNION.png\",\"contentUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2009\\\/12\\\/MySQL-UNION.png\",\"width\":409,\"height\":275,\"caption\":\"MySQL UNION\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-union\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Basics\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MySQL UNION\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.org\\\/?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":"MySQL UNION","description":"This tutorial shows you how to use the MySQL UNION operator to combine results of two or more queries into a single result set.","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:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-union\/","og_locale":"en_US","og_type":"article","og_title":"MySQL UNION","og_description":"This tutorial shows you how to use the MySQL UNION operator to combine results of two or more queries into a single result set.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-union\/","og_site_name":"MySQL Tutorial","article_modified_time":"2023-12-29T07:49:41+00:00","og_image":[{"url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/MySQL-UNION.png","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-union\/","url":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-union\/","name":"MySQL UNION","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-union\/#primaryimage"},"image":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-union\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/MySQL-UNION.png","datePublished":"2023-12-29T07:47:09+00:00","dateModified":"2023-12-29T07:49:41+00:00","description":"This tutorial shows you how to use the MySQL UNION operator to combine results of two or more queries into a single result set.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-union\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-union\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-union\/#primaryimage","url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/MySQL-UNION.png","contentUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/MySQL-UNION.png","width":409,"height":275,"caption":"MySQL UNION"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-union\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"MySQL Basics","item":"https:\/\/www.mysqltutorial.org\/mysql-basics\/"},{"@type":"ListItem","position":3,"name":"MySQL UNION"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/13747","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=13747"}],"version-history":[{"count":2,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/13747\/revisions"}],"predecessor-version":[{"id":13749,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/13747\/revisions\/13749"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/174"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=13747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}