Database.Beginner.What is the difference between INNER JOIN and LEFT JOIN?

INNER JOIN:

  • Returns only the rows that have matching values in both tables.
  • If there’s no match between the two tables, that row is excluded from the result.

LEFT JOIN (or LEFT OUTER JOIN):

  • Returns all rows from the left table.
  • If there’s a match in the right table, it returns the matching row.
  • If there’s no match, it still returns the row from the left table, but with NULLs for columns from the right table.

Example

Given two tables:

Customers

customer_idname
1Alice
2Bob
3Charlie

Orders

order_idcustomer_idproduct
1011Laptop
1022Smartphone

INNER JOIN example:

SELECT Customers.name, Orders.product
FROM Customers
INNER JOIN Orders ON Customers.customer_id = Orders.customer_id;

Result:

nameproduct
AliceLaptop
BobSmartphone
  • Only Alice and Bob appear — because they have matching orders.
  • Charlie doesn’t have an order, so he’s not shown.

LEFT JOIN example:

SELECT Customers.name, Orders.product
FROM Customers
LEFT JOIN Orders ON Customers.customer_id = Orders.customer_id;

Result:

nameproduct
AliceLaptop
BobSmartphone
CharlieNULL
  • Alice and Bob have matching orders — so we see their orders.
  • Charlie has no matching order — but he still shows up, and product is NULL.

In Short:

JOIN TypeRows Returned
INNER JOINOnly where there is a match in both tables.
LEFT JOINAll from left table, matched or not (fill NULLs if no match).

🎯 Analogy:

  • INNER JOIN is like an intersection — show only what both have in common.
  • LEFT JOIN is like a left sweep — show everything from the left, and match from the right if you can, otherwise leave NULL.
This entry was posted in Без рубрики. Bookmark the permalink.