Apples & Oranges - Problem

You're the manager of a fruit stand that sells only apples and oranges. Each day, you record the sales of both fruits in your database. Now you need to analyze your daily performance by calculating the net difference between apple and orange sales.

Given a Sales table with the following structure:

Column NameType
sale_datedate
fruitenum ('apples', 'oranges')
sold_numint

Where (sale_date, fruit) is the primary key, write a SQL query to calculate the difference between apples and oranges sold each day. The result should show apples_sold - oranges_sold for each date.

Goal: Return a table with sale_date and diff (positive if more apples sold, negative if more oranges sold), ordered by sale_date.

Input & Output

example_1.sql โ€” Basic Case
$ 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 |
โ€บ Output: | sale_date | diff | |------------|----- | | 2020-05-01 | 2 | | 2020-05-02 | 0 | | 2020-05-03 | 20 |
๐Ÿ’ก Note: For 2020-05-01: 10 apples - 8 oranges = 2. For 2020-05-02: 15 apples - 15 oranges = 0. For 2020-05-03: 20 apples - 0 oranges = 20 (no oranges sold that day).
example_2.sql โ€” Only Oranges Some Days
$ Input: Sales table: | sale_date | fruit | sold_num | |------------|----------|---------| | 2020-05-01 | oranges | 12 | | 2020-05-02 | apples | 25 | | 2020-05-03 | oranges | 18 | | 2020-05-03 | apples | 5 |
โ€บ Output: | sale_date | diff | |------------|----- | | 2020-05-01 | -12 | | 2020-05-02 | 25 | | 2020-05-03 | -13 |
๐Ÿ’ก Note: For 2020-05-01: 0 apples - 12 oranges = -12. For 2020-05-02: 25 apples - 0 oranges = 25. For 2020-05-03: 5 apples - 18 oranges = -13.
example_3.sql โ€” Single Entry Per Day
$ Input: Sales table: | sale_date | fruit | sold_num | |------------|----------|---------| | 2020-05-01 | apples | 7 | | 2020-05-02 | oranges | 3 |
โ€บ Output: | sale_date | diff | |------------|----- | | 2020-05-01 | 7 | | 2020-05-02 | -3 |
๐Ÿ’ก Note: Edge case where some days only have one type of fruit. For 2020-05-01: 7 apples - 0 oranges = 7. For 2020-05-02: 0 apples - 3 oranges = -3.

Visualization

Tap to expand
๐ŸŽ vs ๐ŸŠ Daily Battle ScoreRaw Sales Data: 2020-05-01|apples|10, 2020-05-01|oranges|8, 2020-05-02|apples|15...Day 1: 2020-05-01๐ŸŽ+10 points๐ŸŠ-8 points+2Day 2: 2020-05-02๐ŸŽ+15 points๐ŸŠ-15 points0Final Scoreboard๐Ÿ“… 2020-05-01: ๐ŸŽ Wins (+2)๐Ÿ“… 2020-05-02: ๐Ÿค Tie (0)๐Ÿ“… 2020-05-03: ๐ŸŠ Wins (-12)๐ŸŽฏ Key: Use GROUP BY date + conditional SUM for O(n) solution
Understanding the Visualization
1
Daily Sales Records
Each row represents sales of one fruit type on a specific date
2
Group by Date
Collect all sales records for the same date together
3
Score Calculation
Add points for apples (+), subtract points for oranges (-)
4
Daily Winner
Positive score means apples won, negative means oranges won
Key Takeaway
๐ŸŽฏ Key Insight: Conditional aggregation with CASE statements lets us calculate the difference in a single pass, making it the most efficient solution at O(n) time complexity.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through all records with GROUP BY operation, linear time complexity

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Space proportional to number of unique dates (k), typically much smaller than total records (n)

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค number of rows in Sales โ‰ค 1000
  • sale_date is in format 'YYYY-MM-DD'
  • fruit is either 'apples' or 'oranges'
  • 1 โ‰ค sold_num โ‰ค 100
  • Each (sale_date, fruit) combination appears at most once
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
58.6K Views
High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen