liveBook · Manning

Join

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

A join operation combines rows from related database tables according to a matching relationship between them.

A join brings related rows together when useful information is split across tables. Instead of storing customer, address, order, product, and item details redundantly, a query follows their relationships and returns the combined view an application needs. section 10.3.4

A practical join follows a small set of decisions:

  • Start with a table and give it an alias, such as customer c, to keep a multi-table query readable. section 10.3.4
  • Use JOIN to connect tables through a primary key and a related foreign key. section 10.3.4
  • Put the matching rule after ON; rows qualify when the key values satisfy that condition. section 10.3.4
  • Add GROUP BY when the result should aggregate identical values, then ORDER BY when presentation order matters. section 10.3.4

From orders to invoice data

A simple customer-address join can count how often each address was used. A larger query joins order, customer, address, item, and product, returning each order line with the customer, delivery address, product name, and quantity. This reconstructs the detailed data needed for invoices without requiring every table to repeat the same information. section 10.3.4

SELECT c.name, a.street, a.zipcode, COUNT(c.name) AS 'Times Used'
FROM CUSTOMER c
JOIN 'order' o ON o.customer_id = c.customer_id 
JOIN address a ON a.address_id = o.address_id
GROUP BY a.street
ORDER BY c.name;
 
name        street      zipcode     Times Used
----------  ----------  ----------  ----------
Joe         12 Main St  12345       2
Mary        127 Margol  40322       1
Mary        41 Orange   40321       1
Sue         212 Grove   34213       1

Inspect a complete customer-address join with aliases, key conditions, grouping, sorting, and returned counts. section 10.3.4

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Explains aliases, key matching, ON conditions, grouping, and multi-table reconstruction, without detailing join variants or query optimization.
Examples
Uses customer-address counts, invoice reconstruction, and a blog post query joined with user information.
What it assumes
Assumes familiarity with SQL tables and key relationships, while demonstrating how primary and foreign keys connect rows.
What you can do after
Apply it via SQL to retrieve related records and assemble application views from normalized tables.
How it connects
Ties joins to SQL, relational tables, primary keys, foreign keys, GROUP BY, ORDER BY, and database queries.

Also explained in

Compare how 16 books explain join →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.