Query to determine if columns combine to create a unique key Query to determine if columns combine to create a unique key sql sql

Query to determine if columns combine to create a unique key


Use the HAVING clause to easily identify duplicates.

select t.a, t.b, t.c, count(1) from my_table t    group by t.a, t.b, t.c having count(1) > 1;


If the table has a decent amount of data, it's probably easier to do

SELECT t.a, t.b, t.c, count(*)  FROM my_table t GROUP BY t.a, t.b, t.cHAVING COUNT(*) > 1

If that query returns 0 rows, the three columns are (currently) unique. If that query returns 1 or more rows, you'll know which values are duplicated.

Of course, if you find that the three columns are currently unique, you'll want to create a unique constraint if you intend to make use of that fact.