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 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: