As per CR, Added a lot more comments to make the SQL more readable