The "Apples & Oranges interview question" is a SQL data transformation problem. You are given a table of sales data where each row contains a date, the type of fruit (Apples or Oranges), and the quantity sold. Your task is to calculate the difference between the number of apples sold and the number of oranges sold for each day.
Meta asks the "Apples & Oranges coding problem" to test a candidate's ability to perform Pivoting or Conditional Aggregation in SQL. It's a common task in business intelligence to compare two different categories side-by-side when they are stored in a long-format (normalized) table.
This problem uses the GROUP BY with CASE WHEN pattern.
sale_date.SELECT statement, use SUM(CASE WHEN fruit = 'apples' THEN sold_num ELSE 0 END) to get the apple total.SUM for oranges.Data:
(2023-01-01, 2), (2023-01-02, -5).GROUP BY, which would make the query fail if new dates are added.SUM might be NULL (use COALESCE or ELSE 0 to fix this).Practice "Pivoting" data in SQL. Converting rows to columns is one of the most useful skills for data analysis interviews. Master the CASE WHEN statement as it is the Swiss Army knife of SQL logic.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Customers Who Bought Products A and B but Not C | Medium | Solve | |
| Get Highest Answer Rate Question | Medium | Solve | |
| Page Recommendations | Medium | Solve | |
| Project Employees III | Medium | Solve | |
| Reported Posts II | Medium | Solve |