Update sql query for getting devices