Final version of query 2 code.