Deloitte Interview Question

Write SQL query interpreting loops in that.