Concepts / Designing Normalized Database Schemas

Designing Normalized Database Schemas

JOIN and ON clauses reconstruct data across multiple tables by following relational connections between foreign keys and primary keys.

  • Programming

From Separate Tables to Complete Context

A normalized database stores related facts in separate tables rather than placing every detail in one repeated table. That separation is useful only if the database can reconstruct the relationships when you need to read the data. JOIN and ON clauses perform that reconstruction by following connections between foreign keys and primary keys.

The central idea is simple: a primary key identifies a row in its own table, while a foreign key stores a primary-key value from a related table. In the Album table, album id identifies an album, and artist_id points to the related row in the Artist table. The foreign key is the bridge that lets a query recover the artist associated with an album.

foreign keymatches primary keyArtistid: 7Matching key valuesAlbum.artist_id = Artist.idAlbumartist_id: 7
How does a foreign-key value in one table connect to the matching primary-key row in another table?

Following the Matching Keys

A JOIN combines rows from two tables. The ON clause tells the database which rows are related. In the common case, the condition compares a foreign key in one table with a primary key in another table. Only rows satisfying that condition are combined.

Suppose an Album row contains artist_id equal to 7. The query compares that value with the id values in Artist. The Artist row whose id is 7 supplies the artist columns for the combined result. The database is not guessing based on text; it is following the explicit key comparison in the ON condition.

query beginsadd tabletest relationshipmatch foundSELECT columnschoose output fieldsAlbumstart tableJOIN Artistbring in related rowsKey comparisonAlbum.artist_id = Artist.idCombined rowalbum and artist columns
What happens step by step when a JOIN uses an ON condition to find matching rows and combine their columns?
thenthenmatch withSELECT columnsfields to returnFROM table1starting tableJOIN table2related tableON conditionforeign key = primary key
What role does each part of a JOIN query play?

One Artist, Many Albums

A one-to-many relationship means that one record in a parent table can connect to multiple records in a child table. Artist is the parent table and Album is the child table in this example. Each album points to one artist through its artist_id foreign key, while one artist can be referenced by multiple album rows.

This relationship determines the shape of the JOIN result. If one artist is connected to three albums, the artist's information can appear in three result rows: one row for each connected album. The repeated artist values do not mean that three artist records were stored. They mean that the one parent row was combined separately with each matching child row.

connected toconnected toconnected tocombinedcombinedcombinedArtist 7one parent rowAlbum 21artist_id: 7Result row 1Artist 7 + Album 21Album 22artist_id: 7Result row 2Artist 7 + Album 22Album 23artist_id: 7Result row 3Artist 7 + Album 23
Why does one row in a parent table produce multiple result rows when it matches several child rows?

Tracing an Artist Through Three Albums

An Artist row has id 7. Three Album rows have artist_id values of 7. How many combined rows can a JOIN produce for that artist?

Find the parent row: Locate the Artist row whose primary key is 7.

Find matching child rows: Compare Album.artist_id with Artist.id. Each Album row whose artist_id is 7 matches the parent row.

Form the result rows: Combine the artist columns with each matching album row separately.

The JOIN produces three result rows for that artist, one for each matching Album row.

Writing the Reconstruction Query

The basic JOIN pattern selects the columns you want, names a starting table, names a related table with JOIN, and states the relationship with ON. The ON condition should express the key comparison that connects the tables.

sql

In this generated example, SELECT requests artist and album columns. FROM identifies Artist as the starting table. JOIN adds Album. ON states that an album belongs with the artist whose primary-key value equals the album's foreign-key value. Because one artist can have many albums, the result can contain several rows with the same artist information.

Output
The result contains one row for each matching Artist and Album pair.

Reconstructing a Three-Level Relationship

The same reasoning extends across more than two tables. Consider Artist, Album, and Track. One artist has many albums, and one album has many tracks. A query that joins all three tables follows both relationships. Each result row represents one track, while also carrying the album and artist context connected to that track.

sql

The first ON condition connects Album to Artist. The second connects Track to Album. The result is reconstructed progressively: a track is connected to its album, and that album is connected to its artist. This is why the final row can contain the full context of the track even though the underlying information remains distributed across normalized tables.

Artist.id matches Album.artist_idAlbum.id matches Track.album_idtrack supplies result rowArtistartist rowTrack result rowartist + album + trackcolumnsAlbumalbum row with artist_idTracktrack row with album_id
How do separately stored normalized tables become a combined, readable result set without duplicating the underlying data?

Avoiding Broken Relationships

  • Treating a one-to-many JOIN as if it must return one row per parent.

    A one-to-many relationship produces one result row for each connected child record.

    Fix: Trace the child table and count the matching foreign-key values. Multiple matching child rows produce multiple result rows.

  • Matching unrelated columns instead of the foreign key and primary key.

    The ON condition is supposed to identify related rows through the defined key relationship.

    Fix: Compare the foreign-key column with the corresponding primary-key column.

  • Assuming a foreign key identifies a unique child row.

    Each child row points to one parent, but multiple child rows can point to the same parent.

    Fix: Distinguish the direction of the relationship: one Artist can have many Albums.

  • Ignoring primary-key uniqueness and foreign-key indexes when designing related tables.

    Primary-key uniqueness is essential for correct JOIN behavior, and without foreign-key indexes, matching can require scanning the entire table and become very slow.

    Fix: Use uniqueness constraints on primary keys and indexes on foreign keys.

Practice the Row Trace

EASY

Imagine one Artist row with id 4. Two Album rows have artist_id equal to 4. Write the JOIN condition that connects Album to Artist, then state how many result rows the JOIN produces for that artist.

Hints
  • Put the foreign-key column and primary-key column on opposite sides of the equality in ON.
  • Count the matching child rows, not just the parent rows.
MEDIUM

Now extend the trace to Artist, Album, and Track. Identify the two relationships that the query must follow to produce a result row containing artist, album, and track information.

Hints
  • First connect Album.artist_id to Artist.id.
  • Then connect Track.album_id to Album.id.

The Reconstruction Pattern

  1. A primary key uniquely identifies a row in its table, while a foreign key stores a related primary-key value.
  2. JOIN combines rows from tables, and ON states which key values must match.
  3. A one-to-many relationship produces one result row for each matching child record.
  4. Joining Artist, Album, and Track follows two relationships to reconstruct the full context of each track.
  5. Primary-key uniqueness supports correct matching, and foreign-key indexes help JOIN queries locate matches efficiently.

Key Takeaways

  • JOIN and ON clauses reconstruct related information from normalized tables.
  • The ON condition normally matches a foreign key with a primary key.
  • One parent row can produce multiple result rows when several child rows reference it.
  • Multi-table JOINs follow each relationship in sequence to restore broader context.
  • Primary-key uniqueness and foreign-key indexes are important for correct and efficient JOIN behavior.