How you will avoid duplicating records in a query?

By using DISTINCT

Showing Answers 1 - 40 of 40 Answers

savan

  • Sep 27th, 2005
 

a) delete from &&table_name t1 where t1.&&column_name in (select &&column_name
from &&table_name t2 where t1.rowid > t2.rowid and t1.&&column_name = t2.&&column_name)
/
undefine table_name
undefine column_name
The script will prompt for a table name and a column name from which duplicate values are to be removed. It will then remove all rows with ROWID values higher than the lowest ROWID for this particular column.
b) Let us take a table containing 3 columns, then we can use the following command to delete the duplicate rows from the table.
delete from table where rowid in (  SELECT  rowid FROM group by rowid,col1,col2,col3
minus SELECT  min(rowid) FROM group by col1,col2,col3);
c) delete from table where rowid not in ( SELECT  min(rowid) FROM group by col1,col2,col3 );
d) delete from employee where empid not in (Select min(empid) from employee)
e) select column_name from table_name group by column_name having count(*) > 1

  Was this answer useful?  Yes

vishal

  • Oct 17th, 2005
 

the best way is to use primary key;second thought if u dont have any constraints,go for distinct..but my view is we can used distinct to select rather then to aviod...plz coment

  Was this answer useful?  Yes

vishal

  • Oct 17th, 2005
 

the best way is to use primary key;second thought if u dont have any constraints,go for distinct..but my view is we can used distinct to select rather then to aviod...plz coment

  Was this answer useful?  Yes

Rohan Deshpande

  • Jan 29th, 2007
 

you can avoid duplicate rows by using DISTINCT.select distinct column_name from table_name;

  Was this answer useful?  Yes

Nikhil_4_Oracle

  • Mar 9th, 2007
 

HI ALL,

The best way is primary key. since we all know that only one primary key per table.

so use combination of unique key and NOT NULL constraint(if req) its also as same

as Primary key.

Regards,

Nikhil.
 

  Was this answer useful?  Yes

shikha

  • Apr 9th, 2007
 

By using Distinct we avoid dupication in records, in a queryselect distinct column_name from table_name

  Was this answer useful?  Yes

velsowmya

  • Aug 7th, 2007
 

To avoid duplications in while entering data in tables we use primary key.
Distinct is the concept which is used in retriving the data.
for example if there is a table and there is no primary key for eno column and there are some employees with same eno.using distinct command we can retrive the data.

  Was this answer useful?  Yes

narmadac

  • Feb 11th, 2010
 

Duplicating records can be avoided in two ways

First -- best method is assigning a Primary Key on the table.

Second --  If primary key is not assigned

select <column_names> from <table_name> where rowid in (select max(rowid) from <table_name> group by <column_name>);

ex: select ename, sal from emp where rowid in (select max(rowid) from emp group by eno);

  Was this answer useful?  Yes

SQL> select * from duplicate;

        ID     SALARY DATES
---------- ---------- --------------------
         1       3000 07-SEP-2010 14:28:22
         1       3000 07-SEP-2010 14:28:22
         2       3000 07-SEP-2010 14:28:22
         2       3000 07-SEP-2010 14:28:22
         3       3000 07-SEP-2010 14:31:43
         3       3000 07-SEP-2010 14:31:43
         3       3000 07-SEP-2010 14:32:18

7 rows selected.

SQL> delete from (select id,salary,dates from duplicate where rowid in (select max(rowid) from dupli
cate group by id,salary,dates));

4 rows deleted.

SQL> select * from duplicate;

        ID     SALARY DATES
---------- ---------- --------------------
         1       3000 07-SEP-2010 14:28:22
         2       3000 07-SEP-2010 14:28:22
         3       3000 07-SEP-2010 14:31:43

  Was this answer useful?  Yes

HI


select * from emp
where sal in (select  sal  from emp
                      group by sal
                       having count(1)>1);



Thanks &Regards,
kethinenisarath
                    

  Was this answer useful?  Yes

Hi! 
I think its best option to avoid duplicating records in a query



select * from emp
where sal in ( select sal from emp 
                    group by sal 
                   having  count(1)>1);


Thanks,
kethinenisarath 

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions