For the second one, it seems most natural to reach for <i>exists</i>, or something (I have not tried this code..)<p><pre><code> select
node
, (case when parent is null then 'Root'
when exists (
select * from tree c
where c.parent = node
) then 'Inner'
else 'Leaf'
end) "label"
from tree
</code></pre>
EDIT: also, in the fourth, it seems like you'd want to <i>partition</i> the window function, who cares about order. Something like<p><pre><code> sum(cash_flow) over (partition by date) "cumulative_cf"</code></pre>