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
What is the main difference between sql and pl/sql?
How do I filter in sql profiler?
what is oltp (online transaction processing)? : Sql dba
What is delimiter sql?
How can I get the number of records affected by a stored procedure?
Explain what is a field in a database and record in a database?
Explain what is a view?
What is record variable?
How do I install microsoft sql?
how to enter binary numbers in sql statements? : Sql dba
What is equi join in sql?
How to check if a column is nullable before setting to nullable?
what are different types of keys in sql?
what is an alias command? : Sql dba
what is online transaction processing (oltp)? : Sql dba