Best-Selling Products per Category with Tie-Breakers

Best-Selling Products per Category with Tie-Breakers

You are given two tables: 'products' (containing product_id, product_name, and category_name) and 'product_sales' (containing product_id, sales_quantity, and rating). Write a PostgreSQL query to identify the top-performing product in every category. The primary metric for performance is 'sales_quantity'. In the event of a tie in sales, the product with the higher 'rating' should be prioritized. If multiple products remain tied after checking both metrics, include all of them. The final result should display the category name and product name, sorted alphabetically by the category name.
PostgreSQLCTEWindow FunctionRANK
10
Read
1
InterviewGPT

AI-powered tools to help you succeed in tech interviews — from resume to offer.

Products

  • Interview Solver
  • Question Bank
  • Golden Blogs
  • Intervipedia
  • Application Tools

Company

  • Pricing
  • FAQ
  • About

Legal

  • Privacy Policy
  • Terms of Service

© 2026 InterviewGPT Inc. All rights reserved.

All systems operationalUS-East

Made with ♥ for developers