SQL Joins Explained Visually: Inner, Left, Right, and Full

SQL joins confuse people because the diagrams look simple and the results do not. Two circles overlap, and somehow the query returns rows you did not expect, or drops rows you needed. SQL joins explained with a small example fixes that: once you see what each join does to two tiny tables, the Venn diagrams finally make sense, and you can predict the result before you run the query.

SQL joins explained with a small example

Picture two tables. The customers table has four rows: Ana, Ben, Cara, and Dan. The orders table has three rows: one for Ana, one for Ben, and one for a customer ID that matches nobody, a leftover from a deleted account. Every join below combines these two tables on customer ID. Keep this picture in mind, because each join is just a different rule about which rows survive.

customers: id 1 Ana, 2 Ben, 3 Cara, 4 Dan
orders:    order 101 customer 1, order 102 customer 2, order 103 customer 99

Inner join keeps matches only

An inner join keeps matches only. It returns a row when a customer has an order and the order has a customer. From our tables that is two rows: Ana with order 101, and Ben with order 102. Cara and Dan disappear because they have no orders. Order 103 disappears because customer 99 does not exist. Use an inner join when you only care about pairs that exist on both sides, like a report of customers who actually bought something. It is the default join in most queries, and the one to reach for first. When you write JOIN without a qualifier, this is what you get.

SELECT customers.name, orders.id
FROM customers
INNER JOIN orders ON orders.customer_id = customers.id;
-- Ana 101, Ben 102

Left join keeps every row from the left

A left join keeps every row from the left table, and adds matching rows from the right where they exist. Our query returns four rows: Ana with order 101, Ben with order 102, Cara with NULL, and Dan with NULL. The NULLs are the point: they show you the customers without orders. Left join is the workhorse for “find the missing ones” questions, like customers who never ordered, or products never sold. It is also the most common source of wrong row counts, because a left join can multiply rows when the right side has several matches for one left row. If a customer has three orders, that customer appears three times.

SELECT customers.name, orders.id
FROM customers
LEFT JOIN orders ON orders.customer_id = customers.id;
-- Ana 101, Ben 102, Cara NULL, Dan NULL

Right join in practice

A right join in practice is a left join with the tables swapped. It keeps every row from the right table and adds matches from the left. Our query returns three rows: Ana with order 101, Ben with order 102, and NULL with order 103, the orphan order. Most teams write left joins only and simply swap the table order when they need this result, which is why you will see right joins far less often in real code. Know what it does so you can read it when you meet it, but do not feel obliged to use it. Consistency beats variety in a shared codebase.

SELECT customers.name, orders.id
FROM customers
RIGHT JOIN orders ON orders.customer_id = customers.id;
-- Ana 101, Ben 102, NULL 103

Full outer join

A full outer join keeps everything from both tables: matched pairs plus unmatched rows from each side, with NULLs filling the gaps. Our query returns five rows: Ana, Ben, Cara with NULL, Dan with NULL, and NULL with order 103. It answers the question “show me everything, and show me where the two sides do not line up”. Use it for reconciliation reports, like matching payments to invoices and listing both the unpaid invoices and the payments with no invoice. Note that MySQL does not support FULL OUTER JOIN directly; you build the same result by combining a left join and a right join with UNION. If you run MySQL or PostgreSQL on a small server, the tuning notes in MySQL vs PostgreSQL on a VPS are worth reading before you run heavy joins in production.

How to pick the right join

Ask one question before you write the query: which rows must survive even when there is no match? If the answer is none, use an inner join. If the answer is the rows from one table, use a left join with that table on the left. If the answer is both sides, use a full outer join. Write the question down as a comment above the query, because six months from now the next person reading it will thank you. And always check the row count after a join. If a query that should return four customers returns twelve, a join multiplied your rows, and the fix is usually a more precise join condition or an aggregation step, not a DISTINCT pasted on top.

Mistakes that return wrong rows

The most common join mistake is joining on the wrong column, which silently produces nonsense instead of an error. The second is forgetting that a join can duplicate rows, which inflates sums and counts in reports. The third is filtering in the WHERE clause on a column from the right side of a left join, which turns it back into an inner join without any warning: a condition like WHERE orders.id IS NOT NULL removes the very NULL rows the left join was meant to keep. Put conditions on the right table inside the ON clause when you want to keep the unmatched rows. Before running joins against real data, make sure your database itself is set up properly; installing PostgreSQL on Ubuntu and securing it covers the setup that these queries assume. And remember that join conditions built from user input need the same care as any other input, which is why sanitizing user input to prevent SQL injection matters even when the query looks harmless.

Joins stop being mysterious once you trace them through a tiny example. Inner keeps the matches, left keeps the left side, right keeps the right side, and full keeps everything. Pick the rule that matches your question, check the row count, and the diagrams will finally agree with the results.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.