CLC-3991 Add clarifying comments, improve formatting of sql