25 March 2020

In PostgreSQL, the EXCEPT operator is used to retrieve distinct rows from the result set of the first query that are not present in the result set of the second query. It effectively performs a set difference operation between the two queries. The queries on either side of EXCEPT must have the same number of columns in their result sets, and the columns must be of compatible data types.

Source code viewer
  1. -- Comparing specific columns.
  2. SELECT comparing_columns FROM my_table_1
  3. EXCEPT
  4. SELECT comparing_columns FROM my_table_2
  5.  
  6. -- Comparing all columns when tables/schemas are identical.
  7. SELECT * FROM my_table_1
  8. EXCEPT
  9. SELECT * FROM my_table_2
Programming Language: PostgreSQL