'SQL statement to get all customers with no orders

I have a typical Persons table and an Orders table defined in such a way that I can do JOIN query as the following to return Orders for all Persons.

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
INNER JOIN Orders
ON Persons.id=Orders.Person_id

The question is, how do I write a statement that would return all Persons with NO Orders?

I'm using mysql.

Thank all in advance.



Solution 1:[1]

This should work... theres more than one way to do it.

select * from persons where person.id not in (select person_id from orders)

Solution 2:[2]

Just for completeness, here is the not exists version:

select * from persons p 
where not exists
(select null from orders o where o.person_id = p.id)

Solution 3:[3]

You can use left join:

    SELECT DISTINCT o.CustomerID from Orders as o
    left join Customers as c
    on o.CustomerID=c.CustomerID

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 David
Solution 2
Solution 3 Kristian