SQL code optimized.