Revised sql