title: Microsoft SQL - Remove duplicate data from query results
body: <p>I am new to SQL Server and need help with one of my SQL query.</p>
  
  <p>I have 2 tables (<code>Rating</code> and <code>LikeDislike</code>).</p>
  
  <p>I am trying to get data from both of these tables using a <code>LEFT JOIN</code> like this:</p>
  
  <pre><code>SELECT distinct LD.TopicID, R.ID, R.Topic, R.CountLikes, R.CountDisLikes, LD.UserName, LD.Clikes
  
  FROM Rating As R
  
  LEFT JOIN LikeDislike AS LD on LD.TopicID = R.ID
  </code></pre>
  
  <p>The above <code>SELECT</code> statement displays results fine but also includes duplicates. I want to remove duplicates when the data is displayed, I tried using <code>DISTINCT</code> and <code>GROUP BY</code>, but with no luck, maybe because I am not using it correctly. </p>
  
  <p>To be more clear and less confusing let me tell you what exactly each table does and what I am trying to achieve.</p>
  
  <p>The <code>Rating</code> table has following columns (<code>ID, Topic, CountLikes, CountDisLikes, Extra, CreatedByUser</code>). It stores topic information and number of likes and dislikes for each topics and the <code>UserID</code> of the user who created that topic. </p>
  
  <p>Rating table with sample data</p>
  
  <pre><code>ID  Topic               CountLikes  CountDisLikes  Extra        CreatedByUser
  1   Do You Like This       211          58          YesId                2
  2   Or This                17           25          This also            3
  79  Testing at home        1             0          Testing at home      2
  80  Testing at home again  1             0          Testing              2
  82  testing dislikes       0             1          Testing              2
  76  Testing part 3         7             5          Testing 3            4
  77  Testing part 4         16            6          Testing 4            5
  </code></pre>
  
  <p>The <code>LikeDisLike</code> table has following columns (<code>ID, TopicID, UserName, Clikes</code>). <code>TopicID</code> is a FK to the <code>ID</code> column in <code>Rating</code> table.</p>
  
  <p>LikeDislike table with sample data</p>
  
  <pre><code>ID  TopicID UserName    Clikes
  213     77      2       TRUE
  214     76      2       FALSE
  215     77      5       TRUE
  194     77      3       TRUE
  195     76      3       FALSE
  196     2       3       TRUE
  197     1       3       FALSE
  </code></pre>
  
  <p>Now what I am trying to do is get information from both of this table without duplicate rows. I need to get data all the columns from <code>Rating</code> table + <code>UserName</code> and <code>Clikes</code> columns from <code>LikeDislike</code> table without any duplicate rows</p>
  
  <p>Below are the results with duplicates</p>
  
  <pre><code>TopicID ID  Topic            CountLikes   CountDislikes UserName    Clikes
  NULL    79  Testing at home    1           0             NULL       NULL
  NULL    80  Testing at home2   1           0             NULL       NULL 
  NULL    82  testing dislikes   0           1             NULL       NULL
  1       1   Do You Like This   211         58            3          FALSE
  2       2   Or This            17          25            3          TRUE
  76      76  Testing part 3     7           5             2          FALSE
  76      76  Testing part 3     7           5             3          FALSE
  77      77  Testing part 4     16          6             2          TRUE
  77      77  Testing part 4     16          6             3          TRUE
  77      77  Testing part 4     16          6             5          TRUE
  </code></pre>