Skip to main content

Why do you need to combine tables?

Suggest an edit

Tables get combined to pull together related data that's stored separately.

In real databases, data rarely lives in a single table - to avoid duplication and errors, data gets normalized (spread across different tables). JOIN is what "reassembles" these pieces during analysis.

Example 1: separated entities

  • The users table stores user names and ids.
  • The orders table stores orders, which only have user_id.

To see who placed which order, you need to join them:

sql
SELECT users.name, orders.amount FROM users JOIN orders ON users.id = orders.user_id;

Without JOIN, you can't link a person's name to their order.

Example 2: saving space and cleaner logic

Instead of storing the customer's name in every order row, the orders table only holds user_id. This makes the database faster, more compact, and safer, and you don't need to duplicate data.

Example 3: multi-layer data

Sometimes data is related as a chain: users -> orders -> products -> categories. JOIN lets you walk those relations and roll everything into one report:

sql
SELECT users.name, products.title, categories.name FROM users JOIN orders ON users.id = orders.user_id JOIN products ON orders.product_id = products.id JOIN categories ON products.category_id = categories.id;

You get the full picture: who bought what, and from which category.

Summary: Tables are combined to:

  • relate data from different sources (e.g. a user <-> an order);
  • avoid duplicating information;
  • build reports, analytics, and selections from several tables at once.

In short, JOIN turns scattered tables into a single logical picture of the data.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.

Suggest an edit