Updated new SQL code for working queries