Paste this into a new question or an answer at dba.stackexchange.com:
<!-- --> > create table emp(empid INT, name text, dept_id int, salary int); > Insert into emp values(1,'Rob', 1, 100); > Insert into emp values(2,'Mark', 1, 300); > Insert into emp values(3,'John', 2, 100); > Insert into emp values(4,'Mary', 2, 300); > Insert into emp values(5,'Bill', 3, 700); > Insert into emp values(6,'Jose', 6, 400); > > > create table department(deptid INT, name text); > Insert into department values(1,'IT'); > Insert into department values(2,'Accounts'); > Insert into department values(3,'Security'); > Insert into department values(4,'HR'); > Insert into department values(5,'R&D'); > > > <pre> > ✓ > 1 rows affected > 1 rows affected > 1 rows affected > 1 rows affected > 1 rows affected > 1 rows affected > > ✓ > 1 rows affected > 1 rows affected > 1 rows affected > 1 rows affected > 1 rows affected > </pre> <!-- --> > Select * from emp; > > > <pre> > empid | name | dept_id | salary > ----: | :--- | ------: | -----: > 1 | Rob | 1 | 100 > 2 | Mark | 1 | 300 > 3 | John | 2 | 100 > 4 | Mary | 2 | 300 > 5 | Bill | 3 | 700 > 6 | Jose | 6 | 400 > </pre> <!-- --> > Select * from department; > > <pre> > deptid | name > -----: | :------- > 1 | IT > 2 | Accounts > 3 | Security > 4 | HR > 5 | R&amp;D > </pre> <!-- --> > -- INNER JOIN > > Select a.empid, a.name, b.name as dept_name > FROM emp a > JOIN department b > ON a.dept_id = b.deptid > ; > > <pre> > empid | name | dept_name > ----: | :--- | :-------- > 1 | Rob | IT > 2 | Mark | IT > 3 | John | Accounts > 4 | Mary | Accounts > 5 | Bill | Security > </pre> <!-- --> > -- LEFT JOIN > > Select a.empid, a.name, b.name as dept_name > FROM emp a > LEFT JOIN department b > ON a.dept_id = b.deptid > ; > > <pre> > empid | name | dept_name > ----: | :--- | :-------- > 1 | Rob | IT > 2 | Mark | IT > 3 | John | Accounts > 4 | Mary | Accounts > 5 | Bill | Security > 6 | Jose | <em>null</em> > </pre> *db<>fiddle [here](https://dbfiddle.uk/?rdbms=postgres_11&fiddle=9dbdd6cf405ae24c5e2a0798a732d389)*
back to fiddle