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

Find Customers Who Placed More Than 5 Orders

Find Customers Who Placed More Than 5 Orders

Customers Table:

Orders Table:

Query Explanation

● SELECT Customers.CustomerID, Customers.Name, COUNT(Orders.OrderID) AS OrderCount
 Selects the customer ID, name, and the total number of orders placed by each customer (as OrderCount).

● FROM Customers
 Starts from the Customers table which holds customer information.

● JOIN Orders ON Customers.CustomerID = Orders.CustomerID
 Joins the Orders table with Customers to link orders to their respective customers.

● GROUP BY Customers.CustomerID, Customers.Name
 Groups the data by each customer to count their total number of orders.

● HAVING COUNT(Orders.OrderID) > 5
 Filters out only those customers who have placed more than 5 orders.

 

SQL Query:

USE SalesInventoryDB;

SELECT Customers.CustomerID, Customers.Name, COUNT(Orders.OrderID) AS OrderCount
FROM Customers
JOIN Orders ON Customers.CustomerID = Orders.CustomerID
GROUP BY Customers.CustomerID, Customers.Name
HAVING COUNT(Orders.OrderID) > 5;

 

Output: