title: Sql ,query for getting total count joining 2 tables
body: <p>I am new to sql. </p>
  
  <p><strong>Here is my question</strong></p>
  
  <p>The requirement is to join 2 tables and get the total count</p>
  
  <p><code>Order</code><strong>:</strong> {<code>ID</code>, <code>AccountID</code>, <code>OrderName</code>}</p>
  
  <p><code>OrderDetails</code><strong>:</strong> {<code>ID</code>, <code>OrderID</code>, <code>Productcode</code>, <code>ProductTitle</code>, ...}</p>
  
  <p><strong>Now</strong> I want to get the total count of orders which has associated 'Orderdetails' for a given account.</p>
  
  <p><strong>More Info..</strong></p>
  
  <p>Join these two tables on <code>OrderID</code> for a particular <code>AccountId</code> and get total count.</p>
  
  <p>also remove duplicates.
  that means order id repeats in the second table for product codes.</p>
  
  <p>There are instances order is inserted without order details.That means <code>OrderID</code> not present in <code>OrderDetails</code>.</p>
  
  <p><strong>My query'</strong></p>
  
  <pre><code>select count(*) from(
      SELECT MO.id
      FROM dbo.Order MO
      JOIN
          dbo.OrderDetails MOD ON 
          MO.ID = MOD.OrderID
          WHERE MO.AccountID ='123rt65-eee-ddd-ddd-dddddd'
          group by MO.ID) n
  </code></pre>
  
  <p>this gives me the correct count.</p>
  
  <p>Is this the right way? or is there any better way to achieve this?</p>
  
  <p>Thanks</p>
  
  <p>SNA</p>