MEDIUMVerified Real Interview• Business Intelligence Engineer
Top 3 Selling Products by Department (DENSE_RANK)
Verified Source:Public Candidate Interview Report
Amazon Retail analytics needs to determine the top 3 revenue-generating products within each retail department.
Write a SQL query using DENSE_RANK() to find the top 3 products by revenue within each department.
In case of tied revenue, both products should share the same rank without skipping subsequent ranks.
Return department, product_name, total_revenue, and rank.
Sort by department ascending, then by rank ascending.
Tables:
product_sales(product_idINT,product_nameVARCHAR,departmentVARCHAR,total_revenueDECIMAL)
Execution Sandbox:
Your query executes inside an isolated in-memory PostgreSQL engine (PGlite WebAssembly). You have full access to standard PostgreSQL aggregations, window functions, and CTEs.
Standard SQL (ANSI)|Ln 1, Col 1(7 lines, 129 chars)
Tab Indent⌘↵ Run Query
Ready to execute. Press Run (⌘+Enter)
Write your query and click “Run” or press ⌘+Enter to inspect output.