title: Can't solve this SQL query
body: <p>I have a difficulty dealing with a SQL query. I use PostgreSQL.</p>
  
  <p>The query says: Show the customers that have done at least an order that contains products from 3 different categories. The result will be 2 columns, <code>CustomerID</code>, and the amount of orders. I have written this code but I don't think it's correct.</p>
  
  <pre><code>select SalesOrderHeader.CustomerID,
         count(SalesOrderHeader.SalesOrderID) AS amount_of_orders
  from SalesOrderHeader 
  inner join SalesOrderDetail on
         (SalesOrderHeader.SalesOrderID=SalesOrderDetail.SalesOrderID)
  inner join Product on 
         (SalesOrderDetail.ProductID=Product.ProductID)
  
  where SalesOrderDetail.SalesOrderDetailID in 
         (select DISTINCT count(ProductCategoryID)
          from Product
          group by ProductCategoryID
          having count(DISTINCT ProductCategoryID)&gt;=3)
  
  group by SalesOrderHeader.CustomerID;
  </code></pre>
  
  <p>Here are the database tables needed for the query:</p>
  
  <p><a href="https://i.stack.imgur.com/BrqTk.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BrqTk.jpg" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/9N2U7.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9N2U7.jpg" alt="enter image description here"></a></p>