Assume I have a table TMP with structure as given below.
I have populated the duplicate values as well.
- CREATE TABLE TMP (
- NAME VARCHARW(30)
- )
I have populated the duplicate values as well.
- SELECT * FROM TEMP;
- Output
- ------
- Rahim
- Vicky
- Sai
- Tom
- Rahim
- Rahim
- Vicky
- Sai
- Jaz
- Apple
- Apple
- Rahim
Query to remove the Duplicate.
Method 1.
- WITH ds AS ( SELECT rownum AS ID, name FROM tmp),
- dup_set AS (SELECT id, RANK() OVER(PARTITION
- BY name ORDER BY ROWNUM) AS rk, name from ds)
- SELECT name FROM dup_set WHERE rk=1;
Output:
- Rahim
- Vicky
- Sai
- Tom
- Jaz
- Apple
Method 2.
- WITH set_a AS
- ( SELECT ROWNUM AS id, name FROM tmp a
- ),
- set_b AS
- (SELECT ROWNUM AS id, name FROM tmp b
- )
- SELECT a.name
- FROM set_a a
- WHERE a.id >= ALL
- (SELECT id FROM set_b b
- WHERE a.name=b.name
- );
Method 3.
- WITH rem_dup_set AS (
- SELECT ROW_NUMBER() OVER(PARTITION BY name
- ORDER BY name) AS rk, name
- FROM temp )
- SELECT name FROM rem_dup_set WHERE rk=1;
Output:
- Rahim
- Vicky
- Sai
- Tom
- Jaz
- Apple