Question

The Order table has the list of orders as well as a Customer ID for every order. And different orders can have the same customer ID , meaning a company has put in multiple orders (Given that Customer ID is the ID for the company).

Company Name comes from the Customer Table and my goal is to select company names with more than X number of orders.

Now, my first step is to join these two tables to compare, and then I can also Select company name, but I am confused as to how to select only the companies with more than X orders.

So far I have this, which writes out a company name X amount of times for X amount of orders they have issued.

SELECT CompanyName
    FROM Orders, Customers
        WHERE Orders.CustomerID = Customers.CustomerID;

How do I only get the company names greater than X Orders? I tried with WHERE COUNT(CompanyName)>3 , but that just gave errors. I don't think I learned any other way how to quantitize Selects from columns.

Was it helpful?

Solution

 SELECT CompanyName
    FROM Orders
    JOIN Customers ON Orders.CustomerID = Customers.CustomerID
GROUP BY CompanyName
  HAVING COUNT(CompanyName) > 3
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top