Société Générale interview question

Sql query about aggregation and pattern printing question