I'm sorry if this has been posted before/similarly, I wasn't sure how to structure my question to find proper results.
Essentially, I have 2 tables. Customers
and Orders
. I'm trying to pull the customer name from the Customers table, customerName
, and the amount of orders each customer has made. The Orders table has unique order ID's for every order placed, so my idea was to do a count on each order that belongs to each indiviual customer. Essentially, I want my query to output 2 columns, Customer name
and Number of Orders
. Then, I only want to display the customers that have greater than or equal to 5 orders.
These are the tables I'm working with.
CREATE TABLE Customers (
customerNumber INTEGER NOT NULL,
customerName VARCHAR(50) NOT NULL,
salesRepEmployeeNumber INTEGER NULL,
PRIMARY KEY (customerNumber),
FOREIGN KEY (salesrepEmployeeNumber) references Employees(employeeNumber)
)ENGINE=innodb;
CREATE TABLE Orders (
orderNumber int(11) NOT NULL,
orderDate DATETIME NOT NULL,
customerNumber INTEGER NOT NULL,
PRIMARY KEY (orderNumber),
FOREIGN KEY (customerNumber) references Customers(customerNumber)
)ENGINE=innodb;
The query I was trying is:
SELECT
c.customerName as 'Customer Name',
count(o.orderNumber) as 'Number of Orders Made',
FROM Customers c
INNER JOIN Orders o;
ON c.customerNumber = o.customerNumber
HAVING count(o.orderNumber) > '4';
However, this is just showing 1 single customer having what I believe is all orders attributed to them.
This is an example of the output I'm tring to obtain: