Learn · From beginner to pro

Learn the skill.
Understand how it works.

Every lesson ends on the mistake people actually make, so you learn the tool and the trap together.

SQL for Data Analytics · Lesson 14Join tables without losing rows
Context

Marketing wants revenue per customer, including customers who haven't bought yet.

AnBinhChiDungElla

5 customers · 3 with a paid order

Approach

Start from the table that decides who is counted, then attach the rest.

customersorders

Every customer stays, matched or not.

Write the code
SELECT c.name, SUM(o.total)
FROM customers c
LEFT JOIN orders o
  ON o.customer_id = c.id
 AND o.status = 'paid'
GROUP BY c.name;
Keep in mind · the trap

Move the 'paid' filter into WHERE and two customers vanish. Your LEFT JOIN quietly became an INNER JOIN.

AnBinhChiDungElla
Takeaways
  • The left table decides the rows.
  • Filter the right table in ON, not WHERE.
  • Count rows before and after every join.
8 min · Lesson 14 of 32Continue lesson →

Your Data Path

Explore more