SQL Fixes and Improvements