Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL query to find rows that don't have a 1 to 1 relationship between 2 columns

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You may EXISTS operator with a correlated subquery as the following:

SELECT Animal, Name
FROM table_name T
WHERE EXISTS(SELECT 1 FROM table_name D WHERE D.Animal=T.Animal AND D.Name<>T.Name)
ORDER BY Animal

See a demo.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading