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

Fetching Data Anomalies in SQL (2 Columns)

I have a table with two columns and following data

ID NAME
1  ALPHA
1  ALPHA
2  BETA
1  BETA

First three rows are correct data, but in the last row someone accidentally entered ID 1 instead of ID 2, can anyone help me with a query to fetch multiple rows of ID for distinct names. I have tried the query below but its not yielding the correct result

SELECT F1.ID FROM myTable F1 WHERE F1.Name in
(SELECT DISTINCT F2.Name FROM myTable F2)

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

>Solution :

Actually, you need names that have multiple IDs; right?

For sample data:

SQL> select * From test;

        ID NAME
---------- -----
         1 alpha
         1 alpha
         2 beta
         1 beta

Query, using group by and having clauses:

SQL> select name
  2  from test
  3  group by name
  4  having count(distinct id) > 1;

NAME
-----
beta

SQL>
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