SELECT
*
FROM
product p
LEFT JOIN variation v ON p.id = v.product_id
ORDER BY COALESCE(v.price, p.price)
COALESCE()
returns the first of its parameters which isn't NULL
. The LEFT JOIN returns everything from product table, if there's no match on variation table, there's NULL
.