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.
Saved automatically
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.
Related Questions in SQL & Relational Databases
What are Clustered and Non-Clustered Indexes in SQL, and when should you use each?
Intermediate level • 388 views
Explain SQL ACID properties and the differences between READ COMMITTED and REPEATABLE READ isolation levels.
Intermediate level • 3 views
What is Database Indexing, how does a B+ Tree index work, and when should you avoid creating indexes?
Intermediate level • 3 views
Explain Database Normalization from 1NF through BCNF with real-world anomaly examples.
Intermediate level • 3 views
Write a query for the second-highest distinct non-NULL salary. What happens if it does not exist?
Medium level • 3 views