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

Retrieve Orders Where Total Amount Is Greater Than 500

Retrieve Orders Where Total Amount Is Greater Than 500
OrderDetails Table:


Orders Table:

Customers Table:

Query Explanation:

  • "Retrieve orders" → we select from the Orders table (o) and join with OrderDetails (od).

  • "Where total amount > 500" → we calculate the total using Quantity * UnitPrice * (1 - Discount/100) and apply the filter in the HAVING clause.

  • JOIN ensures we get the products and pricing details for each order.

  • GROUP BY is used to calculate the total amount per order.

SQL Query:

SELECT 
    o.OrderID,
    o.CustomerID,
    o.OrderDate,
    SUM(od.Quantity * od.UnitPrice * (1 - od.Discount / 100)) AS TotalAmount
FROM 
    Orders o
JOIN 
    OrderDetails od ON o.OrderID = od.OrderID
GROUP BY 
    o.OrderID, o.CustomerID, o.OrderDate
HAVING 
    SUM(od.Quantity * od.UnitPrice * (1 - od.Discount / 100)) > 500;

Output: