Find Duplicate Rows in a Table

Problem Write a SQL query that finds repeated (duplicate) rows in a table.

Schema

  • orders(order_id, customer_id, product_id, order_date) — the columns that define a "duplicate" are agreed with the interviewer, e.g. (customer_id, product_id)

Output

  • Either the duplicated key values together with their occurrence count, or the full duplicate rows themselves — clarify which is wanted before writing

Example

  • Rows: (1, A, X), (2, A, X), (3, B, Y)
  • Grouping on (customer_id, product_id): (A, X) appears twice, (B, Y) once
  • Key-plus-count output → a single row: A, X, 2

Clarify before writing

  • Does "duplicate" mean every column identical, or just a business key matching? The query shape and the answer both change.
  • How should NULL key values be treated? They group together under GROUP BY but never compare equal under =.
asked …
LeaderboardSalaryAccount