Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

SQL
SQL
0/12 completed
Beginner
What is a database?SELECT: reading dataFiltering with WHERESorting and limiting results
Intermediate
Aggregates and GROUP BYJOINs: combining tablesINSERT, UPDATE and DELETECreating tables and constraints
Advanced
Subqueries and CTEsWindow functions
Pro
Indexes and transactionsDatabase design and normalization
Tracks / SQL / Intermediate

JOINs: combining tables

Good database design splits data into separate tables so nothing is duplicated: customers in one, products in another, orders linking them. A JOIN puts that data back together in a query.

INNER JOIN

Returns rows where there's a match in both tables.

SQL · editable
  • o and c are table aliases: short names that keep queries readable.
  • ON says how the rows relate: the order's customer_id equals the customer's id.
  • JOIN on its own means INNER JOIN.

Joining three tables

SQL · editable

LEFT JOIN

Returns every row from the left table, plus matching rows from the right. Where there's no match, the right side's columns are NULL.

SQL · editable

Notice Fatima has 0 orders. An inner join would have dropped her completely.

Finding rows with no match

A classic pattern: LEFT JOIN … WHERE right.id IS NULL.

SQL · editable

Join types at a glance

Join Returns
INNER JOIN Only matching rows from both sides
LEFT JOIN All left rows + matches (NULLs where none)
RIGHT JOIN All right rows + matches
FULL OUTER JOIN All rows from both sides
CROSS JOIN Every combination (rows × rows)

Self join

A table can be joined to itself, for example to find customers from the same city:

SQL · editable

Joins + aggregates = reports

SQL · editable

Practice

  1. List each customer's name with the total amount they've spent, highest first.
  2. Find customers who have never ordered anything.
SQL · editable
✎ Check your understanding

1.You want every customer, even those with no orders. Which join?

2.What does ON c.id = o.customer_id do?

← Aggregates and GROUP BY