updated sql for @DarkMantis