Fixes to SQL