Gomaa Intelligence.Data Hub
عربي

Foundations › SQL › Combining Tables

🔗 Combining Tables

JOIN: Combining Tables (INNER and LEFT)

Lesson 12 / 14· 11 min

Data is split across tables so we need to bring it together in one result. JOIN links the rows of two tables through a shared column (a foreign key and its primary key).

INNER JOIN

AB
▶ Try it yourself

Each order with its customer's name. The condition after ON names the matching columns; o and c are table aliases. Open in SQL Lab

LEFT JOIN

AB

LEFT JOIN returns every row of the left table even without a match, filling the right side with NULL. Very useful for finding what is missing.

▶ Try it yourself

All customers with their order count, including zero. Open in SQL Lab

▶ Try it yourself

Customers who never ordered: a row with no match on the right. Open in SQL Lab

JOIN with aggregation

▶ Try it yourself

Revenue per category: join items to products, then aggregate. Open in SQL Lab

For advanced readers How the database executes a join

The database picks a join algorithm (nested loop, hash join or merge join) based on sizes and indexes. An index on the join column (the foreign key) speeds queries a lot. Use EXPLAIN to see the real plan.

Exercise 1 — Show the order id and customer name for the first 5 orders by id: columns id and name.

Exercise 2 — How many customers have no orders at all?

Exercise 3 — Show revenue per product category: category and revenue (quantity × unit_price).

Summary
  • JOIN links tables through a shared column.
  • INNER shows matches only; LEFT keeps every left row.
  • LEFT JOIN + IS NULL finds what is missing.

Want an expert to do it for you?

Gomaa Intelligence turns your data into clear reports and decisions. Tell us your problem and we will reply.

Get your data analyzed by an expert ↗