Welcome Back

Google icon Sign in with Google
OR
I agree to abide by Pharmadaily Terms of Service and its Privacy Policy

Create Account

Google icon Sign up with Google
OR
By signing up, you agree to our Terms of Service and Privacy Policy
Instagram
youtube
Facebook

Total Units Sold Per Product

Total Units Sold Per Product
OrderDetails Table:


Query Explanation:

  • SUM(od.Quantity): Adds up all quantities sold for each product.

  • JOIN Products: To get the product name (for clarity).

  • GROUP BY p.ProductID, p.ProductName: Groups results by each product.

  • ORDER BY TotalUnitsSold DESC: Sorts from most sold to least.

SQL Query:

SELECT 
    p.ProductID,
    p.ProductName,
    SUM(od.Quantity) AS TotalUnitsSold
FROM 
    OrderDetails od
JOIN 
    Products p ON od.ProductID = p.ProductID
GROUP BY 
    p.ProductID, p.ProductName
ORDER BY 
    TotalUnitsSold DESC;

Output: