'Sales without associated accounts with a Left Join?

I thought I understood how to do this, but I might be missing something. Would this not work?

SELECT COUNT(*)
FROM Sale A
LEFT JOIN Account B on A.Sale_id= B.Sale_id
WHERE account_id IS NULL

Tables involved



Solution 1:[1]

You can do this in one of the many ways. For Instance

Use NOT IN Operator

Select 
     * 
from Sale 
where 
    Sale_id NOT IN (SELECT DISTINCT(Sale_id) FROM Account)

Subquery will return the sale ids which has records in Account Table. NOT IN operator returns all other records from Sale Table

Use LEFT JOIN

Select 
   s.*
from Sale s left join Account a on s.Sale_id=a.Sale_id
where a.Sale_id is null 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bilal Bin Zia