{"id":3953,"date":"2025-06-07T08:11:36","date_gmt":"2025-06-07T08:11:36","guid":{"rendered":"https:\/\/w3buddy.com\/?post_type=cposts&#038;p=3953"},"modified":"2025-06-07T09:58:57","modified_gmt":"2025-06-07T09:58:57","slug":"sql-full-join","status":"publish","type":"cposts","link":"https:\/\/w3buddy.com\/blog\/notes\/sql\/sql-full-join\/","title":{"rendered":"SQL FULL JOIN"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><code>FULL JOIN<\/code> (or <code>FULL OUTER JOIN<\/code>) returns <strong>all rows from both tables<\/strong>. If there\u2019s no match, unmatched columns return <code>NULL<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 Combines the effects of <code>LEFT JOIN<\/code> and <code>RIGHT JOIN<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Basic Syntax<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>SELECT columns\nFROM table1\nFULL JOIN table2\n  ON table1.common_column = table2.common_column;<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u261d\ufe0f Some DBMS (like MySQL) don\u2019t support <code>FULL JOIN<\/code> directly \u2014 use a workaround with <code>UNION<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Example: List All Employees and All Departments (Matched or Not)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>SELECT\n  e.employee_name,\n  d.department_name\nFROM employees e\nFULL JOIN departments d\n  ON e.department_id = d.department_id;<\/code><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If an employee has no department \u2192 <code>department_name<\/code> is <code>NULL<\/code><\/li>\n\n\n\n<li>If a department has no employee \u2192 <code>employee_name<\/code> is <code>NULL<\/code><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 MySQL Workaround for FULL JOIN<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>SELECT e.employee_name, d.department_name\nFROM employees e\nLEFT JOIN departments d ON e.department_id = d.department_id\n\nUNION\n\nSELECT e.employee_name, d.department_name\nFROM employees e\nRIGHT JOIN departments d ON e.department_id = d.department_id;<\/code><\/code><\/pre>\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>FULL JOIN<\/code><\/td><td>Returns all rows from both tables<\/td><\/tr><tr><td>No match?<\/td><td>Missing side columns are filled with <code>NULL<\/code><\/td><\/tr><tr><td>Use case<\/td><td>Useful when you want to show all records, matched or not<\/td><\/tr><tr><td>MySQL note<\/td><td>Use <code>LEFT JOIN<\/code> + <code>RIGHT JOIN<\/code> with <code>UNION<\/code> workaround<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83e\udde9 Use <code>FULL JOIN<\/code> to see the <strong>complete picture from both tables<\/strong><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>FULL JOIN (or FULL OUTER JOIN) returns all rows from both tables. If there\u2019s no match, unmatched columns return NULL. \u2705 Combines the effects of LEFT JOIN and RIGHT JOIN. \ud83d\udd39 Basic Syntax \u261d\ufe0f Some DBMS (like MySQL) don\u2019t support FULL JOIN directly \u2014 use a workaround with UNION. \ud83d\udd39 Example: List All Employees and [&hellip;]<\/p>\n","protected":false},"template":"","meta":{"googlesitekit_rrm_CAowu461DA:productID":""},"categories":[952,979],"class_list":["post-3953","cposts","type-cposts","status-publish","hentry","category-notes","category-sql"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts\/3953","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=3953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=3953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}