Python & SQL Quiz with Answers
1. What is the difference between @staticmethod, @classmethod, and instance methods in Python?
Answer: Instance methods operate on an instance of the class. @staticmethod does not access
class or instance. @classmethod takes the class as the first argument and can access class
variables.
2. How do you handle large CSV files in Python without exhausting memory?
Answer: Use chunking with pandas (e.g., pd.read_csv with chunksize) or use generators to load and
process data in parts.
3. Write a Python script to read a JSON file from S3 and send an SNS alert if a certain key is
missing.
Answer: Use boto3 to get the file: s3.get_object. Load JSON and check for the key, then use
sns.publish if missing.
4. What is the purpose of the 'with' statement in Python?
Answer: The 'with' statement ensures proper resource management, like closing files or releasing
locks.
5. How do you implement retry logic in Python for an API call that might fail intermittently?
Answer: Use the 'retrying' module or write a loop with try-except and time.sleep for retry intervals.
6. Write a SQL query to find duplicate records in a table based on two columns.
Answer: SELECT col1, col2, COUNT(*) FROM table GROUP BY col1, col2 HAVING COUNT(*) > 1;
7. How do you get the second highest salary in each department using SQL?
Answer: Use DENSE_RANK() in a CTE partitioned by department_id and filter where rank = 2.
8. Explain the difference between DENSE_RANK() and ROW_NUMBER() with an example.
Answer: DENSE_RANK() gives the same rank to ties, ROW_NUMBER() gives unique ranks. Use
DENSE_RANK to include duplicates.
9. Write a SQL query to calculate a running total of sales grouped by month.
Answer: SELECT month, SUM(sales) OVER (ORDER BY month ROWS BETWEEN UNBOUNDED
PRECEDING AND CURRENT ROW) AS running_total FROM sales_table;
10. What indexing strategies can you use to improve join performance on large tables?
Answer: Create indexes on the joining keys and use composite indexes when filtering and joining on
multiple columns.