one sql query to rule them all