Paste this into a new question or an answer at dba.stackexchange.com:
<!-- --> > CREATE TABLE categories ( > category_id SERIAL PRIMARY KEY, > name VARCHAR(255) NOT NULL > ); > > CREATE TABLE brands ( > brand_id SERIAL PRIMARY KEY, > name VARCHAR(255) NOT NULL, > created_at TIMESTAMP NOT NULL > ); > > CREATE TABLE products ( > product_id SERIAL PRIMARY KEY, > brand_id INT NOT NULL, > category_id INT NOT NULL, > title VARCHAR(255) NOT NULL, > created_at TIMESTAMP NOT NULL, > CONSTRAINT fk_brand FOREIGN KEY(brand_id) REFERENCES brands(brand_id), > CONSTRAINT fk_category FOREIGN KEY(category_id) REFERENCES categories(category_id) > ); > > <pre> > ✓ > > ✓ > > ✓ > </pre> <!-- --> > INSERT INTO brands(name, created_at) VALUES > ('Apple', '2020-01-01'), > ('Google', '2020-01-02'), > ('Microsoft', '2020-01-03'); > > INSERT INTO categories(name) VALUES > ('laptops'), > ('phones'), > ('tablets'); > > INSERT INTO products(brand_id, category_id, title, created_at) VALUES > (1, 1, 'Macbook Pro', '2020-01-01'), > (1, 2, 'iPhone SE', '2020-01-02'), > (1, 3, 'iPad Air', '2020-01-03'), > (2, 1, 'Pixelbook GO', '2020-01-04'), > (2, 2, 'Pixel 5', '2020-01-05'), > (3, 1, 'Surface 3', '2020-01-06'); > > <pre> 3 rows affected > 3 rows affected > 6 rows affected > </pre> <!-- --> > SELECT * FROM brands; > > <pre> > brand_id | name | created_at > -------: | :-------- | :------------------ > 1 | Apple | 2020-01-01 00:00:00 > 2 | Google | 2020-01-02 00:00:00 > 3 | Microsoft | 2020-01-03 00:00:00 > </pre> <!-- --> > SELECT * FROM categories; > > <pre> > category_id | name > ----------: | :------ > 1 | laptops > 2 | phones > 3 | tablets > </pre> <!-- --> > SELECT * FROM products; > > <pre> > product_id | brand_id | category_id | title | created_at > ---------: | -------: | ----------: | :----------- | :------------------ > 1 | 1 | 1 | Macbook Pro | 2020-01-01 00:00:00 > 2 | 1 | 2 | iPhone SE | 2020-01-02 00:00:00 > 3 | 1 | 3 | iPad Air | 2020-01-03 00:00:00 > 4 | 2 | 1 | Pixelbook GO | 2020-01-04 00:00:00 > 5 | 2 | 2 | Pixel 5 | 2020-01-05 00:00:00 > 6 | 3 | 1 | Surface 3 | 2020-01-06 00:00:00 > </pre> <!-- --> > SELECT products.*, categories.* > FROM products > NATURAL JOIN categories; > > <pre> > product_id | brand_id | category_id | title | created_at | category_id | name > ---------: | -------: | ----------: | :----------- | :------------------ | ----------: | :------ > 1 | 1 | 1 | Macbook Pro | 2020-01-01 00:00:00 | 1 | laptops > 2 | 1 | 2 | iPhone SE | 2020-01-02 00:00:00 | 2 | phones > 3 | 1 | 3 | iPad Air | 2020-01-03 00:00:00 | 3 | tablets > 4 | 2 | 1 | Pixelbook GO | 2020-01-04 00:00:00 | 1 | laptops > 5 | 2 | 2 | Pixel 5 | 2020-01-05 00:00:00 | 2 | phones > 6 | 3 | 1 | Surface 3 | 2020-01-06 00:00:00 | 1 | laptops > </pre> <!-- --> > SELECT products.*, brands.* > FROM products > NATURAL JOIN brands; > > <pre> > product_id | brand_id | category_id | title | created_at | brand_id | name | created_at > ---------: | -------: | ----------: | :---------- | :------------------ | -------: | :---- | :------------------ > 1 | 1 | 1 | Macbook Pro | 2020-01-01 00:00:00 | 1 | Apple | 2020-01-01 00:00:00 > </pre> <!-- --> > CREATE TABLE t1 ( > a INT, > b INT > ); > > CREATE TABLE t2 ( > a INT, > b INT > ); > > <pre> > ✓ > > ✓ > </pre> <!-- --> > INSERT INTO t1 VALUES > (1, 2), > (3, 4), > (5, 6); > > INSERT INTO t2 VALUES > (3, 4), > (5, 6), > (7, 8); > > <pre> 3 rows affected > 3 rows affected > </pre> <!-- --> > SELECT * > FROM ( > SELECT 't1' AS t1, t1.* FROM t1 > ) t1 > NATURAL FULL JOIN ( > SELECT 't2' AS t2, t2.* FROM t2 > ) t2 > WHERE t1 IS NULL OR t2 IS NULL; > > <pre> > a | b | t1 | t2 > -: | -: | :--- | :--- > 1 | 2 | t1 | <em>null</em> > 7 | 8 | <em>null</em> | t2 > </pre> *db<>fiddle [here](https://dbfiddle.uk/?rdbms=postgres_12&fiddle=8daf17bd8051cb74c7d076701db52e34)*
back to fiddle