Société Générale Interview Question

Sql query about aggregation and pattern printing question