nCircle Tech interview question

Python code and SQL query