 <p>You want an outer join to get all rows from table_1 and the matching ones from table2</p> <pre><code>select t1.id, t1.val - coalesce(t2.val, 0) as result from table_1 t1 left join table_2 t2 on t1.id = t2.id; </code></pre> <p>The <code>coalesce(t2.val, 0)</code> is necessary because the outer join will return <code>null</code> for those rows where no id exists in table_2 but <code>t1.val - null</code> would yield <code>null</code></p>