Earlier quoted context omitted.
What makes you think that SQL doesn't have "for loops"? Ever heard of LATERAL joins/CROSS APPLY? SELECT loop.value, x.squared FROM generate_series(1,5) AS loop(value) CROSS JOIN LATERAL (SELECT loop.value * loop.value AS squared) AS x;
TIL
- Reference data from the previous part of the query (the "left-hand side")
- Return multiple columns
The only way you can achieve it is with LATERAL/CROSS APPLY.
Regular correlated subqueries can only return a single column, so something like this doesn't work:
SELECT
loop.val, (SELECT loop.val * loop.val, 'second column') AS squared
FROM
(SELECT loop.val FROM generate_series(1,5) AS loop(val)) as loop
You'd get: error: subquery must return only one column