Trying to understand "except all" in sql query

user234159 picture user234159 · Feb 10, 2015 · Viewed 17.6k times · Source

I came across this example and I don't understand what it means.

(SELECT drinker FROM Frequents)
     EXCEPT ALL
(SELECT drinker FROM Likes);

relations: Frequents(drinker, bar), Likes(drinker, beer)

What does the ALL do in this case? How is the result different from the query below?

(SELECT drinker FROM Frequents)
     EXCEPT
(SELECT drinker FROM Likes);

Answer

Pradeep Kumar Das picture Pradeep Kumar Das · Feb 10, 2015

The SQL EXCEPT operator takes the distinct rows of one query and returns the rows that do not appear in a second result set. The EXCEPT ALL operator does not remove duplicates. For purposes of row elimination and duplicate removal, the EXCEPT operator does not distinguish between NULLs.

EXCEPT ALL which returns all records from the first table which are not present in the second table, leaving the duplicates as is. Unfortunately, SQL Server does not support this operator.