Revised SQL query