{"id":3959,"date":"2025-06-07T08:17:56","date_gmt":"2025-06-07T08:17:56","guid":{"rendered":"https:\/\/w3buddy.com\/?post_type=cposts&#038;p=3959"},"modified":"2025-06-07T09:59:35","modified_gmt":"2025-06-07T09:59:35","slug":"sql-union-union-all","status":"publish","type":"cposts","link":"https:\/\/w3buddy.com\/blog\/notes\/sql\/sql-union-union-all\/","title":{"rendered":"SQL UNION \/ UNION ALL"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><code>UNION<\/code> and <code>UNION ALL<\/code> combine results from <strong>two or more SELECT queries<\/strong>.<br>Both must have the <strong>same number of columns<\/strong> with compatible data types.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Syntax<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>-- Removes duplicates\nSELECT column1, column2 FROM table1\nUNION\nSELECT column1, column2 FROM table2;\n\n-- Keeps duplicates\nSELECT column1, column2 FROM table1\nUNION ALL\nSELECT column1, column2 FROM table2;<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Example: Merge Customers from Two Regions<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>-- Unique customers only\nSELECT customer_name FROM customers_us\nUNION\nSELECT customer_name FROM customers_uk;\n\n-- Include duplicates\nSELECT customer_name FROM customers_us\nUNION ALL\nSELECT customer_name FROM customers_uk;<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Key Differences<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Feature<\/th><th><code>UNION<\/code><\/th><th><code>UNION ALL<\/code><\/th><\/tr><\/thead><tbody><tr><td>Duplicates<\/td><td>Removed<\/td><td>Included<\/td><\/tr><tr><td>Performance<\/td><td>Slower (due to deduplication)<\/td><td>Faster<\/td><\/tr><tr><td>Use When<\/td><td>You want distinct results<\/td><td>You need <strong>all<\/strong> results<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde0 Quick Recap<\/h2>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>Key Point<\/th><th>Explanation<\/th><\/tr><\/thead><tbody><tr><td><code>UNION<\/code><\/td><td>Combines results &amp; removes duplicates<\/td><\/tr><tr><td><code>UNION ALL<\/code><\/td><td>Combines results <strong>with<\/strong> duplicates<\/td><\/tr><tr><td>Column match<\/td><td>Same number and types of columns required<\/td><\/tr><tr><td>Use cases<\/td><td>Merge similar datasets (e.g., logs, users)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83e\udde9 Use <code>UNION<\/code> for clean merged data<br>\ud83e\udde9 Use <code>UNION ALL<\/code> for <strong>full<\/strong> raw results<\/p>\n","protected":false},"excerpt":{"rendered":"<p>UNION and UNION ALL combine results from two or more SELECT queries.Both must have the same number of columns with compatible data types. \ud83d\udd39 Syntax \ud83d\udd39 Example: Merge Customers from Two Regions \ud83d\udd39 Key Differences Feature UNION UNION ALL Duplicates Removed Included Performance Slower (due to deduplication) Faster Use When You want distinct results You [&hellip;]<\/p>\n","protected":false},"template":"","meta":{"googlesitekit_rrm_CAowu461DA:productID":""},"categories":[952,979],"class_list":["post-3959","cposts","type-cposts","status-publish","hentry","category-notes","category-sql"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts\/3959","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts"}],"about":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/types\/cposts"}],"wp:attachment":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/media?parent=3959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=3959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}