One-to-Many

How a one-to-many relationship links many child rows to one parent row through a foreign key, like a customer and their orders.

Apprentice · SQL · Database · Updated

By

The One-to-Many relationship is the most common type of relationship in a database. In a One-to-Many relationship, a row in the parent table can be related to one or more rows in the child table. The relationship is created by having the foreign key in a child table refer to the primary key of the parent table.

For example, a customer can place many orders. The Customer table is the parent table and the Order table is the child.

A Customer can have many Orders

Do it: find one customer's orders

Around the Horn has phoned to ask for a list of their orders. An order's row doesn't hold the customer's name: it holds the customer's CustomerId, a foreign key that points at the customer's row in Customers. One customer, many orders.

The two tables (the Tables tab shows the SQL that makes them):

Customers, the parent table:

CustomerId CompanyName
1 Alfreds Futterkiste
2 Around the Horn
3 Bon app'

Orders, the child table. Its CustomerId is the foreign key:

OrderId CustomerId OrderDate
10248 1 2026-03-02
10249 2 2026-03-03
10250 3 2026-03-03
10251 2 2026-03-05
10252 1 2026-03-06
10253 2 2026-03-09

Working it through: in Customers, Around the Horn's CustomerId is 2. In Orders, the rows whose CustomerId is 2 are theirs. So your query should give exactly these rows, in this order:

OrderId OrderDate
10249 2026-03-03
10251 2026-03-05
10253 2026-03-09

Your job: write one SELECT query on Orders that lists Around the Horn's orders.

  • Show two columns, OrderId and OrderDate, in that order.
  • Keep only the rows whose CustomerId is Around the Horn's.
  • Sort them by OrderId, smallest first.

Practiced live on the lesson page: write the SQL right there and check it.

Key terms

Cheat sheets