liveBook · Manning

Join condition

As explained in Learn SQL in a Month of Lunches

A join condition, or join predicate, is a Boolean condition that specifies how rows from joined tables correspond to one another.

A join condition, also called a join predicate, is the Boolean condition that tells SQL how rows from two joined tables correspond. It usually appears after ON, such as matching each order to its customer with oh.CustomerID = c.CustomerID. section 8.6

Match orders to a particular customer

SELECT oh.OrderID FROM orderheader oh JOIN customer c ON oh.CustomerID = c.CustomerID WHERE c.FirstName = 'Chris' AND c.LastName = 'Dixon'; The join condition connects each order to its customer. The WHERE condition then keeps only Chris Dixon’s rows. With several tables, each JOIN can have its own condition—for example, connecting order headers to order items and titles through OrderID and TitleID. section 8.6

With an outer join, place a condition carefully. Filtering a right-table value in WHERE can remove unmatched rows and make a LEFT OUTER JOIN behave like an INNER JOIN. Put the right-table filter in ON when you want every left-table row retained, with either the matching value or NULL. section 9.2.1

SELECT
    oh.OrderID,
    p.PromotionCode
FROM orderheader oh
LEFT OUTER JOIN promotion p
    ON oh.PromotionID = p.PromotionID
    AND p.PromotionCode = '2OFF2015';

See the join condition that retains every order while restricting the promotion match to 2OFF2015. section 9.2.1

How Learn SQL in a Month of Lunches covers it

How deep it goes
Explains equality-based matching and demonstrates how moving a right-table filter between ON and WHERE changes outer-join results.
Examples
Demonstrates customer orders, order items and titles, and promotion matching with preserved unmatched orders.
What you can do after
Apply via SQL by writing JOIN conditions and placing filters to preserve or exclude unmatched outer-join rows deliberately.
How it connects
Connects join predicates with inner joins, outer joins, filtering predicates, table aliases, and multi-table queries.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn SQL in a Month of Lunches. The section links point to the parts of the book it draws on.