How would you explain INNER JOIN, LEFT JOIN, grouping and aggregation during a data-validation task?
Answer
INNER JOIN returns matching rows from both sides; LEFT JOIN retains every left-side row and adds matching right data. GROUP BY forms sets for aggregate functions such as COUNT or SUM, while HAVING filters groups after aggregation.
Strong answer includes
- matching versus retained rows
- grouping before aggregation
- WHERE versus HAVING
Practical example
Runs against the resettable SQLite sample database used by the playground.
Run the query to see the result.
INNER JOIN removes left-side rows without a match; LEFT JOIN preserves them and fills right-side columns with NULL. GROUP BY creates one aggregate group per customer, aggregate functions calculate values for each group, and HAVING filters those groups after aggregation. COUNT(o.id), rather than COUNT(*), keeps zero-order customers from being counted as one joined row.
Expected result: The first query contains only matched customers/orders; the second returns customers with at least two orders and their aggregated spend.