Except and Except ALL in postgresql (Set Difference in Postgresql)

EXCEPT in Postgresql returns the rows that are in the first table but not in the second table. (i.e.) Except Operator performs set difference in Postgresql. Except Operator removes the duplicate. Except  ALL in postgresql does not remove duplicate.

Both EXCEPT and EXCEPT ALL in Postgresql returns the common rows of two different SQLs. They differ in the way they handle duplicates.

Except in Postgresql: performs a DISTINCT on the result set, returns set difference in Postgresql.

Except in Postgresql 4

 

EXCEPT ALL in Postgresql: Performs the set difference but does not remove duplicates

Lets discuss with an example. Lets take two table for example

Table 1 : SummerFruits

Except in Postgresql 1

Table 2 : Fruits

Except in Postgresql 2

 

EXCEPT in Postgresql:

SELECT * FROM SummerFruits
EXCEPT
SELECT * FROM Fruits

EXCEPT gets the set difference and also remove duplicates so resultant table will be

Except in Postgresql 3

 

EXCEPT ALL in Postgresql:


SELECT * FROM SummerFruits
EXCEPT ALL
SELECT * FROM Fruits

EXCEPT ALL gets the set difference and does not remove duplicates so resultant table will be

Except in Postgresql 5