In standard SQL, there are no parentheses in DISTINCT colA . DISTINCT not a function.
SELECT DISTINCT colA FROM mytable WHERE colA NOT IN (SELECT DISTINCT colB FROM mytable);
Added DISTINCT to the subselect. If you have a lot of duplicates, this can speed up the request.
CTE may be faster, depending on your DBMS. In addition, I demonstrate LEFT JOIN as an alternative to excluding values in valB and an alternative way to get different values using GROUP BY :
WITH x AS (SELECT colB FROM mytable GROUP BY colB) SELECT m.colA FROM mytable m LEFT JOIN x ON x.colB = m.colA WHERE x.colB IS NULL GROUP BY m.colA;
Or, simplified further, and with a simple subquery (possibly the fastest):
SELECT DISTINCT m.colA FROM mytable m LEFT JOIN mytable x ON x.colB = m.colA WHERE x.colB IS NULL;
basically 4 methods to exclude rows with keys present in another (or the same) table:
The deciding factor for speed will be the indices . You need to have indices on colA and colB to make this query fast.
source share