Table: Sales
+---------------+---------+ | Column Name | Type | +---------------+---------+ | sale_date | date | | fruit | enum | | sold_num | int | +---------------+---------+ (sale_date, fruit) is the primary key (combination of columns with unique values) of this table. This table contains the sales of "apples" and "oranges" sold each day.
Write a solution to report the difference between the number of apples and oranges sold each day.
Return the result table ordered by sale_date.
The result format is in the following example.
Example 1:
Input: Sales table: +------------+------------+-------------+ | sale_date | fruit | sold_num | +------------+------------+-------------+ | 2020-05-01 | apples | 10 | | 2020-05-01 | oranges | 8 | | 2020-05-02 | apples | 15 | | 2020-05-02 | oranges | 15 | | 2020-05-03 | apples | 20 | | 2020-05-03 | oranges | 0 | | 2020-05-04 | apples | 15 | | 2020-05-04 | oranges | 16 | +------------+------------+-------------+ Output: +------------+--------------+ | sale_date | diff | +------------+--------------+ | 2020-05-01 | 2 | | 2020-05-02 | 0 | | 2020-05-03 | 20 | | 2020-05-04 | -1 | +------------+--------------+ Explanation: Day 2020-05-01, 10 apples and 8 oranges were sold (Difference 10 - 8 = 2). Day 2020-05-02, 15 apples and 15 oranges were sold (Difference 15 - 15 = 0). Day 2020-05-03, 20 apples and 0 oranges were sold (Difference 20 - 0 = 20). Day 2020-05-04, 15 apples and 16 oranges were sold (Difference 15 - 16 = -1).
Problem Overview: The table records how many apples and oranges were sold on each date. The task is to return the daily difference between apples and oranges sold (apples - oranges) for every sale_date.
Approach 1: Conditional Aggregation with GROUP BY (O(n) time, O(1) space)
The cleanest solution uses SQL conditional aggregation. Scan the table once, group rows by sale_date, and compute totals for each fruit using SUM(CASE WHEN ...). One conditional sum collects the number of apples sold and another collects oranges. Subtract the two aggregated values to produce the required difference. Because the table is processed once and aggregation happens during grouping, the time complexity is O(n) with constant extra space aside from the grouped result set.
This approach works well because each date already contains rows for both fruit types. Instead of joining rows or running multiple queries, conditional aggregation computes both totals in a single pass. The pattern is extremely common in database interview problems where multiple categories must be aggregated from the same dataset.
Approach 2: Self Join on Date (O(n log n) time, O(n) space)
Another option separates apples and oranges into two logical sets and joins them on sale_date. You can filter one subquery for apples and another for oranges, then join them and subtract the quantities. While conceptually simple, it introduces extra work for the database optimizer and may require sorting or indexing during the join. The result is typically O(n log n) time depending on the execution plan.
This method can still be useful if the dataset requires filtering or transformations before comparison. However, for straightforward aggregation problems, conditional sums are usually faster and easier to read.
Problems like this frequently appear in SQL interview rounds because they test your understanding of SQL aggregation and GROUP BY logic. You need to recognize when multiple rows should be summarized into a single metric per group.
Recommended for interviews: The GROUP BY + conditional SUM solution is the expected answer. It scans the table once, avoids unnecessary joins, and demonstrates strong knowledge of SQL aggregation patterns. Mentioning a join-based alternative shows breadth, but the single-pass aggregation solution signals better query design.
We can group the data by date, and then use the sum function to calculate the difference in sales between apples and oranges for each day. If it is an apple, we represent it with a positive number, and if it is an orange, we represent it with a negative number. Finally, we sort the data by date.
MySQL
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Conditional Aggregation (GROUP BY + SUM) | O(n) | O(1) | Best general solution for SQL aggregation problems with category-based totals |
| Self Join by Date | O(n log n) | O(n) | When data must be separated or filtered before comparing two categories |
LeetCode Medium 1445 Interview SQL Question with Detailed Explanation | Practice SQL • Everyday Data Science • 21,181 views views
Watch 6 more video solutions →Practice Apples & Oranges with our built-in code editor and test cases.
Practice on FleetCode