SQL JOINs Explained for Beginners, Step by Step

Two small tables, customers and orders, joined by a matching customer id column

Key takeaways

  • A join matches rows from two tables using a shared column called a key.
  • INNER JOIN keeps only the rows that match in both tables.
  • LEFT JOIN keeps every row from the first table, even when there is no match.
  • Count the rows before and after a join to catch mistakes early.

A JOIN is how SQL answers a question that needs facts from two tables. This guide explains the idea with a tiny example, then shows the two joins beginners use most.

What is a SQL join?

A database keeps different facts in different tables. One table lists customers. Another lists orders. A join connects them through a shared column, called a key, so one result can show a customer name next to each order.

A small example

Imagine two tables with made-up names.

customer_id name
1 Ada
2 Tunde
3 Ngozi
order_id customer_id item
10 1 Notebook
11 1 Pen
12 2 Folder

The customer_id column appears in both tables. That is the key the join uses.

How does an INNER JOIN work?

An INNER JOIN keeps only the rows that match in both tables. Ngozi has no orders, so she does not appear in the result.

SELECT customers.name, orders.item
FROM customers
INNER JOIN orders ON customers.customer_id = orders.customer_id;

The result has three rows: Ada with Notebook, Ada with Pen, and Tunde with Folder.

How does a LEFT JOIN work?

A LEFT JOIN keeps every row from the first table. When the second table has no match, the columns from the second table are empty.

SELECT customers.name, orders.item
FROM customers
LEFT JOIN orders ON customers.customer_id = orders.customer_id;

Now Ngozi appears too, with an empty item. This is useful when you want a list of customers who have not ordered yet.

What other joins are there?

PostgreSQL's documentation lists five kinds. A CROSS JOIN pairs every row of one table with every row of the other. A RIGHT JOIN is the mirror image of a LEFT JOIN: it keeps every row from the second table. A FULL JOIN keeps unmatched rows from both sides and fills the gaps with empty values. As a beginner, master INNER and LEFT first, because they answer most everyday questions.

One more detail matters. A condition written in the ON clause is applied while the tables are joined, but the same condition in a WHERE clause is applied afterwards. With a LEFT JOIN the two can give different results, so put the matching rule in ON and the filtering in WHERE.

How do I check a join is correct?

Count your rows before and after. If an INNER JOIN returns more rows than the first table has, the key is probably repeated in the second table. Read the PostgreSQL guide to joins for the official description, then try the same queries on your own small tables.

Common mistakes to avoid

  • Joining on the wrong column, so unrelated rows are matched.
  • Forgetting that an INNER JOIN silently drops rows with no match.
  • Trusting a result without counting the rows.

Where can I learn this properly?

Joins are one lesson in a larger path. If you are new to the field, start with what data analytics is, then see the Data Analytics program for the full outline. You can also read our guide to online skills to learn for remote work. RemoteWay teaches skills and does not promise jobs or income.

Frequently asked questions

What is a SQL join in simple words?

A SQL join puts information from two tables side by side. You tell the database which column in each table matches, such as a customer id, and it builds new rows from the matching pairs. It is how you answer questions that need facts stored in different tables.

What is the difference between INNER JOIN and LEFT JOIN?

An INNER JOIN returns only the rows that have a match in both tables. A LEFT JOIN returns every row from the first table, and where the second table has no match it shows empty values instead. Use LEFT JOIN when you must not lose any row from the first table.

Why does my join return more rows than I expected?

Usually the matching column is not unique in one of the tables, so one row matches several rows on the other side. Count the rows in each table first, check the key for duplicates, and compare the row count after the join. If it grew, look at the duplicates before trusting the result.

Sources

  1. PostgreSQL documentation: Joins Between Tables (opens in a new tab)Accessed
  2. PostgreSQL documentation: Table Expressions (opens in a new tab)Accessed

Learn this properly

RemoteWay teaches this skill in a self-paced program with comprehensive training videos created by top experts in their fields.

See the Data Analytics program

Ready to learn this properly?

Creating an account costs nothing. Every program is self-paced, with comprehensive training videos created by top experts in their fields. We teach skills. We don't promise jobs or income.