Skip to content
Advertisement

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.

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.

Advertisement

Answer

You may want to use LEFT JOIN and IS NULL:

The result of a left join always contains all records of the “left” table (Persons), even if the join-condition does not find any matching record in the “right” table (Orders). When there is no match, the columns of the “right” table will NULL in the result set.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement