Improved code for database queries by removing useless variables