On optimizing an SQL-like nested query | Synapse