Español Practice SQL

SQL LEFT JOIN: keep every row and find missing matches

LEFT JOIN keeps every row of the table after FROM. No match means NULL on the other side.

SQL skills · Joins

Practice this nowLearn it step by step

Free. No sign up. Your progress stays in this browser.

What you will learn

INNER JOIN drops rows without a match. LEFT JOIN keeps them and fills the missing side with NULL.

Those NULLs are useful: they tell you what has no match at all.

The basics

LEFT JOIN keeps every row of the table after FROM. When the other table has no match, its columns are filled with NULL.

SELECT c.customer_name, i.invoice_id
FROM customers c
LEFT JOIN invoices i ON i.customer_id = c.customer_id

Finding what is missing

SELECT c.customer_name
FROM customers c
LEFT JOIN invoices i ON i.customer_id = c.customer_id
WHERE i.invoice_id IS NULL

This pattern answers questions like which customers never ordered or which agents have no tickets.

Counting with zeros

After a LEFT JOIN, COUNT(*) counts the empty row and shows 1. COUNT(i.invoice_id) counts only real matches and shows 0.

Table order matters

With LEFT JOIN, the table after FROM is the one kept whole. RIGHT JOIN does the opposite and is rarely used; FULL JOIN keeps both sides.

Worked examples

Example 1

Customers who never got an invoice.

SELECT c.customer_name
FROM customers c
LEFT JOIN invoices i ON i.customer_id = c.customer_id
WHERE i.invoice_id IS NULL

The classic “find what is missing” query.

Example 2

Invoices per customer, including zero.

SELECT c.customer_name, COUNT(i.invoice_id)
FROM customers c
LEFT JOIN invoices i ON i.customer_id = c.customer_id
GROUP BY c.customer_name

COUNT of a column gives an honest 0.

Reading helps, typing makes it stick. Every task gives you a fresh table, and your query is checked by a real database.

Try a task

Common mistakes

  • Using COUNT(*) after a LEFT JOIN, which shows 1 for customers with nothing.
  • Putting the tables in the wrong order: the one after FROM is kept whole.
  • Filtering the right table in WHERE, which turns the LEFT JOIN back into an inner one. Put such conditions in ON.

Where you will use it at work

  • Marketing: campaigns that brought no leads.
  • HR: departments with no employees.
  • Support: agents with no tickets this month.

Questions people ask

What is RIGHT JOIN?

The same idea with the second table kept whole. Most people swap the tables and use LEFT JOIN.

What is FULL JOIN?

It keeps unmatched rows from both sides.

Why did my LEFT JOIN lose rows?

Usually a WHERE condition on the right table. Move it into the ON clause.

Ready to write it yourself?

Practice LEFT JOIN and missing matches

Further reading

Related guides