DELETE DUPLICATE ROWS FROM A TABLE IN SQL || 4 ways
ฝัง
- เผยแพร่เมื่อ 20 ก.ค. 2024
- In today's video, we'll learn how to delete duplicate rows from a table in SQL.
4 following ways are shown:
1) remove duplicate rows using a temporary table
2) remove duplicate rows using auto ID
3) remove duplicate rows using row_number()
4) remove duplicate rows using CTE
Thank you!
✨Work related mails can be sent on:
work.sadiasiddiqui@gmail.com
✨If you appreciate my work and would like to donate to support our channel, you can do so here:
paypal.me/sadia1702
✨Link to my SQL e-book: imojo.in/rkajq9
✨Link to 'Switch your career easily' e-book: imojo.in/1FL7KaG
(Perfect for anyone willing to switch to a completely new field or to a different company in the Tech World)
Seen so many videos in SQL messed up ...this one really understandable with clear explanation!
I really appreciate your time and effort in sharing these (even though you are having cold)!! Thank you
Ya true..👍
That's called dedication...keep it up dear
Having cold ..hahaha..nailed it bro
Such developers actually reach to top positions, appreciate it.
Thank you for the clear explanations! You made the concepts simple to understand. I’m excited for new videos.
Mam it would be great if you can take up topics or concepts which are really important like this though not much frequently asked in interviews and also it is useful for an engineer to know all these as well
Wow! You are so good teacher. Was able to understand it more than other videos with fancy animation.
Nice & simple way of understanding 👏👏 for new starters in sql ur videos are best .. thank you for ur efforts and time
explanation is very clear by writing in paper which is unique in youtube. great job
Simply superb, doing a great job, simple and clear
Thankyou so much. You've covered all the methods and discussed them in detail.
I have been stuck on this since yesterday. Thank you😎
Excellent, your solutions are just great and easy to understand
Hello madam..
I have doubt in using temporary table..
Can insert distinct values in new table
Drop the old table
Rename new table as old table?
Absolutely brilliant explanation... Thank you so much..
kitni awesome hai yaar... So well explained... calm and soothing...
Excellent Explanation.....This video is really helpful.
This was very helpful! Thank you so much
So helpful thank you for sharing!
Your explanation is superb. one small request what ever you explained can you explain in sql server . that is very use full to us.
Nice video, and it would be great if you could make a video on types of Analytical Functions in sql, thanks!
Very detailed description... thanks
It's a great post Sadia, no matter how many years of experience we have ,it's always a good revision for any one
# Additional Reference
There are also other ways to restrict the duplicates
1. UNIQUE
2. UNION
3. ROW_NUMBER | RANK () | DENSE_RANK () - Analytical Function
4. INTERSECT
5. MINUS
6. ROWID | ROWID + CORRELATED SUBQUERY
7. GROUP BY
Very well explained....Thank you 😀
Nice Explaination. You have a lovely voice
SUPER HELPFUL!
Thank a lots !!! :) Hope to see more videos from you.
Very nice.simple understanding way you explain so nice
Thank you very much for you efforts... Must to be known things👌👌
Clear explanation keep doing more videos👍
Well explained, It will be most useful video in this topic..
Nice explanation with such a calm voice.
Thank you 😃
Helped me a lot Thanks 😁
Learned best way to query thanks for the video !!
Good Job!, Thank you.
well Explained !! Thank you.
th-cam.com/video/zA9v9lnP2BE/w-d-xo.html
Thanks for Helping
over partition example was really helpful
excellent explanation
It's really helpful
very good explanation..
thanks you explained well...
The first method ,the same query will it work in Oracle sql
Hi..one question i have ..pls rply ASAP... I'm using MYSQL and MYSQL doesn't support select * into.. therefore i used insert into newtable select distinct * from old table ...here i faced one issue...i hv one tble with eid,ename and salary. eid field is PK and ename and salary has duplicate values ...now when i use insert into using distinct keyword it copies duplicate data as well ......i only want distict data to be copied ..i tried dropping PK but on column eid but i'm facing same issue
Watching in 2022, for placements as being CSE student. Thanks a lot, didi. Your explanation is just amazing.
Very good explanation keep it up 👍
Good explanation. Can please explain and show by executing in the system.
your videos are awesome please make more videos on different topics
Thanks for such stuff🥰
Your efforts helped me a lot Thankyou mam
Good explanation
Your contents are amazing
Perfect. Thanks.
Good work, thanks
So informative...👍
Very well explained
Well explained👍
Hii.. Please clear one question in this video ..In CTE inner query ..you have not used * after select keyword ..you have directly written " select row_number()..." .... * aaega na after select ??
SO SIMPLE AND EASY
3rd and 4th way will delete duplicate row from result set and not from actual table, now what to do for deleting duplicate rows from actual actual?
very good effort and I appreciate your clips. However I have a question; suppose application messed up and gave same ID to two (2) different unique valid name. eg. 5001 to ABC and 5001 to XYZ and we want to keep both that is they should exists in our resultant with 2 unique IDs. Please don't get me wrong I am not trying to trick you, I had this real situation once with thousands of rows and was stuck.
In that scenario, you need to add ID, name in partition.
DELETE FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY ID, NAME ORDER BY ID) AS Rno
FROM emp
) AS E
WHERE Rno > 1;
This query will delete duplicate rows from the "emp" table where duplicates are determined by the combination of "ID" and "NAME" columns, keeping only the first occurrence and removing the rest.
Thanks it is very important interview question
Excelente muchas gracias
What happen if there are more than 50 columns with the duplicate records
Instead of passing all the columns names to the partition by clause
Is there any way to put * .
Any thoughts?
Hello, I have a question, how to delete duplicate records with no unique key in mysql? Can this be done using the row_number function?
DELETE K FROM
(SELECT *, ROW_NUMBER () OVER (PARTITION BY emp_id ORDER BY emp_id) AS RN FROM MyTable) K
WHERE RN>1
a bit speedy but you're doing a great job!
शेंबडाचा त्रास होत असताना देखील खूप छान शिकवलंस🙏👍 thank you
First method is easy to understand ...so I will go with the first method
First method will overload database performance. Avoid using select *
Thank you so much
Thank u dear..
Great videos.
Can we use the MAX(ID) method?
THANKS
Thank you.
Nyc video sister.
Thank you
your explanation is good. I am watching most of the videios.
for me 1 st method is showing error missing expression.
ur voice is so nice😍
Much appreciated
Excellent
superb...good job...
Thank you!
How come only distinct values will stored in new table if use -- Insert into old_table select * from new _table
? pls explain
because than new table has distinct values
Hi just one suggestion you first way will always not would the solution for routine. N in your second way we have to design table with identify type then you will be able to access
In third method : Can we access alias rn in outer query as well as use in where condition ?
It won't work.
The row_number() methodology is not working in snowflake db . anyone help me on this, how to resolve this like if we have to mention some alias name aslo and instead of rn>1 if we have to write alias.rn>1
Super thank 🌹you
Thanks
Explaining good & may be increase the volume (sound) better good
Thanku
so sNice tutorialt like that
Superb
Why we can't use direct delete query whose count greater than 1
Excellent madam pls do videos on power bi also pls
Very nice
Gr8 wrk!
Instead of MIN if we use Max then what will happen? I guess it will also remove duplicate
Super
Good
sorry but the third ways not working for me . this the query that i use
delete from
(select nom, row_number() over(partition by nom order by nom ) as count_num from fourniseurs)as tab where count_num >1;
but the select query work fine i wander why is it not working i use workbench mysql :/
delete from fourniseurs where nom IN (select nom from (select nom,row_number() over(partition by nom) as count_num from fourniseurs) ee where ee.count_num>1 );
thank you so much! I followed the first step and it made a new table with the distinct values in there. Could we just leave it there without having to delete old table data and bring the distinct values back from new table into oldtable?
yes it is done so that we don't have an empty original table which in business scenarios will be bad as there might be lot of dependencies in that table
Your English is good