#SQL data validation

1 messages · Page 1 of 1 (latest)

frosty panther
#

How would you check in SQL if your table have any duplicated row based on unique key columns?

Let say I’ve 4 columns that are unique keys. How would I check if I don’t have any duplication in my table?

supple urchin
#

#📄・posting-guidelines 🤓

#

lol

real oasis
#

You can use GROUP_BY and HAVING.

Ex. (with one col for simplicity)

SELECT column1 FROM my_table GROUP BY column1 HAVING count(*) > 1;
frosty panther
frosty panther
real oasis
#

Ex.

frosty panther
#

Uhm, I see.

#

Womp Womp my table lol

real oasis
#

This will return the owner_id that has more than 1 notebook

#

But you can include other info if you want too or count the number of columns

#

But I'd say you're problem is weird. Don't you have an unicity constraint on your columns? Or the goal is that you want to convert them into unique columns.

frosty panther
#

Yeah, that make sense.
You can do:

SELECT count(*) FROM my_table GROUP BY col1, col2 HAVING count(*) > 1;

This should return the number of duplicated row based on col1 and col2 uniqueness

real oasis
#

Yea