{"id":332,"date":"2024-11-26T09:15:21","date_gmt":"2024-11-26T02:15:21","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=332"},"modified":"2025-02-10T14:45:23","modified_gmt":"2025-02-10T07:45:23","slug":"postgresql-full-join","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-full-join\/","title":{"rendered":"PostgreSQL Full Join"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use PostgreSQL <code>FULL JOIN<\/code> to merge rows from two tables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-postgresql-full-join-statement'>Introduction to PostgreSQL FULL JOIN statement <a href=\"#introduction-to-postgresql-full-join-statement\" class=\"anchor\" id=\"introduction-to-postgresql-full-join-statement\" title=\"Anchor for Introduction to PostgreSQL FULL JOIN statement\">#<\/a><\/h2>\n\n\n\n<p>The <code>FULL JOIN<\/code> merges rows from two tables and returns all rows from both tables. Additionally, the <code>FULL JOIN<\/code> uses NULLs for every column of the table that does not have a matching row.<\/p>\n\n\n\n<p>In other words, the <code>FULL JOIN<\/code> combines the result sets of the <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-left-join\/\">LEFT JOIN<\/a> and <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-right-join\/\">RIGHT JOIN<\/a>.<\/p>\n\n\n\n<p>Here&#8217;s the syntax of the <code>FULL JOIN<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  table1.column1,\n  table2.column2,\n  ...\n<span class=\"hljs-keyword\">FROM<\/span>\n  table1\n  <span class=\"hljs-keyword\">FULL<\/span> <span class=\"hljs-keyword\">JOIN<\/span> table2 <span class=\"hljs-keyword\">ON<\/span> table2.column1 = table1.column1;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, specify the name of the first table (<code>table1<\/code>) in the&nbsp;<code>FROM<\/code>&nbsp;clause.<\/li>\n\n\n\n<li>Second, provide the name of the second table (<code>table2<\/code>) you want to perform a full join in the&nbsp;<code>FULL JOIN<\/code>&nbsp;clause.<\/li>\n\n\n\n<li>Third, use a condition to match rows from both tables in the&nbsp;<code>ON<\/code>&nbsp;clause. The condition matches rows by comparing the values of&nbsp;<code>column1<\/code>&nbsp;in&nbsp;<code>table1<\/code>&nbsp;with the values of&nbsp;<code>column1<\/code>&nbsp;in&nbsp;<code>table2<\/code>.<\/li>\n\n\n\n<li>Finally, specify the columns you want to retrieve data in the&nbsp;<code>SELECT<\/code>&nbsp;clause.<\/li>\n<\/ul>\n\n\n\n<p>The <code>FULL OUTER JOIN<\/code> is an alternative syntax of the <code>FULL JOIN<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  table1.column1,\n  table2.column2,\n  ...\n<span class=\"hljs-keyword\">FROM<\/span>\n  table1\n  <span class=\"hljs-keyword\">FULL<\/span> <span class=\"hljs-keyword\">OUTER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> table2 <span class=\"hljs-keyword\">ON<\/span> table2.column1 = table1.column1;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"note\">Note that the <code>OUTER<\/code>&nbsp;keyword is optional.<\/p>\n\n\n\n<p>How the PostgreSQL <code>FULL JOIN<\/code> works:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, the <code>FROM<\/code> clause returns all rows from the <code>table1<\/code>.<\/li>\n\n\n\n<li>Second, the <code>FULL JOIN<\/code> matches each row in the <code>table1<\/code> with every row in the <code>table2<\/code> by comparing values from <code>column1<\/code> in both tables. If they match, the <code>FULL JOIN<\/code> merges two rows into a single row. Otherwise, it fills <code>NULLs<\/code> for the columns of the row from table that has no match and merges with the row from the other table. <\/li>\n\n\n\n<li>Third, return a result set that includes columns specified in the <code>SELECT<\/code> clause.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"block-f343c05b-7d74-40bc-b6c6-98a072af211f\" id='understanding-postgresql-full-join'>Understanding PostgreSQL Full Join <a href=\"#understanding-postgresql-full-join\" class=\"anchor\" id=\"understanding-postgresql-full-join\" title=\"Anchor for Understanding PostgreSQL Full Join\">#<\/a><\/h2>\n\n\n\n<p id=\"block-dfd1d496-be50-4757-b8ed-fa2c8628f6b7\">Suppose you want to merge rows from <code>X<\/code> and <code>Y<\/code> tables using a full join:<\/p>\n\n\n\n<ul id=\"block-5d11c230-9f3a-44f1-ac08-4a595ae41e30\" class=\"wp-block-list\">\n<li>The <code>X<\/code> table has two columns: <code>id <\/code>(key) and <code>x<\/code>.<\/li>\n\n\n\n<li>The <code>Y<\/code> table has also has two columns: <code>id<\/code> (key) and <code>y<\/code>.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><div class=\"wp-block-image\">\n<figure class=\"alignright size-full\"><img decoding=\"async\" src=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2025\/02\/x-table.svg\" alt=\"x table\" class=\"wp-image-1951\"\/><\/figure>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><div class=\"wp-block-image\">\n<figure class=\"alignleft size-full\"><img decoding=\"async\" src=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2025\/02\/y-table.svg\" alt=\"\" class=\"wp-image-1952\"\/><\/figure>\n<\/div><\/div>\n<\/div>\n\n\n\n<p id=\"block-fe6dd99e-b835-43b4-8bec-eb7adab8e146\">The full outer join returns a result set that includes rows from both tables whether or not the rows have matching rows from another table. It fills\u00a0<code>NULLs<\/code>\u00a0for columns of rows in the table that do not have matching rows in another table:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2025\/02\/postgresql-full-join-visualization.svg\" alt=\"PostgreSQL full join visualization\" class=\"wp-image-1966\"\/><\/figure>\n<\/div>\n\n\n<p id=\"block-09019613-ecf7-454b-9292-8c5df333a098\">The following Venn diagram is another way to depict how a full join works:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2025\/02\/postgresql-full-outer-join-Venn-diagram.svg\" alt=\"PostgreSQL full outer join Venn diagram\" class=\"wp-image-1967\"\/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id='setting-up-sample-tables'>Setting up sample tables <a href=\"#setting-up-sample-tables\" class=\"anchor\" id=\"setting-up-sample-tables\" title=\"Anchor for Setting up sample tables\">#<\/a><\/h2>\n\n\n\n<p>First, <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-create-table\/\">create a new table<\/a> called <code>warehouses<\/code> with some sample data:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> brands (\n  brand_id <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">GENERATED<\/span> <span class=\"hljs-keyword\">ALWAYS<\/span> <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span> <span class=\"hljs-keyword\">PRIMARY KEY<\/span>,\n  <span class=\"hljs-type\">name<\/span> <span class=\"hljs-type\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-insert\/\">insert three rows<\/a> into the <code>brands<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span>\n  brands (<span class=\"hljs-type\">name<\/span>)\n<span class=\"hljs-keyword\">VALUES<\/span>\n  (<span class=\"hljs-string\">'Apple'<\/span>),\n  (<span class=\"hljs-string\">'Samsung'<\/span>),\n  (<span class=\"hljs-string\">'Google'<\/span>) <span class=\"hljs-keyword\">RETURNING<\/span> *;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\"> brand_id |  name\n----------+---------\n        1 | Apple\n        2 | Samsung\n        3 | Google<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-create-table\/\">create a new table<\/a> called <code>products<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> products (\n  product_id <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">GENERATED<\/span> <span class=\"hljs-keyword\">ALWAYS<\/span> <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span> <span class=\"hljs-keyword\">PRIMARY KEY<\/span>,\n  <span class=\"hljs-type\">name<\/span> <span class=\"hljs-type\">VARCHAR<\/span>(<span class=\"hljs-number\">100<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n  price <span class=\"hljs-type\">DECIMAL<\/span>(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">2<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n  brand_id <span class=\"hljs-type\">INT<\/span>,\n  <span class=\"hljs-keyword\">FOREIGN KEY<\/span> (brand_id) <span class=\"hljs-keyword\">REFERENCES<\/span> brands (brand_id)\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, insert four rows into <code>products<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span>\n  products (<span class=\"hljs-type\">name<\/span>, price, brand_id)\n<span class=\"hljs-keyword\">VALUES<\/span>\n  (<span class=\"hljs-string\">'iPhone 14 Pro'<\/span>, <span class=\"hljs-number\">999.99<\/span>, <span class=\"hljs-number\">1<\/span>),\n  (<span class=\"hljs-string\">'iPhone 15 Pro'<\/span>, <span class=\"hljs-number\">1199.99<\/span>, <span class=\"hljs-number\">1<\/span>),\n  (<span class=\"hljs-string\">'Galaxy S23 Ultra'<\/span>, <span class=\"hljs-number\">1149.47<\/span>, <span class=\"hljs-number\">2<\/span>),\n  (<span class=\"hljs-string\">'Oppo Find Flip'<\/span>, <span class=\"hljs-number\">499.99<\/span>, <span class=\"hljs-keyword\">NULL<\/span>) \n<span class=\"hljs-keyword\">RETURNING<\/span> *;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\"> product_id |       name       |  price  | brand_id\n------------+------------------+---------+----------\n          1 | iPhone 14 Pro    |  999.99 |        1\n          2 | iPhone 15 Pro    | 1199.99 |        1\n          3 | Galaxy S23 Ultra | 1149.47 |        2\n          4 | Oppo Find Flip   |  499.99 |     NULL<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='postgresql-full-join-example'>PostgreSQL FULL JOIN example <a href=\"#postgresql-full-join-example\" class=\"anchor\" id=\"postgresql-full-join-example\" title=\"Anchor for PostgreSQL FULL JOIN example\">#<\/a><\/h2>\n\n\n\n<p>The following statement uses the <code>FULL JOIN<\/code> to merge rows from the <code>products<\/code> and <code>brands<\/code> tables:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  p.name <span class=\"hljs-keyword\">as<\/span> product_name,\n  p.price,\n  b.name brand_name\n<span class=\"hljs-keyword\">FROM<\/span>\n  products p\n  <span class=\"hljs-keyword\">FULL<\/span> <span class=\"hljs-keyword\">JOIN<\/span> brands b <span class=\"hljs-keyword\">ON<\/span> b.brand_id = p.brand_id;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=join&amp;q=U0VMRUNUIHAubmFtZSBhcyBwcm9kdWN0X25hbWUsIHAucHJpY2UsIGIubmFtZSBicmFuZF9uYW1lIEZST00gcHJvZHVjdHMgcCBGVUxMIEpPSU4gYnJhbmRzIGIgT04gYi5icmFuZF9pZCA9IHAuYnJhbmRfaWQ7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">   product_name   |  price  | brand_name\n------------------+---------+------------\n iPhone 14 Pro    |  999.99 | Apple\n iPhone 15 Pro    | 1199.99 | Apple\n Galaxy S23 Ultra | 1149.47 | Samsung\n Oppo Find Flip   |  499.99 | NULL\n NULL             |    NULL | Google<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=join&amp;q=U0VMRUNUIHAubmFtZSBhcyBwcm9kdWN0X25hbWUsIHAucHJpY2UsIGIubmFtZSBicmFuZF9uYW1lIEZST00gcHJvZHVjdHMgcCBGVUxMIEpPSU4gYnJhbmRzIGIgT04gYi5icmFuZF9pZCA9IHAuYnJhbmRfaWQ7\">Try it<\/a><\/p>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, the <code>FROM<\/code> clause returns all rows from the <code>products<\/code> table.<\/p>\n\n\n\n<p>Second, the <code>FULL JOIN<\/code> clause matches each row from the <code>products<\/code> table with every row from the <code>brands<\/code> table by comparing the values in the <code>brand_id<\/code> column.<\/p>\n\n\n\n<p>If there is a match, the <code>FULL JOIN<\/code> merges the rows from both tables into a single row.<\/p>\n\n\n\n<p>If there is no match, the <code>FULL JOIN<\/code> fills every column of the row in the table that has no match with NULLs and merges with the row from the other table.<\/p>\n\n\n\n<p>The row with <code>product_id<\/code> 1 from the <code>products<\/code> table matches the row with <code>brand_id<\/code> 1 from the <code>brands<\/code> table:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>product_id<\/th><th>name<\/th><th>price<\/th><th>brand_id<\/th><th>brand_id<\/th><th>name<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>iPhone 14 Pro<\/td><td>999.99<\/td><td>1<\/td><td>1<\/td><td>Apple<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The row with <code>product_id<\/code> 2 from the <code>products<\/code> table matches the row with <code>brand_id<\/code> 1 from the <code>brands<\/code> table:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>product_id<\/th><th>name<\/th><th>price<\/th><th>brand_id<\/th><th>brand_id<\/th><th>name<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>iPhone 14 Pro<\/td><td>999.99<\/td><td>1<\/td><td>1<\/td><td>Apple<\/td><\/tr><tr><td>2<\/td><td>iPhone 15 Pro<\/td><td>1299.99<\/td><td>1<\/td><td>1<\/td><td>Apple<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The row with <code>product_id<\/code> 3 from the <code>products<\/code> table matches the row with <code>brand_id<\/code> 2 from the <code>brands<\/code> table:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>product_id<\/th><th>name<\/th><th>price<\/th><th>brand_id<\/th><th>brand_id<\/th><th>name<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>iPhone 14 Pro<\/td><td>999.99<\/td><td>1<\/td><td>1<\/td><td>Apple<\/td><\/tr><tr><td>2<\/td><td>iPhone 15 Pro<\/td><td>1299.99<\/td><td>1<\/td><td>1<\/td><td>Apple<\/td><\/tr><tr><td>3<\/td><td>Galaxy S23 Ultra<\/td><td>1149.47<\/td><td>2<\/td><td>2<\/td><td>Samsung<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The row with <code>product_id<\/code> 4 from the <code>products<\/code> table does not have any matching rows in the <code>brands<\/code> table, the <code>FULL JOIN<\/code> fills the <code>brand_id<\/code> and <code>name<\/code> columns from the <code>brands<\/code> table with <code>NULL<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>product_id<\/th><th>name<\/th><th>price<\/th><th>brand_id<\/th><th>brand_id<\/th><th>name<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>iPhone 14 Pro<\/td><td>999.99<\/td><td>1<\/td><td>1<\/td><td>Apple<\/td><\/tr><tr><td>2<\/td><td>iPhone 15 Pro<\/td><td>1299.99<\/td><td>1<\/td><td>1<\/td><td>Apple<\/td><\/tr><tr><td>3<\/td><td>Galaxy S23 Ultra<\/td><td>1149.47<\/td><td>2<\/td><td>2<\/td><td>Samsung<\/td><\/tr><tr><td>4<\/td><td>Oppo Find Flip<\/td><td>499.99<\/td><td>NULL<\/td><td>NULL<\/td><td>NULL<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The row with <code>brand_id<\/code> 3 from the <code>brands<\/code> table does not have any matching rows in the <code>products<\/code> table,  the <code>FULL JOIN<\/code> fills the columns of the row from  the <code>products<\/code> table with <code>NULL<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>product_id<\/th><th>name<\/th><th>price<\/th><th>brand_id<\/th><th>brand_id<\/th><th>name<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>iPhone 14 Pro<\/td><td>999.99<\/td><td>1<\/td><td>1<\/td><td>Apple<\/td><\/tr><tr><td>2<\/td><td>iPhone 15 Pro<\/td><td>1299.99<\/td><td>1<\/td><td>1<\/td><td>Apple<\/td><\/tr><tr><td>3<\/td><td>Galaxy S23 Ultra<\/td><td>1149.47<\/td><td>2<\/td><td>2<\/td><td>Samsung<\/td><\/tr><tr><td>4<\/td><td>Oppo Find Flip<\/td><td>499.99<\/td><td>NULL<\/td><td>NULL<\/td><td>NULL<\/td><\/tr><tr><td>NULL<\/td><td>NULL<\/td><td>NULL<\/td><td>NULL<\/td><td>3<\/td><td>Google<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Third, return the columns specified in the <code>SELECT<\/code> clause, also assign the column alias to these columns:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>product_name<\/th><th>price<\/th><th>brand_name<\/th><\/tr><\/thead><tbody><tr><td>iPhone 14 Pro<\/td><td>999.99<\/td><td>Apple<\/td><\/tr><tr><td>iPhone 15 Pro<\/td><td>1299.99<\/td><td>Apple<\/td><\/tr><tr><td>Galaxy S23 Ultra<\/td><td>1149.47<\/td><td>Samsung<\/td><\/tr><tr><td>Oppo Find Flip<\/td><td>499.99<\/td><td>NULL<\/td><\/tr><tr><td>NULL<\/td><td>NULL<\/td><td>Google<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use PostgreSQL <code>FULL JOIN<\/code> to merge rows from two tables and return rows from both tables including matching and non-matching rows.<\/li>\n\n\n\n<li><code>FULL OUTER JOIN<\/code> is an alternative syntax of the <code>FULL JOIN<\/code>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=full-join\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\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=\"332\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-full-join\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL Full Join\"\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=\"332\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-full-join\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL Full Join\"\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 PostgreSQL FULL JOIN to merge rows from two tables. Introduction to PostgreSQL FULL JOIN statement # The FULL JOIN merges rows from two tables and returns all rows from both tables. Additionally, the FULL JOIN uses NULLs for every column of the table that does [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":13,"menu_order":25,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-332","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>PostgreSQL FULL JOIN<\/title>\n<meta name=\"description\" content=\"Use PostgreSQL FULL JOIN to merge rows from two tables and return rows from both tables including matching and non-matching rows.\" \/>\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.pgtutorial.com\/postgresql-tutorial\/postgresql-full-join\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL FULL JOIN\" \/>\n<meta property=\"og:description\" content=\"Use PostgreSQL FULL JOIN to merge rows from two tables and return rows from both tables including matching and non-matching rows.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-full-join\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-10T07:45:23+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-full-join\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-full-join\\\/\",\"name\":\"PostgreSQL FULL JOIN\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-full-join\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-full-join\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2025\\\/02\\\/x-table.svg\",\"datePublished\":\"2024-11-26T02:15:21+00:00\",\"dateModified\":\"2025-02-10T07:45:23+00:00\",\"description\":\"Use PostgreSQL FULL JOIN to merge rows from two tables and return rows from both tables including matching and non-matching rows.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-full-join\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-full-join\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-full-join\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2025\\\/02\\\/x-table.svg\",\"contentUrl\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2025\\\/02\\\/x-table.svg\",\"caption\":\"x table\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-full-join\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL Tutorial\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PostgreSQL Full Join\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/\",\"name\":\"PostgreSQL Tutorial\",\"description\":\"Learn PostgreSQL from Scratch\",\"alternateName\":\"PostgreSQL\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pgtutorial.com\\\/?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":"PostgreSQL FULL JOIN","description":"Use PostgreSQL FULL JOIN to merge rows from two tables and return rows from both tables including matching and non-matching rows.","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.pgtutorial.com\/postgresql-tutorial\/postgresql-full-join\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL FULL JOIN","og_description":"Use PostgreSQL FULL JOIN to merge rows from two tables and return rows from both tables including matching and non-matching rows.","og_url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-full-join\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-02-10T07:45:23+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-full-join\/","url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-full-join\/","name":"PostgreSQL FULL JOIN","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-full-join\/#primaryimage"},"image":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-full-join\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2025\/02\/x-table.svg","datePublished":"2024-11-26T02:15:21+00:00","dateModified":"2025-02-10T07:45:23+00:00","description":"Use PostgreSQL FULL JOIN to merge rows from two tables and return rows from both tables including matching and non-matching rows.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-full-join\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-full-join\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-full-join\/#primaryimage","url":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2025\/02\/x-table.svg","contentUrl":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2025\/02\/x-table.svg","caption":"x table"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-full-join\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL Tutorial","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":3,"name":"PostgreSQL Full Join"}]},{"@type":"WebSite","@id":"https:\/\/www.pgtutorial.com\/#website","url":"https:\/\/www.pgtutorial.com\/","name":"PostgreSQL Tutorial","description":"Learn PostgreSQL from Scratch","alternateName":"PostgreSQL","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pgtutorial.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/332","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/comments?post=332"}],"version-history":[{"count":9,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/332\/revisions"}],"predecessor-version":[{"id":1968,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/332\/revisions\/1968"}],"up":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/13"}],"wp:attachment":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/media?parent=332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}