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 a heap in sql?
What's the procedure?
How do I install sql?
What is fmtonly in sql?
Why select is used in sql?
Is nosql faster than sql?
Explain two easy sql optimizations.
What is multiple partition?
What is difference between inner join and cross join?
what are the types of join and explain each? : Sql dba
explain the difference between myisam static and myisam dynamic. : Sql dba
List the different type of joins?
What is meant by temporal data?
What are triggers and its uses?
How do you create an update query?