How to retrieve Duplicate Rows only in a Table?
Suppose if a Table Name is "Education". It consists of
multiple columns. Then if we insert rows into this table
with duplicate records then how can we retrieve only
duplicate records from that table?
Answer Posted / selvaraj v
In Oracle 10g :
-----------------
SQL> SELECT * FROM emp;
EMPNO ENAME JOB MGR HIREDATE
SAL COMM DEPTNO
---------- ---------- --------- ---------- ---------
---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80
7500 20
7499 ALLEN SALESMAN 7698 17-DEC-80
1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81
1250 500 30
7566 JONES MANAGER 7839 02-APR-81
2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81
1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81
2850 30
7782 CLARK MANAGER 7839 09-JUN-81
2450 10
7788 SCOTT ANALYST 7566 19-APR-87
3000 20
7839 KING PRESIDENT 17-NOV-81
5000 10
7844 TURNER SALESMAN 7698 08-SEP-81
1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87
1100 20
7900 JAMES CLERK 7698 03-DEC-81
950 30
7902 FORD ANALYST 7566 03-DEC-81
3000 20
7934 MILLER CLERK 7782 23-JAN-82
1300 10
1 SELVA S/W ENG 7521 18-DEC-09
15000 1000 10
7521 WARD SALESMAN 7698 22-FEB-81
1250 500 30
16 rows selected.
The emp table contains DUPLICATE empno in this table. that's
2 empno's same. We find that empno's, Using the below query :
SQL> SELECT * FROM EMP
2 WHERE ROWID IN(SELECT MAX(ROWID) FROM EMP
3 GROUP BY EMPNO,ENAME,MGR,SAL,DEPTNO
4 HAVING COUNT(*)>1);
EMPNO ENAME JOB MGR HIREDATE
SAL COMM DEPTNO
---------- ---------- --------- ---------- ---------
---------- ---------- ----------
7521 WARD SALESMAN 7698 22-FEB-81
1250 500 30
In this emp table contains empno 7251 is 2 times.
Anyone Check this query. I'm expecting user comments...
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
Does pl sql work in mysql?
Is like operator in sql case sensitive?
What is sql and db2?
how to extract a unit value from a date and time? : Sql dba
Explain what is a column in a table?
Does truncate release storage space?
What is the difference between the conventional and direct path loads? : aql loader
Mention what does plv msg allows you to do?
how to drop an existing table in mysql? : Sql dba
What is Difference Between Unique and Primary Key Constraints?
What are conditional predicates?
Define commit?
what is table? : Sql dba
When sql appeared?
What is the command used to fetch first 5 characters of the string?