An INNER JOIN can add customer fields first, then WHERE filters the joined rows to one customer segment.

highlighted = computed this step

Orders input

Start with orders that carry customer keys.

Orders\text{Orders}
Ordersorder_idcustomer_idamounto1c1120o2c280o3c_missing50o4NULL40

Customers input

Customers maps matching customer keys to segments.

Customers\text{Customers}
Customerscustomer_idsegmentc1retailc2wholesale

Join query

INNER JOIN adds customer segment for matching keys, then WHERE keeps retail rows.

JOIN then WHERE segment = retail\text{JOIN then WHERE segment = retail}
INNER JOIN customers then WHERE segment = retailwhere segment eq retailarity 5 rows 1join customer_id=customer_idarity 5 rows 2Ordersarity 3 rows 4Customersarity 2 rows 2

Joined output

Only the joined retail order remains.

retail joined rows\text{retail joined rows}
Joined outputorder_idcustomer_idamountCustomers.customer_idsegmento1c1120c1retail