title: SQL: How to apply distinct only for one column out of many?
body: <p>I have two tables <code>Orders</code> and <code>Customers</code>. <code>Orders</code> contains the following columns: <code>OrderID</code>, <code>CustomerID</code>. <code>Customers</code> contains columns: <code>CustomerID</code>, <code>CustomerName</code>. I have a few records from <code>Customers</code> which do not have any orders. So I want to get all unique customers (even they do not have orders) and orders. </p>
  
  <p>I want to get it by using this query:</p>
  
  <pre><code>SELECT DISTINCT(CustomerName) OrderID, Orders.CustomerID, CustomerName
  FROM Customers
  LEFT JOIN Orders ON Orders.CustomerID = Customers.CustomerID
  ORDER BY CustomerName;
  </code></pre>
  
  <p>But it replace <code>OrderID</code> column values by <code>CustomerName</code> values.
  That why I should use the following query to get the desired result.</p>
  
  <pre><code>SELECT OrderID, CustomerID, CustomerName
  FROM 
  (
      SELECT DISTINCT(CustomerName) OrderID, Orders.CustomerID, CustomerName
      FROM Customers
      LEFT JOIN Orders ON Orders.CustomerID = Customers.CustomerID
      ORDER BY CustomerName
  );
  </code></pre>
  
  <p>Is there any errors into my first query? Is it possible to fix it?</p>