MEDIUMVerified Real Interview• Business Intelligence Engineer

Top 3 Selling Products by Department (DENSE_RANK)

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_id INT, product_name VARCHAR, department VARCHAR, total_revenue DECIMAL)

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
⌘↵ Run Query
Ready to execute. Press Run (⌘+Enter)
Write your query and click “Run” or press ⌘+Enter to inspect output.