Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to understand "except all" in sql query

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);
like image 878
user234159 Avatar asked Feb 10 '15 13:02

user234159


People also ask

How does except all work in SQL?

The table operator except all returns the rows of the first result except those that are also in the second. As with all table operators both sides of except must have the same number of columns and their types must be compatible based on their position (see corresponding ).

How do I SELECT all values except one in SQL?

The SQL EXCEPT operator is used to return all rows in the first SELECT statement that are not returned by the second SELECT statement. Each SELECT statement will define a dataset. The EXCEPT operator will retrieve all records from the first dataset and then remove from the results all records from the second dataset.

Can we use except in SQL?

The SQL EXCEPT clause/operator is used to combine two SELECT statements and returns rows from the first SELECT statement that are not returned by the second SELECT statement. This means EXCEPT returns only rows, which are not available in the second SELECT statement.


2 Answers

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.

like image 155
Pradeep Kumar Das Avatar answered Oct 13 '22 22:10

Pradeep Kumar Das


The except operator returns the first table minus any overlap with the second table.

Set A = (10,11,12,10,10)

Set B = (10,10)

A except B --> (11,12)

A except all B --> (10,11,12)

except removes all occurrences of duplicate data from set A, whereas except all only removes one occurrence of duplicate data from set A for every occurrence in set B.

like image 21
Fernando Rodriguez Avatar answered Oct 14 '22 00:10

Fernando Rodriguez