Your manager drops a database on your desk and says: "We need a report showing which products are selling and which customers are buying them." You open the database and find six tables. Without an ER diagram, you'd have to guess which columns connect orders to customers, or whether order_items links to products at all. An ER diagram answers those questions before you write a single line of SQL.
What an ER Diagram Actually Shows
An ER diagram is a picture of a database schema. Each box in the diagram represents a table. Inside each box are the column names. The lines between boxes show how the tables relate to each other.
Every table in a well-designed database has a primary key: a column whose value is unique for every row. In the customers table, that column is customer_id. No two customers share the same customer_id. That uniqueness is what makes the column useful as an identifier.
When another table needs to refer to a customer, it stores that customer_id as a foreign key. The orders table has a customer_id column. That column doesn't describe the order itself; it points back to the customers table. The line between customers and orders in an ER diagram represents exactly this relationship.
In the ecommerce schema, the relationships look like this:
| Table | Primary key | Foreign keys |
|---|---|---|
customers | customer_id | none |
products | product_id | category_id |
employees | employee_id | none |
orders | order_id | customer_id, employee_id |
order_items | item_id | order_id, product_id |
payments | payment_id | order_id |
The orders table sits in the middle. It connects to customers (who placed the order), employees (who handled it), order_items (what was in it), and payments (how it was paid for). Understanding that position tells you which table to start from when you're writing a query that spans multiple tables.
Types of Relationships
ER diagrams use specific notation to show how many rows on each side of a relationship can exist. The three types you'll see most often are:
One-to-many. One row in the first table connects to many rows in the second. A single customer can place many orders, but each order belongs to exactly one customer. This is the most common relationship type in relational databases.
One-to-one. One row connects to exactly one other row. Less common in practice. You might store sensitive employee data in a separate table and link each employee to exactly one record there.
Many-to-many. Many rows on both sides can connect. A product can appear in many orders, and an order can contain many products. Databases handle this with a junction table. In the ecommerce schema, order_items is that junction table. It sits between orders and products and holds one row for each product in each order.
Reading the ecommerce.db Schema as an ER Diagram
Here's how the ecommerce schema maps out as relationships:
customersone-to-manyorders: one customer, many ordersemployeesone-to-manyorders: one employee processes many ordersordersone-to-manyorder_items: one order contains many line itemsproductsone-to-manyorder_items: one product appears in many order itemsordersone-to-manypayments: one order can have more than one payment record
When you want to know which customer placed which order, you join customers and orders on customer_id. That join works because orders.customer_id is a foreign key pointing to customers.customer_id.
SELECT c.first_name, c.last_name, o.order_id, o.total_amount
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
The ON clause in a JOIN is almost always a primary key matched to a foreign key. The ER diagram tells you exactly which columns to use.
When you need to trace a purchase all the way from customer to product, you follow the chain of relationships:
SELECT c.first_name, p.product_name, oi.quantity
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
JOIN order_items oi ON o.order_id = oi.order_id
JOIN products p ON oi.product_id = p.product_id
Each JOIN follows a line in the ER diagram. You're not guessing. You're reading the map.
How ER Diagrams Connect to Other Concepts
Once you can read an ER diagram, [JOINs](sqlround.com/lessons/sql-joins) become much more predictable. You already know which column connects which tables before you write anything.
[Primary and foreign keys](sqlround.com/lessons/sql-primary-foreign-keys) are the mechanism that makes the relationships in an ER diagram work at the database level. An ER diagram is the visual layer; keys are the technical implementation underneath.
Common Mistakes
1. Joining on the wrong column
Joining two tables on a column that looks similar but means something different gives you wrong results without an error message.
-- wrong
SELECT o.order_id, oi.quantity
FROM orders o
JOIN order_items oi ON o.order_id = oi.item_id
Check the ER diagram. orders and order_items connect on order_id, not item_id.
-- correct
SELECT o.order_id, oi.quantity
FROM orders o
JOIN order_items oi ON o.order_id = oi.order_id
2. Skipping the junction table
Trying to join orders directly to products fails because there is no direct foreign key between them. order_items is the bridge.
-- wrong
SELECT o.order_id, p.product_name
FROM orders o
JOIN products p ON o.order_id = p.product_id
Always go through the junction table when the ER diagram shows no direct line between two tables.
-- correct
SELECT o.order_id, p.product_name
FROM orders o
JOIN order_items oi ON o.order_id = oi.order_id
JOIN products p ON oi.product_id = p.product_id
3. Assuming any shared column name is a join key
Both orders and payments have an amount column, but that doesn't mean you join on it. You join on order_id, which is the actual foreign key.
-- wrong
SELECT o.order_id, pay.method
FROM orders o
JOIN payments pay ON o.total_amount = pay.amount
The ER diagram shows the correct key clearly.
-- correct
SELECT o.order_id, pay.method
FROM orders o
JOIN payments pay ON o.order_id = pay.order_id
Quick Recap
- An ER diagram maps every table, column, and relationship in a database visually.
- A primary key uniquely identifies each row in its table.
- A foreign key in one table points to the primary key of another table, creating a relationship.
- One-to-many is the most common relationship type: one parent row, many child rows.
- Junction tables like
order_itemsresolve many-to-many relationships between two other tables. - Reading an ER diagram before writing a query tells you exactly which columns to join on and in what order.
employee_id, first_name, last_name, and department of every employee in the employees table.employees.SELECT e.employee_id, e.first_name, e.last_name, e.department
FROM employees e
payment_id, amount, and method for every payment, along with the order_id it belongs to.payments table. No join needed.SELECT pay.payment_id, pay.order_id, pay.amount, pay.method
FROM payments pay
first_name and last_name of each employee alongside every order_id they handled, using the foreign key relationship between orders and employees.employees and orders on employee_id. That column is the foreign key in orders pointing to the primary key in employees.SELECT e.first_name, e.last_name, o.order_id
FROM employees e
JOIN orders o ON e.employee_id = o.employee_id
order_id, product_id, and quantity from order_items, but only for orders where quantity is greater than 2, sorted by quantity descending.oi.quantity, then sort with ORDER BY.SELECT oi.order_id, oi.product_id, oi.quantity
FROM order_items oi
WHERE oi.quantity > 2
ORDER BY oi.quantity DESC
department from the employees table along with the number of employees in that department, using the column alias employee_count.department column and use COUNT(*) with an alias.SELECT e.department, COUNT(*) AS employee_count
FROM employees e
GROUP BY e.department
An ER diagram (entity-relationship diagram) is a visual map of a database. It shows which tables exist, what columns they have, and how the tables connect to each other through keys.
A primary key uniquely identifies each row in a table. A foreign key is a column in another table that points to that primary key, creating a link between the two tables.
Not strictly, but reading an ER diagram tells you immediately which tables to join and on which columns. It saves a lot of guessing when you work with an unfamiliar database.
It means one row in the first table can link to many rows in the second table. One customer can have many orders, for example, but each order belongs to exactly one customer.