Add SQL for Step 5 including new functions