Overview

8 Querying multiple tables

This chapter explains what makes relational databases “relational” and why splitting data across multiple, related tables is powerful. It contrasts a single, spreadsheet-like table full of repeated values with a well-designed schema that organizes data into logical groups. By introducing primary keys and foreign keys, it shows how replacing repeated strings with compact identifiers reduces redundancy, improves consistency, and makes updates reliable. Using an order tracking example, it motivates separating customers, titles, orders, and order items, and describes common relationship types, especially one-to-many, such as customer-to-orders and order-to-items.

With the relational design in place, the chapter demonstrates how to retrieve related data using joins in the FROM clause. It introduces JOIN and ON to connect tables via matching key columns, explains these predicates alongside WHERE filters, and emphasizes two-part names (table.column) to disambiguate shared column names. It scales joins from two tables to several (orderheader, customer, orderitem, title) and notes that join order in the query is often about readability as long as relationships are correctly expressed. To improve clarity, it advocates concise table aliases (for example, oh, oi, c, t) and offers simple rules for picking sensible, short alias names.

Finally, the chapter presents the older, comma-and-WHERE join style as something you may encounter but should generally avoid. While syntactically valid, pushing all join conditions into WHERE harms readability, complicates troubleshooting, and restricts you to a single join type, whereas explicit JOINs are clearer and more flexible. The chapter closes by encouraging practice with multi-table queries, mindful use of predicates, and consistent aliasing, since joins will be central to most real-world SQL.

The first ten rows in the orderitem table, which includes the column “TitleID”
shows our hypothetical table used to track orders that contains order date, title name, price, and customer name.
The results for TitleID and TitleName for the titles “Pride and Predicates” and “The Join Luck Club.
shows what it would look like if we replaced TitleName with TitleID in our hypothetical order table.
The results from the CustomerID, FirstName, and LastName for customers with the name “Chris Dixon” or “David Power”
shows our hypothetical order table with a CustomerID column to reference names in the customer table.
The results of joining the orderheader and customer tables to show CustomerID and customer name values for the first order
Customer information from the customer table, and the price of the item ordered in the first order from the orderitem table
Customer information from the customer table, the price of the item ordered in the first order from the orderitem table, and the title name from the title table

Lab answers

  1. The results will be the same. Since our query is matching all OrderID values from the orderheader to the orderitem table, the OrderID column from either table can be used in the filtering condition to return the same results.
  2. Chris Dixon placed 3 orders. You could use a query like this to get the results:
  1. Eight customers placed an order in 2015:
  2. Chris Dixon
    David Power
    Arnold Hinchcliffe
    Keanu O’Ward
    Lisa Rosenqvist
    Maggie Ilott
    Cora Daly
    Dan Wilson
    You could find them with a query like this:
  1. You could write a query like this using JOINs and aliases:
  1. The query executes successfully with the same result set, but as noted in this chapter, we generally try to avoid combining filtering predicates with join predicates for the sake of readability.

FAQ

What makes a database management system “relational”?It stores data in separate, logical tables that relate to each other. Those relationships let you retrieve related data from multiple tables with a single query by joining them on shared values.
Why split data across multiple tables instead of one big table?To reduce redundancy, save storage, prevent inconsistencies, and make updates easier. For example, storing a book’s TitleName once in a title table and referencing it by TitleID elsewhere avoids repeated long strings and typos.
What are primary keys and foreign keys?A primary key uniquely identifies each row in a table (for example, title.TitleID). A foreign key is a column in another table that stores those key values to represent the relationship (for example, orderitem.TitleID referencing title.TitleID).
What is a one-to-many relationship?One row in a “parent” table relates to many rows in a “child” table. Examples: customer → orderheader (one customer, many orders) and orderheader → orderitem (one order, many items).
Why replace names with numeric IDs?Numeric IDs take less space, speed queries, and centralize the authoritative value. If a name changes or was entered incorrectly, you update it once in its table and all references remain consistent via the ID.
How do I join two tables in SQL?Use JOIN ... ON in the FROM clause to state the tables and the matching columns. Example: FROM orderheader JOIN customer ON orderheader.CustomerID = customer.CustomerID. The ON part is a predicate that evaluates matching rows.
Why are two-part names (table.column) important when joining?They disambiguate columns that exist in multiple tables (like CustomerID). Without two-part names you can get “ambiguous column” errors and make the query harder to read.
Can I join more than two tables? Does join order matter?Yes, you can chain multiple JOINs. With standard inner joins, the order of JOINs generally doesn’t change the result; choose an order that’s readable and ensure you’ve declared tables before referencing them in an ON clause.
What are table aliases and how should I use them?Aliases are short names for tables (for example, customer as c, orderheader as oh, orderitem as oi, title as t). They make queries shorter and clearer. Aliases must start with a letter and can include numbers (no special characters). Pick aliases that resemble the table names.
What’s the comma-and-WHERE join style, and why is it discouraged?It lists tables comma-separated in FROM and puts join conditions in WHERE. While valid, it’s harder to read and troubleshoot and limits you to certain join types. Prefer explicit JOIN ... ON for clarity and flexibility. Put relationship conditions in ON and row filters in WHERE; for inner joins these placements often yield the same result, but with other join types they can differ.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn SQL in a Month of Lunches ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn SQL in a Month of Lunches ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn SQL in a Month of Lunches ebook for free