Conditional DELETE and INSERT in PostgreSQL


We are comfortable with inserting/deleting record(s) into/from a table. But we sometimes get confused when we need to insert or delete records conditionally from a table. Let us look at an example.

Example scenario

Suppose we have a table, called ALL_EMPLOYEES, which contains all employees’ information who are working or had ever worked for the company. Suppose we also have a table, called SEP_EMPLOYEES, which contains those employees’ information who have separated from the company. Now, the company wants to clean up their database of all the separated employees’ information from the ALL_EMPLOYEES table. In this case, we need