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

ความคิดเห็น • 183

  • @crackconcepts
    @crackconcepts  9 หลายเดือนก่อน

    ✨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)

  • @karthikd2104
    @karthikd2104 3 ปีที่แล้ว +6

    Seen so many videos in SQL messed up ...this one really understandable with clear explanation!

  • @rajkiranboggala9722
    @rajkiranboggala9722 3 ปีที่แล้ว +35

    I really appreciate your time and effort in sharing these (even though you are having cold)!! Thank you

    • @srinivasprasad7765
      @srinivasprasad7765 3 ปีที่แล้ว

      Ya true..👍

    • @ravindrabisht7372
      @ravindrabisht7372 3 ปีที่แล้ว +1

      That's called dedication...keep it up dear

    • @ShubhamKumar-mf4sg
      @ShubhamKumar-mf4sg 2 ปีที่แล้ว

      Having cold ..hahaha..nailed it bro

    • @tubepankajyou
      @tubepankajyou 2 ปีที่แล้ว +1

      Such developers actually reach to top positions, appreciate it.

  • @umaannamalai3205
    @umaannamalai3205 3 ปีที่แล้ว +5

    Thank you for the clear explanations! You made the concepts simple to understand. I’m excited for new videos.

  • @akashagarwal6390
    @akashagarwal6390 4 ปีที่แล้ว +9

    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

  • @marvinmachitar7229
    @marvinmachitar7229 2 ปีที่แล้ว +1

    Wow! You are so good teacher. Was able to understand it more than other videos with fancy animation.

  • @mastanshaik5479
    @mastanshaik5479 3 ปีที่แล้ว +1

    Nice & simple way of understanding 👏👏 for new starters in sql ur videos are best .. thank you for ur efforts and time

  • @adapass
    @adapass 2 ปีที่แล้ว +1

    explanation is very clear by writing in paper which is unique in youtube. great job

  • @Rjjithin777
    @Rjjithin777 2 ปีที่แล้ว

    Simply superb, doing a great job, simple and clear

  • @rajattalnikar6167
    @rajattalnikar6167 3 ปีที่แล้ว

    Thankyou so much. You've covered all the methods and discussed them in detail.

  • @tchelaa6894
    @tchelaa6894 7 หลายเดือนก่อน +1

    I have been stuck on this since yesterday. Thank you😎

  • @deepakaggarwal8624
    @deepakaggarwal8624 2 ปีที่แล้ว

    Excellent, your solutions are just great and easy to understand

  • @bhavanikandikondamatam5926
    @bhavanikandikondamatam5926 3 ปีที่แล้ว +6

    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?

  • @aeriesx4727
    @aeriesx4727 3 ปีที่แล้ว

    Absolutely brilliant explanation... Thank you so much..

  • @MoodyVideoGraphies
    @MoodyVideoGraphies 2 ปีที่แล้ว

    kitni awesome hai yaar... So well explained... calm and soothing...

  • @techninja9461
    @techninja9461 3 ปีที่แล้ว

    Excellent Explanation.....This video is really helpful.

  • @MUSKAN0896
    @MUSKAN0896 2 ปีที่แล้ว

    This was very helpful! Thank you so much

  • @maxiejane
    @maxiejane 4 ปีที่แล้ว +1

    So helpful thank you for sharing!

  • @rajendrarajendra583
    @rajendrarajendra583 2 ปีที่แล้ว

    Your explanation is superb. one small request what ever you explained can you explain in sql server . that is very use full to us.

  • @sohailkat8867
    @sohailkat8867 3 ปีที่แล้ว +1

    Nice video, and it would be great if you could make a video on types of Analytical Functions in sql, thanks!

  • @nimmikcherian3242
    @nimmikcherian3242 3 ปีที่แล้ว

    Very detailed description... thanks

  • @rakeshgopidi4066
    @rakeshgopidi4066 2 ปีที่แล้ว +10

    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

  • @nausheen292
    @nausheen292 3 ปีที่แล้ว

    Very well explained....Thank you 😀

  • @Sriramlivz
    @Sriramlivz 4 ปีที่แล้ว

    Nice Explaination. You have a lovely voice

  • @richardconnolly4295
    @richardconnolly4295 3 ปีที่แล้ว

    SUPER HELPFUL!

  • @mudasar8629
    @mudasar8629 3 ปีที่แล้ว +1

    Thank a lots !!! :) Hope to see more videos from you.

  • @giridandi7480
    @giridandi7480 ปีที่แล้ว

    Very nice.simple understanding way you explain so nice

  • @akashverma1640
    @akashverma1640 2 ปีที่แล้ว +1

    Thank you very much for you efforts... Must to be known things👌👌

  • @jaganjayakumar4874
    @jaganjayakumar4874 4 ปีที่แล้ว

    Clear explanation keep doing more videos👍

  • @joshyjohn9636
    @joshyjohn9636 3 ปีที่แล้ว

    Well explained, It will be most useful video in this topic..

  • @naveenshagala3136
    @naveenshagala3136 5 ปีที่แล้ว +1

    Nice explanation with such a calm voice.

  • @Aryan-ly5ho
    @Aryan-ly5ho 3 ปีที่แล้ว

    Helped me a lot Thanks 😁

  • @saravananbhojan8750
    @saravananbhojan8750 2 ปีที่แล้ว

    Learned best way to query thanks for the video !!

  • @mangamanasvi5485
    @mangamanasvi5485 4 ปีที่แล้ว +1

    Good Job!, Thank you.

  • @akash_pal
    @akash_pal 4 ปีที่แล้ว +1

    well Explained !! Thank you.

    • @bro_7079
      @bro_7079 4 ปีที่แล้ว

      th-cam.com/video/zA9v9lnP2BE/w-d-xo.html

  • @mohansaivelagapudi929
    @mohansaivelagapudi929 2 ปีที่แล้ว

    Thanks for Helping

  • @DeepakMeena-kz9bu
    @DeepakMeena-kz9bu 3 ปีที่แล้ว

    over partition example was really helpful

  • @maryamfatima5882
    @maryamfatima5882 ปีที่แล้ว

    excellent explanation

  • @veerulinganagoudar3456
    @veerulinganagoudar3456 2 ปีที่แล้ว

    It's really helpful

  • @ankiturmaliya7286
    @ankiturmaliya7286 ปีที่แล้ว

    very good explanation..

  • @saurabhbenbanshi3460
    @saurabhbenbanshi3460 4 ปีที่แล้ว

    thanks you explained well...

  • @kshathrugan5238
    @kshathrugan5238 4 ปีที่แล้ว

    The first method ,the same query will it work in Oracle sql

  • @tushardeepsingh4976
    @tushardeepsingh4976 3 ปีที่แล้ว

    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

  • @husler7424
    @husler7424 ปีที่แล้ว

    Watching in 2022, for placements as being CSE student. Thanks a lot, didi. Your explanation is just amazing.

  • @boddanapalliavinash6418
    @boddanapalliavinash6418 ปีที่แล้ว

    Very good explanation keep it up 👍

  • @praveenvadde278
    @praveenvadde278 3 ปีที่แล้ว

    Good explanation. Can please explain and show by executing in the system.

  • @shubhamanand1347
    @shubhamanand1347 4 ปีที่แล้ว

    your videos are awesome please make more videos on different topics

  • @AmanKhan-ei4cg
    @AmanKhan-ei4cg 4 ปีที่แล้ว +1

    Thanks for such stuff🥰

  • @zafariqbalunknown9786
    @zafariqbalunknown9786 2 ปีที่แล้ว

    Your efforts helped me a lot Thankyou mam

  • @swapneelkambe2092
    @swapneelkambe2092 ปีที่แล้ว

    Good explanation

  • @sheikhmohduzair4641
    @sheikhmohduzair4641 2 ปีที่แล้ว

    Your contents are amazing

  • @HardikShah17
    @HardikShah17 3 ปีที่แล้ว

    Perfect. Thanks.

  • @qaz_testzaq_test8138
    @qaz_testzaq_test8138 3 ปีที่แล้ว

    Good work, thanks

  • @shubhamchoudhary5461
    @shubhamchoudhary5461 3 ปีที่แล้ว

    So informative...👍

  • @shwetabudake6144
    @shwetabudake6144 3 ปีที่แล้ว

    Very well explained

  • @namratanikam7566
    @namratanikam7566 3 ปีที่แล้ว

    Well explained👍

  • @tushardeepsingh4976
    @tushardeepsingh4976 3 ปีที่แล้ว

    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 ??

  • @faizazhar768
    @faizazhar768 ปีที่แล้ว

    SO SIMPLE AND EASY

  • @azharuddinluhar9667
    @azharuddinluhar9667 2 ปีที่แล้ว +1

    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?

  • @mrf7283
    @mrf7283 3 ปีที่แล้ว +5

    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.

    • @user-rr9lo4wc8g
      @user-rr9lo4wc8g 10 หลายเดือนก่อน

      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.

  • @meditationandrelaxationmus741
    @meditationandrelaxationmus741 2 ปีที่แล้ว

    Thanks it is very important interview question

  • @CarlosRivera-mp7mm
    @CarlosRivera-mp7mm ปีที่แล้ว

    Excelente muchas gracias

  • @saisumanthnagasuri7741
    @saisumanthnagasuri7741 ปีที่แล้ว

    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?

  • @sukritsen9530
    @sukritsen9530 4 ปีที่แล้ว +2

    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?

    • @chanduch8973
      @chanduch8973 3 ปีที่แล้ว +4

      DELETE K FROM
      (SELECT *, ROW_NUMBER () OVER (PARTITION BY emp_id ORDER BY emp_id) AS RN FROM MyTable) K
      WHERE RN>1

  • @hadireg
    @hadireg 3 ปีที่แล้ว +1

    a bit speedy but you're doing a great job!

  • @sushantpawar5635
    @sushantpawar5635 3 ปีที่แล้ว

    शेंबडाचा त्रास होत असताना देखील खूप छान शिकवलंस🙏👍 thank you

  • @hidden590
    @hidden590 4 ปีที่แล้ว +4

    First method is easy to understand ...so I will go with the first method

    • @FitVisionTV
      @FitVisionTV 3 ปีที่แล้ว

      First method will overload database performance. Avoid using select *

  • @ybujji
    @ybujji 4 ปีที่แล้ว

    Thank you so much

  • @vivekrahate2564
    @vivekrahate2564 2 ปีที่แล้ว

    Thank u dear..

  • @mrrishiraj88
    @mrrishiraj88 3 ปีที่แล้ว

    Great videos.

  • @rosonerri-faithful
    @rosonerri-faithful 2 ปีที่แล้ว

    Can we use the MAX(ID) method?

  • @digitalcode5685
    @digitalcode5685 2 ปีที่แล้ว

    THANKS

  • @muraliraj9658
    @muraliraj9658 3 ปีที่แล้ว

    Thank you.

  • @ravaliannaldas9320
    @ravaliannaldas9320 2 ปีที่แล้ว

    Nyc video sister.

  • @nareshd4199
    @nareshd4199 4 ปีที่แล้ว

    Thank you

  • @ganeshstars1
    @ganeshstars1 2 ปีที่แล้ว

    your explanation is good. I am watching most of the videios.
    for me 1 st method is showing error missing expression.

  • @vrocks2210
    @vrocks2210 3 ปีที่แล้ว

    ur voice is so nice😍

  • @dhineshrajendran2312
    @dhineshrajendran2312 3 ปีที่แล้ว

    Much appreciated

  • @Pavankumar71036
    @Pavankumar71036 3 ปีที่แล้ว

    Excellent

  • @dstar2211
    @dstar2211 4 ปีที่แล้ว

    superb...good job...

  • @ramchinthakayala2092
    @ramchinthakayala2092 4 ปีที่แล้ว

    How come only distinct values will stored in new table if use -- Insert into old_table select * from new _table
    ? pls explain

    • @yashgoswami5374
      @yashgoswami5374 3 ปีที่แล้ว

      because than new table has distinct values

  • @dreambig1668
    @dreambig1668 4 ปีที่แล้ว

    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

  • @arjunpawar4564
    @arjunpawar4564 3 ปีที่แล้ว

    In third method : Can we access alias rn in outer query as well as use in where condition ?

  • @sandysahu5651
    @sandysahu5651 2 ปีที่แล้ว

    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

  • @sathyasathyam6930
    @sathyasathyam6930 3 ปีที่แล้ว

    Super thank 🌹you

  • @ankittiwari-pw6nr
    @ankittiwari-pw6nr 4 ปีที่แล้ว

    Thanks

  • @leesubiramaniyam2704
    @leesubiramaniyam2704 2 ปีที่แล้ว

    Explaining good & may be increase the volume (sound) better good

  • @simranjeetsingh4101
    @simranjeetsingh4101 ปีที่แล้ว

    Thanku

  • @music4u1235
    @music4u1235 ปีที่แล้ว

    so sNice tutorialt like that

  • @joshuasamuel5851
    @joshuasamuel5851 4 ปีที่แล้ว

    Superb

  • @labheshlalka9666
    @labheshlalka9666 3 ปีที่แล้ว +1

    Why we can't use direct delete query whose count greater than 1

  • @yes-pg5dy
    @yes-pg5dy 3 ปีที่แล้ว

    Excellent madam pls do videos on power bi also pls

  • @rk-ej9ep
    @rk-ej9ep 3 ปีที่แล้ว

    Very nice

  • @divyasnarayan1803
    @divyasnarayan1803 3 ปีที่แล้ว

    Gr8 wrk!

  • @mannu567
    @mannu567 ปีที่แล้ว

    Instead of MIN if we use Max then what will happen? I guess it will also remove duplicate

  • @mohammedyousuf1918
    @mohammedyousuf1918 2 ปีที่แล้ว

    Super

  • @MrsBestYT
    @MrsBestYT 2 ปีที่แล้ว

    Good

  • @helmiboubaker2742
    @helmiboubaker2742 3 ปีที่แล้ว +1

    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 :/

    • @mobileminimaster3015
      @mobileminimaster3015 3 ปีที่แล้ว

      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 );

  • @Almay20
    @Almay20 3 ปีที่แล้ว

    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?

    • @nihar4713
      @nihar4713 3 ปีที่แล้ว +1

      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

  • @ravindrabisht7372
    @ravindrabisht7372 3 ปีที่แล้ว

    Your English is good