All for Joomla All for Webmasters

SQL DELETE DUPLICATE ROWS

If you have got a situation that you have multiple duplicate records in a table, so at the time of fetching records from the table you should be more careful. You make sure that you are fetching unique records instead of fetching duplicate records.

To overcome with this problem we use DISTINCT keyword.

It is used along with SELECT statement to eliminate all duplicate records and fetching only unique records.

SYNTAX:

The basic syntax to eliminate duplicate records from a table is:

  1. SELECT DISTINCT column1, column2,….columnN
  2. FROM table _name
  3. WHERE [conditions]

EXAMPLE:

Let us take an example of STUDENT table.

ROLL_NO NAME PERCENTAGE ADDRESS
1 AJEET MAURYA 72.8 ALLAHABAD
2 CHANDAN SHARMA 63.5 MATHURA
3 DIVYA AGRAWAL 72.3 VARANASI
4 RAJAT KUMAR 72.3 DELHI
5 RAVI TYAGI 75.5 HAPUR
6 SONU JAISWAL 71.2 GHAZIABAD

Firstly we should check the SELECT query and see how it returns the duplicate percentage records.

  1. SQL > SELECT PERCENTAGE FROM STUDENTS
  2. ORDER BY PERCENTAGE;
PERCENTAGE
63.5
71.2
72.3
72.3
72.8
75.5

Now let us use SELECT query with DISTINCT keyword and see the result. This will eliminate the duplicate entry.

  1. SQL > SELECT DISTINCT PERCENTAGE FROM STUDENTS
  2. ORDER BY PERCENTAGE;
PERCENTAGE
63.5
71.2
72.3
72.8
75.5
PinIt
submit to reddit

Leave a Reply

Top