An inner join combines rows from two tables only when they match on a shared column
An inner join is the most common way to connect data from two tables in SQL. It returns only the rows where the values in a shared column exist in both tables. If a row in the first table has no matching row in the second table, that row does not appear in the result.
Think of it like a Venn diagram where only the overlapping middle section shows up. You ask the database: "Show me records from Table A and Table B, but only when they have the same value in this column." Anything that doesn't match gets left out.
Key Takeaways
- An inner join returns only rows where the join column values match in both tables; unmatched rows are excluded.
- The basic syntax is SELECT * FROM table1 INNER JOIN table2 ON table1.column = table2.column.
- Inner joins work with any column that contains comparable data, not just primary keys.
- If you need unmatched rows too, you would use a left join, right join, or full outer join instead.
The basic syntax and how to read it
The structure of an inner join query looks like this:
SELECT * FROM customers INNER JOIN orders ON customers.customer_id = orders.customer_id
Break this down piece by piece. SELECT * means "get all columns." FROM customers names your first table. INNER JOIN orders says "connect this to the orders table." ON customers.customer_id = orders.customer_id is the matching rule — it tells SQL which column to use to decide if a row belongs in the result.
The ON clause is the critical part. It specifies the condition that must be true for rows to be included. Both sides of the equals sign must have the same value for that row to appear in your result. If a customer has no orders, that customer row never appears. If an order has no matching customer, that order row never appears.
A concrete example with real data
Imagine you have a customers table with three rows: customer_id 1 (Alice), customer_id 2 (Bob), and customer_id 3 (Carol). You also have an orders table with four rows: order_id 101 for customer_id 1, order_id 102 for customer_id 1, order_id 103 for customer_id 2, and order_id 104 for customer_id 4.
When you run an inner join on customer_id, the result includes only three orders: the two orders from Alice and one from Bob. Order 104 disappears because customer_id 4 does not exist in the customers table. Carol disappears because she has no orders. The inner join shows only the intersection — the data that exists in both tables.
If you had used a left join instead, you would keep all three customers and show NULL for Carol's orders. An inner join is stricter: it keeps only the matches.
When to use an inner join versus other join types
Use an inner join when you want to see only records that have a match in both tables. Common scenarios include finding customers who have placed orders, finding employees assigned to projects, or finding products that have been sold. You are asking: "What data exists in both places?"
Use a left join when you want to keep all rows from the first table even if they have no match in the second table. This is useful when you want to see customers who have never ordered, or employees with no assigned projects.
Use a right join when you want to keep all rows from the second table. Use a full outer join when you want to keep all rows from both tables, whether they match or not. Most databases support these alternatives, though some (like MySQL) require workarounds for full outer joins.
Common mistakes when writing inner joins
The most frequent error is forgetting the ON clause or writing it incorrectly. Without it, SQL performs a cross join — it multiplies every row in the first table by every row in the second table, creating a huge result set you probably did not want. Always double-check that your ON condition is spelled correctly and uses the right column names.
Another mistake is joining on columns with different data types. If one column is a number and the other is text, the match may fail silently or produce unexpected results. Make sure both sides of the ON clause contain the same type of data.
A third error is assuming an inner join will return all rows from your first table. It will not. If you need all rows from the first table regardless of matches, you need a left join. Test your query on a small dataset first to see what actually comes back.
Joining more than two tables
You can chain inner joins together to connect three, four, or more tables. Each join adds another matching condition. For example:
SELECT * FROM customers INNER JOIN orders ON customers.customer_id = orders.customer_id INNER JOIN products ON orders.product_id = products.product_id
This query connects customers to orders, then orders to products. A row appears in the result only if a customer exists, that customer has an order, and that order references a product that exists. The more joins you add, the stricter the filter becomes — fewer rows will match all the conditions.
When joining multiple tables, be careful about the order. It usually does not affect the final result, but it can affect performance. Start with the table that will eliminate the most rows, then join smaller result sets to other tables.
Performance considerations with inner joins
Inner joins are generally fast because the database can use indexes on the join columns to find matches quickly. If you are joining on a primary key or a column with an index, the query runs efficiently even with large tables.
Performance slows down when you join on columns without indexes, or when you join on calculated values (like UPPER(name)). The database has to scan every row and compute the value before checking for a match. If you find a join is slow, check whether the join columns are indexed and whether you can add an index if they are not.
Joining many tables together also slows queries down. If you are chaining five or more joins, consider whether you really need all that data, or whether you could break the query into smaller pieces.
Frequently Asked Questions
What is the difference between INNER JOIN and just JOIN?
They are the same thing. JOIN without a keyword defaults to INNER JOIN. Writing SELECT * FROM table1 JOIN table2 produces identical results to SELECT * FROM table1 INNER JOIN table2. Most people write just JOIN because it is shorter, but both are correct.
Can I use an inner join with more than one matching condition?
Yes. Use AND to add more conditions to the ON clause. For example: ON customers.customer_id = orders.customer_id AND customers.country = orders.country. A row appears in the result only if both conditions are true. You can add as many conditions as you need.
What happens if the join column has NULL values?
Rows with NULL in the join column never match anything, even other NULLs. If a customer_id is NULL in the customers table, that row will not appear in an inner join result, even if there are orders with NULL customer_id. This is standard SQL behavior and can be surprising if you have NULL values in your data.
Can I join a table to itself with an inner join?
Yes, this is called a self-join. You give the same table two different names (called aliases) and join them together. For example: SELECT * FROM employees e1 INNER JOIN employees e2 ON e1.manager_id = e2.employee_id. This finds each employee and their manager by matching them within the same table.
Do I need to list all columns, or can I choose specific ones?
You can choose specific columns. Instead of SELECT *, name the columns you want: SELECT customers.name, orders.order_id FROM customers INNER JOIN orders ON customers.customer_id = orders.customer_id. Selecting only the columns you need makes queries faster and results easier to read.