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

Get the Most Expensive Product(s) Per Category

Get the Most Expensive Product(s) Per Category
Products Table:


Categories Table:

Query Explanation
The subquery:

SELECT MAX(p2.UnitPrice) FROM Products p2 WHERE p2.CategoryID = p.CategoryID

finds the highest price within the same category.

  • The main query fetches all products whose price matches that max — this covers multiple most expensive products per category.

  • JOIN Categories: To show the category name for readability.

SQL Query:

SELECT 
    p.ProductID,
    p.ProductName,
    c.CategoryName,
    p.UnitPrice
FROM 
    Products p
JOIN 
    Categories c ON p.CategoryID = c.CategoryID
WHERE 
    p.UnitPrice = (
        SELECT MAX(p2.UnitPrice)
        FROM Products p2
        WHERE p2.CategoryID = p.CategoryID
    )
ORDER BY 
    c.CategoryName, p.UnitPrice DESC;

Output: