mostly working sql output