SQL reference

SQL JOIN: combine two tables (like VLOOKUP)

In Excel, you'd reach for
fxVLOOKUP or XLOOKUP across two sheets

Business data is split across tables: orders in one, products in another, customers in a third. A JOIN puts them side by side by matching a shared ID, the way VLOOKUP pulls a value from another sheet. The difference is that a join brings back every column you ask for in one go.

The ON part is the matching rule. Below, each order's customer_id is matched to the id in customers, so every order row gains that customer's name and country.

SQL joins Venn diagram: INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL OUTER JOIN, each with its query
The four SQL joins as Venn diagrams, each with its query. Download · Save to Pinterest

Syntax

SELECT a.col, b.col
FROM table_a a
JOIN table_b b ON b.id = a.b_id;
Try itEdit the query, then Run
order_idorder_datenamecountry
12026-01-01Umar ShahCanada
22026-01-01Leo BrownUnited States
32026-01-01Quinn ShahIndia
42026-01-02Umar TanakaNigeria
52026-01-02Chen NovakGermany
62026-01-02Chen OkaforCanada
72026-01-03Aisha BrownUnited States
82026-01-04Ximena MüllerUnited States
92026-01-04Dana HaddadUnited States
102026-01-04Pedro BrownUnited States

10 rows

The four kinds of join

  • JOIN (also written INNER JOIN): only rows that match on both sides. Unmatched rows disappear.
  • LEFT JOIN: every row from the first table, with empty values where nothing matched. This is VLOOKUP keeping the #N/A rows.
  • RIGHT JOIN: the mirror image. Most people just swap the tables and use LEFT JOIN.
  • FULL OUTER JOIN: everything from both sides, matched where possible.

Joining more than two tables is just more JOIN lines, one per table.

Your turn

Show each order's id, the product name and the quantity, by joining orders to products.

This page on one card

SQL JOIN: combine two tables (like VLOOKUP): the Excel way (VLOOKUP or XLOOKUP across two sheets) and the SQL query, side by side
SQL JOIN: combine two tables (like VLOOKUP), on one card: the Excel way and the SQL. Download · Save to Pinterest
The practice data (7 tables)

See every column and sample rows