How to remove Duplicate Data in SQL | SQL Query to remove duplicate

How to remove Duplicate Data in SQL | SQL Query to remove duplicate

techTFQ

1 год назад

331,972 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Arif Raza
Arif Raza - 01.10.2023 20:04

wonderful explanation! I got overwhelmed!

Ответить
Ninni Srivastava
Ninni Srivastava - 27.09.2023 22:10

Pls sent the cars file

Ответить
Michał
Michał - 22.09.2023 19:46

I believe solution 8 has a "bug", if you would have more than 1 duplicate - that is 3 or more records with the same values you would just delete one duplicate with proposed query which has biggest id value. So with simplified schema = (id, model, brand, row_num) and records = [(1, X5, BMW, 1), (1, X5, BMW, 2), (1, X5, BMW, 3)] you would just delete last record and still have [(1, X5, BMW, 1), (1, X5, BMW, 2)] left. It can be fixed by changing the query to `DELETE FROM prod WHERE row_num NOT IN (SELECT MIN(row_num) min_id FROM prod GROUP BY model, brand HAVING COUNT(*) > 1)`
This way you would delete every possible duplicate. Anyway, great video✌

Ответить
Venkatesh R
Venkatesh R - 14.09.2023 05:14

Thanks a lot brother. Its gonna help me in so many ways. Subscribed to your channel and ill definitely going to learn lot more things from you without any doubt. Thanks again

Ответить
Vikash kumar Mahato
Vikash kumar Mahato - 12.09.2023 20:38

1st 2 solutions are not working in MYSQL, You can't specify target table 'cars' for update in FROM clause --getting these error

Ответить
yamuna
yamuna - 29.08.2023 11:39

Hi Bro,in solution 1 if three or four duplicate records then like this we can write-> delete from cars where id not in (select min(id) from cars group by model,brand having count(*) >1);

Ответить
Leo Prabhakar
Leo Prabhakar - 26.08.2023 19:32

Very Useful toufiq

Ответить
S.S.Vinodh Vinu
S.S.Vinodh Vinu - 24.08.2023 10:32

Very nice video and explanation and would like to share the backup table creation as (Select *
into cars_bkp
From cars
where 1=2;) please check and share your feedback it works for me. Thanks for your video sir,

Ответить
Sarthak Haldar
Sarthak Haldar - 24.08.2023 02:13

7 mins: This deletion will not work in mySQL. In MySQL, you can't modify the same table which you use in the SELECT part. Else you get error: ERROR: You can't specify target table 'cars' for update in FROM clause.

Ответить
Nakul Barot
Nakul Barot - 22.08.2023 14:12

Scenario 2: Data duplicated based on ALL of the columns <<<<>>>>
alter table cars add column row_num int auto_increment primary key;

DELETE c1
FROM cars c1
JOIN cars c2 ON c1.model = c2.model AND c1.brand = c2.brand
WHERE c1.row_num > c2.row_num;

Ответить
Debapriya behera
Debapriya behera - 19.08.2023 14:18

Hi ... From scenario 2 and sol 1 delete with ctid , if ther are multiple records duplicate .... Rather than it display all records ...it's only filtering max records ... Please help me on that

Ответить
Kavitha Balasubramaniyan
Kavitha Balasubramaniyan - 06.08.2023 09:02

Thank you❤

Ответить
Elliot Baker-James
Elliot Baker-James - 03.08.2023 13:49

For the 2nd solution would this where clause work the same? I'm using data without unique ids:
WHERE c1.color <> c2>color

Ответить
michaely53
michaely53 - 27.07.2023 21:50

Thank you so much. Your lesson is so help full! I wonder do you have any lesson for preventing to have duplicate data in tables ?

Ответить
Diana Kotenko
Diana Kotenko - 26.07.2023 19:22

Thanks for this great video! I believe solution #2 will not work if you have 3 copies of the same row. Can you please check?
I.e., if the same model and car repeats 3 times with different ids, then your result will have 2 rows with different ids.
The 3rd solution will work, though!

Ответить
Rattana Crader
Rattana Crader - 26.07.2023 15:44

Thank you for your wonderful video it helped me a lot!!!

Ответить
Matthew Walton
Matthew Walton - 26.07.2023 02:37

If you are using SQLite you can use a NOT IN clause with built in rowid.

DELETE FROM dataset
WHERE rowid NOT IN (
SELECT MIN(rowid)
FROM dataset
GROUP BY col1, col2, col3 etc etc.
);


This will assign all duplicates a rowid, grouped by the columns you want to scrutinize, then it will delete all but the MIN rowid in that set of duplicates.

Hope this helps with SQLite users.

Ответить
Nadeem Ahmed
Nadeem Ahmed - 23.07.2023 12:02

its not executing showing this (Error Code: 1093. You can't specify target table 'cars' for update in FROM clause)

Ответить
rohit raghwa
rohit raghwa - 22.07.2023 23:10

Please make a video on advance sql questions.
Please.

Ответить
Nitin Mishra
Nitin Mishra - 22.07.2023 20:46

Perfect explanition!!

Ответить
Krishna Mangalapally
Krishna Mangalapally - 21.07.2023 11:38

Thanks😊

Ответить
Neptoon Palace
Neptoon Palace - 19.07.2023 14:05

But the 1st solution only deletes the 1 duplicate which has max id. What if the count is > 2

Ответить
Ananya Agarwal
Ananya Agarwal - 16.07.2023 03:15

Method 2 doesn't seem to work in Mysql. Raises the error "You can't use the same table in from clause". How can we solve this problem ? I see a few solutions that suggest self join instead of subquery. but couldn't understand the logic of how it is deleting from main table.

Ответить
Kamalika Mukherji
Kamalika Mukherji - 14.07.2023 15:39

Using cte?

Ответить
jathin kachve
jathin kachve - 08.07.2023 15:18

Hi sir, instead of using generated always as identity we could have used ROWNUNBER or other window functions right..?

Ответить
Nagesh Thakre
Nagesh Thakre - 03.07.2023 05:13

Happy Gurupornima 🙏

Ответить
ARINDAM SAHA
ARINDAM SAHA - 02.07.2023 18:18

SECINARIO 2
--> USING ROW NUMBER FUNCTION

WITH CTE AS (
SELECT *, ROW_NUMBER() OVER(PARTITION BY ID ORDER BY ID)RN FROM CARS)

DELETE FROM CTE
WHERE RN > 1

Ответить
yamuna
yamuna - 01.07.2023 11:06

super explaination bro

Ответить
Kamyab Sayed
Kamyab Sayed - 01.07.2023 01:56

Hi Thoufiq,
Solution 1 for the duplicate data with ID can be achieved in MS-SQL with this query -> DELETE FROM cars WHERE id NOT in (
select id FROM
(
select id, row_number() OVER(order by id) as unid
from cars
group by model, brand
)
);

Ответить
Imran Khan
Imran Khan - 30.06.2023 10:03

Very helpful

Ответить
Beks
Beks - 27.06.2023 15:43

@techTFQ
Your first method 'Delete using Unique Identifier' works if we have just one duplicate but what if we have 55 rows with Tesla Model S?

Ответить
Suresh Sekar
Suresh Sekar - 23.06.2023 12:41

Please makea video for data migration concept

Ответить
SAHIL BHANGE
SAHIL BHANGE - 16.06.2023 08:15

To remove row level duplicates, we can simply use set operation UNION
create table cars_bk as
select * from cars
UNION
select * from cars where 1=2;

Ответить
Burra Swathi
Burra Swathi - 07.06.2023 22:41

I have a query. in a table of 6 rows and 5 column I have added a new column(now 6 columns). So now to enter the data in that newly added column of all rows at a time what will be the query? could you please help me out with this?

Ответить
suyash pandey
suyash pandey - 23.05.2023 07:26

You are doing a great Job and your way of explaining is also very good, Thanks for helping us out

Ответить
Nawaz Khan
Nawaz Khan - 19.05.2023 01:25

Man you've explained exactly what i needed to know. You've no idea how relieved i feel after finally understanding the concept of joins. I needed to understand how sql works with each record of each table and you've explained that really well. Thanks a lot brother for this video. I'll definitely subscribe to your channel.

Ответить
neeraj gupta
neeraj gupta - 11.05.2023 22:08

Hi, your solution1 is having a error it should be "not in" in the delete statement as your query will not work in the case where more than 2 similar records are found for the same model and brand.

Ответить
Siva Kowshik
Siva Kowshik - 11.05.2023 17:42

is there any course in udemy (you have any membership in udemy)

Ответить
Abdulsamad Ibrahim
Abdulsamad Ibrahim - 08.05.2023 18:10

Thanks TFQ, I've been battling on how to remove duplicates from a project I am working on currently. But thanks to this video for coming to my rescue. Personally, I prefer the one of creating a temporary unique id column because we are used to having unique IDs in our dataset. The dataset I am working doesn't have a unique id, that's why it has been a bit difficult for me. But thanks once again.

Ответить
Darth MicHau
Darth MicHau - 08.05.2023 01:13

Ответить
Ben W.
Ben W. - 23.04.2023 17:24

Sorry, why do you not use distinct?

select DISTINCT model, brand
from cars
order by model, brand

Thank you.

Ответить
Sandeep Singh
Sandeep Singh - 14.04.2023 07:47

Hey
techTFQ,

Where i can learn Python , please help me on that ....

Ответить
Sandeep Singh
Sandeep Singh - 14.04.2023 07:46

Hey
techTFQ,Thank you so for your all videos, This is very helpful not helpful i think this is awesome for me, not me i think everyone , god will blessing you always.😍

Ответить
Nitesh Anjane
Nitesh Anjane - 13.04.2023 07:05

if id column is primary column then

Ответить
Imran Sayyad
Imran Sayyad - 11.04.2023 20:27

Not working with MySQL!!! :(

Ответить
GirdhaR
GirdhaR - 08.04.2023 13:34

explanation op💙❤

Ответить
Nath
Nath - 06.04.2023 18:00

I'm from Philippines and your new subscriber, I appreciate your lesson how you to discuss this in nice way and simple ,so that the viewer like me can understand clearly, thank you sir!

Ответить
venkatesh kilari
venkatesh kilari - 05.04.2023 22:43

hi

Ответить