Finished SQL and Python code