Black Friday Sale20% off every plan — this weekend onlyEnds in04days11hrs51min52secSee plans

SQL joins explained: INNER, LEFT, RIGHT, FULL and SELF

SQL Updated 2 min read By LogicByte team

Short answer

A join combines rows from two tables using a matching column. INNER JOIN keeps only matches; LEFT JOIN keeps every row from the first table; RIGHT JOIN keeps every row from the second; FULL OUTER JOIN keeps everything from both.

On this page
  1. The example tables
  2. INNER JOIN — only matches
  3. LEFT JOIN — every customer
  4. RIGHT JOIN — every order
  5. FULL OUTER JOIN — everything
  6. CROSS JOIN — every combination
  7. SELF JOIN — a table with itself
  8. Mistakes to avoid

The example tables

customers: Asha (1), Ravi (2), Meera (3). orders: order 101 by customer 1, order 102 by customer 1, order 103 by customer 4 (a customer who was deleted).

INNER JOIN — only matches

SELECT c.name, o.order_id
FROM customers c
INNER JOIN orders o ON o.customer_id = c.id;

Result: Asha–101, Asha–102. Ravi and Meera have no orders; order 103 has no customer.

LEFT JOIN — every customer

SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id;

Result: Asha–101, Asha–102, Ravi–NULL, Meera–NULL. Add WHERE o.order_id IS NULL to find customers with no orders — a very common interview question.

RIGHT JOIN — every order

The mirror of LEFT JOIN: keeps all orders, so order 103 appears with a NULL name. Most people rewrite it as a LEFT JOIN with the tables swapped.

FULL OUTER JOIN — everything

Keeps all customers and all orders. MySQL does not support FULL OUTER JOIN; combine a LEFT and a RIGHT join with UNION instead.

SELECT c.name, o.order_id FROM customers c LEFT JOIN orders o ON o.customer_id = c.id
UNION
SELECT c.name, o.order_id FROM customers c RIGHT JOIN orders o ON o.customer_id = c.id;

CROSS JOIN — every combination

Pairs every row of one table with every row of the other. 3 customers × 3 orders = 9 rows. Useful for building calendars or size-colour grids.

SELF JOIN — a table with itself

SELECT e.name AS employee, m.name AS manager
FROM employees e
LEFT JOIN employees m ON m.id = e.manager_id;

Mistakes to avoid

  • Filtering the right table in WHERE turns a LEFT JOIN into an INNER JOIN. Put that condition in the ON clause instead.
  • Joining on a column that is not unique multiplies rows. Check counts before and after the join.

Try it yourself

5 questions from the LogicByte practice bank. Pick an answer to check it.

  1. 1What is the maximum row count if `a` has 3 rows and `b` has 4 rows?

    SELECT * FROM a CROSS JOIN b;
    Show answer

    Answer: A — 12

    CROSS JOIN produces 3 × 4 combinations.

  2. 2What does an INNER JOIN do if no rows match the condition?

    Show answer

    Answer: B — Returns no rows for those unmatched records

    INNER JOIN keeps only matching pairs.

  3. 3Which query keeps all employees when a manager may be absent?

    SELECT e.name, m.name AS manager_name FROM employees e LEFT JOIN employees m ON e.manager_id = m.employee_id;
    Show answer

    Answer: A — SELECT e.name, m.name AS manager_name FROM employees e LEFT JOIN employees m ON e.manager_id = m.employee_id;

    LEFT self join preserves employees without managers.

  4. 4Which join is appropriate to show all students and their enrollments if some students are not enrolled?

    Show answer

    Answer: B — LEFT JOIN students to enrollments

    LEFT JOIN preserves students without enrollment rows.

  5. 5What does this query return?

    SELECT a.account_id, t.transaction_id FROM accounts a LEFT JOIN transactions t ON a.account_id = t.account_id AND t.amount > 0;
    Show answer

    Answer: A — All accounts and positive transactions when present

    The ON filter limits matches without removing accounts.

Take the free 10-question SQL test

Frequently asked questions

What is the difference between JOIN and INNER JOIN?

None. JOIN on its own means INNER JOIN.

Which join is used most in data analysis?

LEFT JOIN, because analysts usually want to keep every record from the main table, such as all customers, even when related data is missing.

Does MySQL support FULL OUTER JOIN?

No. Use a LEFT JOIN and a RIGHT JOIN combined with UNION.