Amazon Coding Question – Solved

7 Live
2. SQL: Ecommerce Deal Report An online shop needs a new deal history feature. Create a query that returns a list of the top three seller profiles with the highest total deals in June, 2022. The result should have the following columns: first_name / last_name | email | total. total - the total amount of all deals for a specific profile The result should be sorted in descending order by total. The result should be limited to the first three records. Note: Only deals for June 2022 should be included in the report.

Asked in: Amazon

Image of the Question

Question Image

All Testcases Passed βœ”



Passcode Image

Solution


/*
Enter your query below.
Please append a semicolon ";" at the end of the query
*/
// ... rest of solution available after purchase

πŸ”’ Please login to view the solution

Explanation


```
To approach the problem of generating an SQL query for the ecommerce deal report, begin by understanding the requirements and the dataset structure implied by the problem.

Step 1: Identify the Relevant Tables and Columns
- Since the goal is to list seller profiles along with their total deals, you need to focus on tables that hold profile information (likely a sellers or profiles table) and deals or transactions.
- The profile table should have columns such as first_name, last_name, and email.
- The deals or transactions table should include at least a reference to the seller profile (e.g., seller_id or profile_id), the deal amount, and a date or timestamp column representing when the deal occurred.

Step 2: Understand the Filtering Criteria
- The query must consider deals that happened strictly in June 2022.
- This implies filtering the deals table based on the deal date column using date range conditions.
- You need to be precise in defining the date boundaries to cover June 1, 2022 to June 30, 2022, inclusive.

Step 3: Aggregate Data by Seller Profile
- For each seller profile, sum the total amount of their deals within the specified date range.
- This requires grouping the data by seller profile fields (at least by seller_id or profile_id).
- The aggregation function to use here is SUM on the deal amount.

Step 4: Join Profiles and Deals
- Since the total deals are in the deals table and the seller details are in the profiles table, you must perform a join operation.
- Usually, this is done using an INNER JOIN, matching the profile identifier column from both tables.
- This join ensures you have access to the seller's personal information alongside their deal totals.

Step 5: Sorting and Limiting the Results
- After calculating the total deals for each profile, sort the output by the total in descending order to find the highest totals.
- The top three profiles must be selected, so you apply a LIMIT clause (or equivalent depending on the SQL dialect) to restrict the output to only the first three records.

Step 6: Selecting the Output Columns
- The final SELECT statement must include first_name, last_name, email, and the calculated total deal amount.
- The columns should be clearly aliased if necessary to maintain readability and clarity.

Step 7: Edge Cases and Validations
- Consider what happens if multiple profiles have the same total deal amounts near the cutoff point.
- The LIMIT clause typically just selects the first three records after sorting, so if there's a tie for third place, some sellers might be excluded.
- This behavior is acceptable unless the problem specifies handling ties differently.

Step 8: Performance Considerations
- Ensure that indexes exist on the date column in the deals table and the profile id columns in both tables to optimize join and filtering performance.
- Filtering early (restricting to June 2022 deals before joining) can reduce the size of data processed in the join.

Step 9: SQL Dialect Specifics
- Be mindful of differences in date functions or LIMIT syntax across SQL dialects.
- For example, some SQL dialects use TOP instead of LIMIT, or date functions like DATE_TRUNC, BETWEEN, or explicit date comparisons.

Summary:
- Filter deals to June 2022.
- Join deals with seller profiles.
- Group by seller.
- Sum the deal amounts.
- Sort descending by total.
- Limit output to top three.
- Select the requested columns.

Following this approach will produce the desired report accurately and efficiently.
```


Related Questions