Instagram
youtube
Facebook
Twitter

Retrieve Orders with More Than 3 Different Products

Retrieve Orders with More Than 3 Different Products

orderdetails Table:

orders Table:

Query Explanation:

SELECT OrderID
 – Selects the Order ID of each order.

FROM OrderDetails
 – Uses the OrderDetails table, where each row represents a product in an order.

GROUP BY OrderID
 – Groups the records by each unique order.

HAVING COUNT(DISTINCT ProductID) > 3
 – Filters only those orders that contain more than 3 different products.

 

SQL Query:

SELECT OrderID
FROM OrderDetails
GROUP BY OrderID
HAVING COUNT(DISTINCT ProductID) > 3;

 

Output: