Debugging duplicated data in SQL


  1. Are you missing a GROUP BY clause?
  2. Check if your source tables or nested queries have duplicated rows. You’ll need to repeat steps 3 and 4 for every table or query result that contains duplicate rows.

    -- If the row_count is greater than 1,
    -- you have duplicated rows in your results.
    
     SELECT
         < your_columns >,
         COUNT(*) AS row_count
     FROM
         < your_table_or_upstream_query >
     GROUP BY
         < your_columns >
     ORDER BY
         row_count DESC;
    
  3. Check your table to see how your join type interacts with your table relationships.
  4. Change your join type (LEFT JOIN, INNER JOIN etc) 

Date Added: