Programming fundamentals
SQL Joins Explained With Examples and Result Rows
A join combines rows from two tables by matching a column, and the join type decides what happens to rows that find no partner: INNER drops them, LEFT keeps unmatched left rows, RIGHT keeps unmatched right rows, FULL keeps both, and CROSS pairs everything. Seeing the actual result rows for one tiny dataset is the fastest way to stop guessing which one you need.
By CodingMarble · Published · 5 min read

The two tables used throughout
Every example below uses the same two small tables so the only thing that changes is the join. A customers table holds three people. An orders table holds four orders: two from Asha, one from Ben, and one guest checkout whose customer_id is NULL because nobody was logged in. Chen has never ordered anything. Those two awkward facts, a customer with no orders and an order with no customer, are exactly what separate the join types.
customers orders
id | name id | customer_id | item
1 | Asha 101 | 1 | Notebook
2 | Ben 102 | 1 | Pen
3 | Chen 103 | 2 | Lamp
104 | NULL | PosterThe relationship is one-to-many: one customer can have many orders, and each order points back to at most one customer through customer_id. Keep that shape in mind, because it explains both the duplicates and the missing rows you are about to see.
INNER JOIN: only the matches
An INNER JOIN returns a row only when the ON condition is true for a pair of rows. Anything without a partner disappears, which is why Chen and the guest order vanish. JOIN on its own means INNER JOIN in standard SQL, so the two spellings are identical.
SELECT c.name, o.id AS order_id, o.item
FROM customers AS c
INNER JOIN orders AS o ON o.customer_id = c.id;
-- name | order_id | item
-- Asha | 101 | Notebook
-- Asha | 102 | Pen
-- Ben | 103 | LampNotice Asha appears twice. The result has one row per matching pair, not one row per customer. That is correct behaviour for a one-to-many join, and it matters the moment you add up or count anything, as the duplicates section explains.
LEFT, RIGHT and FULL joins
A LEFT JOIN keeps every row from the table written before the keyword, here customers, and fills the columns from the other table with NULL when nothing matches. Chen now appears with empty order columns. The guest order is still missing, because it has no row on the left to attach to.
SELECT c.name, o.id AS order_id, o.item
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.id;
-- name | order_id | item
-- Asha | 101 | Notebook
-- Asha | 102 | Pen
-- Ben | 103 | Lamp
-- Chen | NULL | NULLA RIGHT JOIN is the mirror image: all orders are kept, so the guest Poster order appears with a NULL name, and Chen disappears. Most people simply swap the table order and write a LEFT JOIN instead, which reads more naturally. A FULL JOIN keeps unmatched rows from both sides, giving five rows here: the three matches, Chen with NULL order columns, and the Poster with a NULL name. Dialect note: MySQL and MariaDB do not support FULL JOIN directly, and SQLite only added it in recent versions, so check your engine's documentation; the usual workaround is a LEFT JOIN combined with a RIGHT JOIN using UNION.
SELECT c.name, o.id AS order_id, o.item
FROM customers AS c
FULL JOIN orders AS o ON o.customer_id = c.id;
-- Asha | 101 | Notebook
-- Asha | 102 | Pen
-- Ben | 103 | Lamp
-- Chen | NULL | NULL
-- NULL | 104 | PosterCROSS JOIN: every combination
A CROSS JOIN has no ON condition. It pairs every row of one table with every row of the other, so three customers and four orders produce twelve rows. That is rarely what you want from these two tables, but it is genuinely useful for generating combinations, such as every size crossed with every colour for a product catalogue, or every date crossed with every store for a report skeleton.
The danger is accidental cross joins. Forgetting the ON clause, or writing a condition that is always true, silently multiplies the rows. Two tables of a thousand rows become a million. When a query is mysteriously slow and returns far too many rows, check that every join has a condition linking the tables.
Finding rows that have no match
A very common question is which customers have never ordered. The reliable pattern is a LEFT JOIN followed by a filter for NULL in a column that can only be NULL when no match was found. Use the other table's key, here o.id, because a real order id is never NULL.
SELECT c.id, c.name
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.id
WHERE o.id IS NULL;Test the condition with IS NULL, never = NULL, because comparing anything to NULL yields unknown rather than true. Also keep the filter on the right-hand table in the WHERE clause only when you mean to find missing rows. If you want a LEFT JOIN restricted to, say, orders for one item while still keeping every customer, put that restriction in the ON clause, otherwise the WHERE turns your LEFT JOIN into an inner join by discarding the NULL rows.
Duplicates from one-to-many joins
Joining customers to orders repeats each customer once per order. If you then sum a customer-level column, or count customers with COUNT(*), you count the repeats. To count orders per customer including those with none, group on the customer and count a column from the orders table.
SELECT c.name, COUNT(o.id) AS order_count
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.id
GROUP BY c.id, c.name;
-- Asha | 2
-- Ben | 1
-- Chen | 0COUNT(o.id) ignores NULLs, so Chen correctly shows zero, whereas COUNT(*) would report one because the NULL-filled row still exists. When a total looks inflated, suspect a join that multiplied rows, and check by counting before and after adding each join.
Common join mistakes
Two mistakes cause most wrong results. The first is forgetting that a one-to-many join repeats the parent row. Summing a customer-level value across the joined rows counts it once per order. If you only need to know whether a customer has any orders, use EXISTS instead of a join, which returns each customer once however many orders they have.
The second is filtering the right-hand table of a LEFT JOIN in WHERE. Suppose you want every customer, along with only their Pen orders. Writing WHERE o.item = 'Pen' discards every row where o.item is NULL, so Ben and Chen vanish and the query behaves like an inner join. Move the condition into ON, and the join decides which orders match while the left side keeps all its rows.
-- Wrong: returns only Asha
SELECT c.name, o.item
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.id
WHERE o.item = 'Pen';
-- Right: returns Asha with Pen, Ben and Chen with NULL
SELECT c.name, o.item
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.id AND o.item = 'Pen';
-- Customers with at least one order, each listed once
SELECT c.name
FROM customers AS c
WHERE EXISTS (SELECT 1 FROM orders AS o WHERE o.customer_id = c.id);A third, quieter mistake is joining on a column that is not unique on the side you assumed was unique. If two customers shared an id, every order would appear twice. Confirm the key is really unique with a constraint, or with a GROUP BY and HAVING COUNT(*) greater than one on that column.
Performance basics
Joins are fast when the database can look up matching rows through an index on the join column. The foreign key side, orders.customer_id, usually needs its own index, because some engines create one for the primary key automatically but not for the referencing column. Select only the columns you need, filter early with a WHERE clause, and join on columns of the same type so no conversion is needed.
Practical tip: before trusting a join, run it on a tiny hand-made dataset like the one above and predict the row count on paper. Then run it on real data with EXPLAIN in front of it to see the plan your database chose. A mismatch between your prediction and the result is almost always a missing ON condition or a one-to-many repeat.
Keep reading
- Interview questions and cheat sheetsSQL Cheat Sheet for BeginnersThis cheat sheet teaches the SQL you use every week, task by task, with every query written against one small customers and orders schema. It uses standard SQL and points out where MySQL, PostgreSQL, SQL Server and SQLite differ.
- Programming fundamentalsDatabase Indexing Explained: When It Helps and When It HurtsA database index is a separate, sorted structure that lets the engine jump to matching rows instead of reading the whole table, at the cost of extra storage and slower writes. It only helps when your queries can actually use it, so the habit to build is checking the query plan with EXPLAIN rather than adding indexes on a hunch.
- Programming fundamentalsData Structures Every Developer Should Know, in PracticeA data structure is a way of arranging data so that the operations you need most, such as lookup, insert or ordering, are cheap. Learning the seven or eight common ones, and what each does well, lets you choose the right container before you write the code instead of fixing a slow one afterwards.
- Web developmentREST vs GraphQL: How to Choose an API StyleREST exposes many URLs that each return a fixed resource, while GraphQL exposes one endpoint where the client describes the exact shape of data it wants. For a small team with a handful of clients, REST is usually the simpler default; GraphQL pays off when many different screens or apps need different slices of the same data.
Need this built properly for your business?
CodingMarble is a remote studio in India that builds websites, apps and games on fixed packages with the scope written down before payment. Try the free tools we run, or start a project when you are ready.