Data6 min read10 interview questions

SQL joins explained

How to combine two tables, and what inner, left and right joins return.

In one sentenceA join lines up rows from two tables using a value they share, such as a customer number.

Think of it like thisIt is like matching a list of order slips to a list of customers by the customer number written on both.

The big picture

Tap any step to see where to use it, how to use it and an example result.

Inner join

Returns only rows that have a match in both tables. Customers who never ordered are left out, and so are orders with no customer.

Left join

Returns every row from the left table, with matching data from the right where it exists, and empty values where it does not. Use it to find customers who have never ordered.

Reading the query

In SELECT name, total FROM customers JOIN orders ON customers.id = orders.customer_id, the ON part says how rows are matched. Check that it uses the right columns, or you will get far more rows than expected.

  • INNER JOIN: only matches
  • LEFT JOIN: all from the left, matches where found
  • FULL JOIN: everything from both

Common mistakes

  • Forgetting the ON condition, which pairs every row with every row.
  • Using an inner join when you wanted to keep unmatched rows.
  • Joining on columns that are not unique and getting duplicate rows.

Quick recap

  • A join matches rows by a shared value
  • Inner keeps matches only
  • Left keeps all rows from the left table
  • Always check the row count after a join

Try it yourself

Create two tiny tables in SQLite with 3 rows each, make one row in each table that has no match, and run an inner and a left join to see the difference.

Top 10 interview questions

Ordered from basic to advanced. Try answering out loud first, then open the answer.

  1. What is a SQL join?Basic

    A way to combine rows from two tables based on a related column, for example matching orders to customers using customer_id.

  2. What does INNER JOIN return?Basic

    Only the rows that have a match in both tables.

  3. What is the difference between LEFT JOIN and INNER JOIN?Basic

    LEFT JOIN keeps every row from the left table and fills the right table's columns with NULL when there is no match. INNER JOIN drops rows without a match.

  4. What are primary keys and foreign keys?Basic

    A primary key uniquely identifies each row in a table. A foreign key is a column that points to the primary key of another table, which is what joins use.

  5. What do RIGHT JOIN and FULL OUTER JOIN do?Intermediate

    RIGHT JOIN is LEFT JOIN from the other side: it keeps every row of the right table. FULL OUTER JOIN keeps all rows from both tables, with NULL where there is no match.

  6. What is a CROSS JOIN?Intermediate

    It pairs every row of one table with every row of the other, giving rows multiplied by rows. It is rarely wanted, and an accidental one is a common cause of huge results.

  7. How would you find customers who have no orders?Intermediate

    LEFT JOIN orders and keep rows where the order id IS NULL, or use WHERE NOT EXISTS with a subquery.

  8. What is a self join?Intermediate

    Joining a table to itself using aliases, for example to match employees with their managers when both are rows in the same employees table.

  9. Why can a join return more rows than expected?Advanced

    If the join column is not unique on one side, each match repeats the row, so a one-to-many or many-to-many relationship multiplies rows. Check the keys, and aggregate or deduplicate when needed.

  10. How do you make a slow join faster?Advanced

    Make sure the join columns are indexed, select only the columns and rows needed, filter early, and check the query plan with EXPLAIN to see whether it scans whole tables.

Back to Learn Hub

Guides are simplified on purpose. Tool names and details change, so check the official documentation before relying on a detail.