Learn · SQL · Core
SQL Joins
Databases split data across tables to avoid repeating it. A JOIN is how you stitch those tables back together to answer a real question — like “which customer placed this order?”
Before we start
- What relational data is: tables, rows, keys
- What a JOIN does and the 4 types you must know
- Primary keys vs foreign keys
- How to combine two tables into one answer
- Write an INNER and a LEFT join and know the difference
- Explain how two tables relate through keys
- Read a query that joins customers and orders
Why you’re learning it: joins are the single most-asked SQL topic, and every backend query touches them. Siemens tests DBMS basics directly. ⏱️ ~30 min.
First — tables and keys
A table is a grid: rows (records) and columns (fields). Instead of storing a customer’s name on every order, we keep customers in one table and orders in another, and link them by an id.
- Primary key: a column that uniquely identifies each row (e.g.
customer_id). No two rows share it. - Foreign key: a column in one table that points to another table’s primary key (orders.customer_id → customers.customer_id).
What a JOIN does
Two tables — customers and their orders:
| customer_id | name |
|---|---|
| 1 | Darshan |
| 2 | Amit |
| order_id | customer_id |
|---|---|
| 101 | 1 |
| 102 | 1 |
Join them on customer_id and you get a combined row per match: “order 101 → Darshan”, “order 102 → Darshan”. That’s essentially a VLOOKUP, done properly.
SELECT c.name, o.order_id FROM customers c JOIN orders o ON o.customer_id = c.customer_id;
The 4 join types
| Type | Returns |
|---|---|
| INNER JOIN | Only rows that match in BOTH tables (the overlap) |
| LEFT JOIN | ALL left rows + matches from the right (nulls where none) |
| RIGHT JOIN | ALL right rows + matches from the left |
| FULL JOIN | All rows from both sides, matched where possible |
Interview gold: LEFT JOIN is how you find “customers with no orders” — join, then filter where the right side is null.
Where you’ll use it — real life
“Orders with customer names”, “payments with merchant details” — all joins.
Joining a bank feed to your ledger on a reference id is your Recon project in one query.
A screen showing data from multiple tables is almost always a join underneath.
LEFT JOIN + “where null” finds missing/unmatched records — the core of auditing.
Now YOU do the reps
Out loud: “What’s the difference between an INNER and a LEFT join, and how would I find customers with no orders?” Then log it in your Journal.
Next: SQL Indexes →