Back to List

Relational Databases β€” JOIN and Normalization

Understand why tables are split, how to combine them with JOIN, and why normalization is necessary through practical examples.

Intermediate
|
8min
|
Verified (2026-07)
Progress0/55 (0%)

Relational Databases β€” JOIN and Normalization

After completing this topic, you will be able to:

Explain why tables should be separated and write SQL to combine data using JOIN.


One Table is Not Enough

Let's say we're building an online shopping mall. What happens if we put all the order information into a single table?

text
orders (all information in one table):
+----+--------+-------+----------+--------+
| id | customer_name | email | product | price |
+----+--------+-------+----------+--------+
| 1 | Kim Hoon | h@ex | laptop | 1.5M |
| 2 | Kim Hoon | h@ex | mouse | 50K |
| 3 | Lee Soo | s@ex | keyboard | 100K |
+----+--------+-------+----------+--------+

Can you see the problems?

  • Redundancy: "Kim Hoon" and "h@ex" are repeated every time they place an order.
  • Risk of Modification: If Kim Hoon changes his email address, you have to update all rows. If you miss one, there will be inconsistencies.
  • Risk of Deletion: If you delete all of Kim Hoon's orders, his customer information will be lost.

Table Separation β€” The Basics of Normalization

The solution is to separate related data into separate tables.

text
users table:                   orders table:
+----+--------+-------+         +----+---------+----------+--------+
| id | name | email |         | id | user_id | product | price |
+----+--------+-------+         +----+---------+----------+--------+
| 1 | Kim Hoon | h@ex |         | 1 | 1 | laptop | 1.5M |
| 2 | Lee Soo | s@ex |         | 2 | 1 | mouse | 50K |
+----+--------+-------+         | 3 | 2 | keyboard | 100K |
                                 +----+---------+----------+--------+

orders.user_id refers to users.id. This is a Foreign Key β€” the link that connects the two tables.

The process of removing redundancy and separating tables is called Normalization.


JOIN β€” Combining Separated Tables

Now that we've separated the tables, we need a way to combine them again. That's where JOIN comes in.

sql
-- Retrieve the order list and attach the customer name
SELECT users.name, orders.product, orders.price
FROM orders
JOIN users ON orders.user_id = users.id;

Result:

text
+--------+----------+--------+
| name | product | price |
+--------+----------+--------+
| Kim Hoon | laptop | 1.5M |
| Kim Hoon | mouse | 50K |
| Lee Soo | keyboard | 100K |
+--------+----------+--------+

In JOIN ... ON, you specify which column to match on. orders.user_id = users.id β€” it combines rows where the order's user_id is the same as the user's id.


Types of JOIN

sql
-- INNER JOIN: Only rows that match on both sides (default)
SELECT * FROM orders JOIN users ON orders.user_id = users.id;

-- LEFT JOIN: All rows from the left table, and matching rows from the right
-- Includes customers who have not placed an order
SELECT users.name, orders.product
FROM users
LEFT JOIN orders ON users.id = orders.user_id;

-- Result: Customers with no orders will have product as NULL
JOIN TypeDescription
INNER JOINOnly includes rows that exist in both tables
LEFT JOINIncludes all rows from the left table, plus matching rows from the right
RIGHT JOINIncludes all rows from the right table, plus matching rows from the left

In practice, INNER JOIN and LEFT JOIN are used most often.


Normalization β€” Why Separate?

The core principle of normalization is simple: Each fact should be stored in one place only.

Before NormalizationAfter NormalizationBenefit
Customer name is repeated for each orderCustomer name is stored once in the users tableEliminates redundancy
Email change requires updating N rowsOnly one row needs to be updatedEnsures consistency
Deleting an order deletes the customerCustomer exists independentlyPreserves data

Normalization is not "always good" β€” if tables are split too much, JOINs can become complex and performance can degrade. In practice, redundancy is sometimes intentionally allowed for performance reasons (denormalization). However, the foundation is normalization.


πŸ’¬ Questions & Comments

0 comments

You can post without signing in. Guest comments cannot be edited or deleted by their author.

0/2000

Loading...