slightly updated sql