Wednesday, July 15, 2009

How to get the duplicate values from table using sql

SOLUTION :

SELECT * from TABLE_NAME a where rowid not in (select max(rowid) from TABLE_NAME b
where a.COLUMN_NAME=b.COLUMN_NAME)

above query retrieves the duplicated values from the table now we can see with the example

BOOK
---------------------------------------------
BOOK_NAME PRICE AUTHOR
--------------------------------------------
JAVA 200 XXXX
LEARNING C 100 YYYY
JAVA 150 ZZZZ
C# 180 AAAA
C++ 200 BBBB
JAVA 200 XXXX
C# 150 RRRR
ORACLE 300 UUUU
PHP 100 EEEE

in the above BOOK table BOOK_NAME is column name here the JAVA and C# gets duplicated .
from this if you want to get duplicated value based on the column name .use the following query

SELECT * from BOOK a where rowid not in (select max(rowid) from BOOK b
where a.BOOK_NAME=b.BOOK_NAME)

after execute this query you will get the following values ,which is the highest of records from duplicated values.

---------------------------------------------
BOOK_NAME PRICE AUTHOR
---------------------------------------------
JAVA 200 XXXX
JAVA 150 ZZZZ
C# 180 AAAA

thanks...
A.T.J

No comments:

Post a Comment