Added an explanation and fixed some SQL syntax