Mysql Remove Duplicate Data or Rows With DISTINCT

Mysql Remove Duplicate Data or Rows With DISTINCT

To remove duplicate data or rows from a MySQL table, you can use the DISTINCT keyword in a SELECT statement.

For example, suppose you have a table named users with the following data:

idnameemail
1Alicealice@example.com
2Bobbob@example.com
3Alicealice@example.com
To remove the duplicate rows and select only unique rows, you can use the following `SELECT` statement: refer ‮al:ot‬utturi.com
SELECT DISTINCT * FROM users;

This will return the following result:

idnameemail
1Alicealice@example.com
2Bobbob@example.com
You can also use the `DISTINCT` keyword to select only unique values for a specific column. For example, to select only unique names from the `users` table, you can use the following `SELECT` statement:
SELECT DISTINCT name FROM users;

This will return the following result:

name
Alice
Bob
If you want to remove the duplicate rows from the table permanently, you can use the `DELETE` statement with the `DISTINCT` keyword. For example, to delete all duplicate rows from the `users` table, you can use the following `DELETE` statement:
DELETE FROM users WHERE id NOT IN (SELECT MIN(id) FROM users GROUP BY name, email);

This will delete all rows from the users table except for the rows with the minimum id.

Created Time:2017-10-30 10:17:51  Author:lautturi