I understand, that INNER JOIN is made for referenced keys and INTERSECT is not. But afaik in some cases, both of them can do the same thing. So, is there a difference (in performance or anything) between the following two expressions? And if there is, which one is better?
Expression 1:
SELECT id FROM customers
INNER JOIN orders ON customers.id = orders.customerID;
Expression 2:
SELECT id FROM customers
INTERSECT
SELECT customerID FROM orders
They are very different, even in your case.
The
INNER JOINwill return duplicates, ifidis duplicated in either table.INTERSECTremoves duplicates. TheINNER JOINwill never returnNULL, butINTERSECTwill returnNULL.The two are very different;
INNER JOINis an operator that generally matches on a limited set of columns and can return zero rows or more rows from either table.INTERSECTis a set-based operator that compares complete rows between two sets and can never return more rows than in the smaller table.