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

Create a View of Top 5 Selling Products

Create a View of Top 5 Selling Products

Products Table:

Short Description:
This SQL view, Top10SellingProducts, displays the top 5 best-selling products based on the total quantity sold.
It joins the Products and OrderDetails tables, groups the data by ProductID and ProductName, and orders them in descending order of total sales.


SQL Query:

GO  -- this starts a new batch
CREATE VIEW Top10SellingProducts AS
SELECT TOP 5 
    p.ProductID,
    p.ProductName,
    SUM(od.Quantity) AS TotalQuantitySold
FROM 
    Products p
INNER JOIN 
    OrderDetails od ON p.ProductID = od.ProductID
GROUP BY 
    p.ProductID, p.ProductName
ORDER BY 
    TotalQuantitySold DESC;


Output: