SQL Refactor Previous Versions

Refactors and formats SQL code.