Verified Jobs Live
SQL & Relational Databases Medium Level ~3 min read

A LEFT JOIN report counts one order for customers who have none. What caused it?

Interactive Practice Mode
Formulate your thoughts or take notes before revealing the model solution.
Model Answer & Deep-Dive Explanation

COUNT(*) counts the NULL-extended customer row produced by the LEFT JOIN. Count a non-nullable order identifier instead to count actual matching orders. Group by a stable customer identifier, and include other selected columns in the grouping as required by the dialect. Additional joins can multiply order rows, so verify cardinality rather than adding DISTINCT without understanding the data. Test customers with zero, one, and several orders. The following query assumes orders.id is non-NULL and each joined row represents one order.