Fixup some old badly built SQL queries

Add a few braces and stuff

Change-Id: I5d4c053a44c8e55a67189afb7f2540dc94757fc5
Merge "Fixup some old badly built SQL queries"