Can you provide the second highest and second lowest sales items? Also, can you give the most viable product, defined as having the lowest return rate and highest sales?
1Times asked
Jul 2026Last seen
Jul 2026First seen
💡 Model Answer
Use the same approach as in question 3. First, aggregate sales per product and rank them:
sql
SELECT product_name
FROM (
SELECT product_name,
SUM(Sale_Quantity) AS total_sales,
RANK() OVER (ORDER BY SUM(Sale_Quantity) DESC) AS rank_desc,
RANK() OVER (ORDER BY SUM(Sale_Quantity) ASC) AS rank_asc
FROM sales_table
GROUP BY product_name
) AS ranked
WHERE rank_desc = 2 OR rank_asc = 2;Then, identify the most viable product by lowest return rate and highest sales:
sql
SELECT product_name
FROM (
SELECT product_name,
SUM(Sale_Quantity) AS total_sales,
SUM(Return) AS total_return,
SUM(Return) / NULLIF(SUM(Sale_Quantity),0) AS return_rate
FROM sales_table
GROUP BY product_name
) AS metrics
ORDER BY return_rate ASC, total_sales DESC
LIMIT 1;Both queries are O(n) in time and O(k) in space, where k is the number of distinct products. They provide the requested items efficiently.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.
Get Assisting AI — Starts at ₹500