Optimize SQL to minimize scans
Company: Meta
Role: Data Engineer
Category: Data Manipulation (SQL/Python)
Difficulty: Medium
Interview Round: Onsite
Given a large analytics query, refactor it to minimize table scans.
1) Replace unnecessary CTEs that cause multiple scans with inline aggregations or derived tables where appropriate.
2) Prefer GROUP BY aggregations when applicable and justify the execution plan.
3) Explain how you would verify the number of scans using EXPLAIN and iterate on the plan.
4) Start with a quick draft solution, then refine it to reduce scans and improve readability.
Quick Answer: This question evaluates proficiency in SQL query optimization, specifically minimizing table scans, choosing between CTEs and derived tables, applying aggregation strategies, and interpreting execution plans.