Given two columns (Animal and Name) how do I write a SQL statement that finds the rows that don’t have a 1 to 1 relationship i.e. Dog = Patch and Dog = Rover ?
Animal | Name |
---|---|
Dog | Patch |
Cat | Sylvester |
Mouse | Gerry |
Mouse | Gerry |
Dog | Rover |
Given two columns (Animal and Name) how do I write a SQL statement that finds the rows that don’t have a 1 to 1 relationship i.e. Dog = Patch and Dog = Rover ?
Animal | Name |
---|---|
Dog | Patch |
Cat | Sylvester |
Mouse | Gerry |
Mouse | Gerry |
Dog | Rover |
2
Answers
You may use
EXISTS
operator with a correlated subquery as the following:See a demo.
Something like this would work (probably not the best way to do it):
This looks the number of distinct Animal values for each Name anywhere in the table and vice versa and returns only the rows where one or the other is greater than 1.