SQL Query to remove Duplicate values from table. Session 3

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 ต.ค. 2024
  • SQL Queries and Interview Questions
     Get duplicate data
     Remove duplicate data
     The GROUP BY statement groups rows that have the same values into summary rows.
     A Common Table Expression, also called as CTE in short form, is a temporary named result set that you can reference within a SELECT, INSERT, UPDATE, or DELETE statement that immediately follows the CTE. The CTE can also be used in a View.
    --------------------------------------------------------------------------------------------------------------------------------------------------
    CREATE TABLE [customer3](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [name] [varchar](20) NULL,
    [place] [varchar](30) NULL,
    [gender] [char](10) NULL,
    [salary] [int] NULL
    )
    --------------------------------------------------------------------------------------------------------------------------------------------------
    insert into customer3 values('sunny', 'noida', 'male', 1000)
    insert into customer3 values('rahul', 'delhi', 'male', 2000)
    insert into customer3 values('mahesh', 'goa', 'male', 3000)
    insert into customer3 values('suresh', 'USA', 'male', 4000)
    ----------------------------------------------------------------------------------------------------------------------------------------------------
    with test as
    (
    select name, place,gender,
    row_number() over(partition by name, place,gender order by id)
    as rownumber from customer3
    )
    select * from ctename where rownumber (GreaterThanSign) 1
    delete from test where rownumber (GreaterThanSign) 1
    ---------------------------------------------------------------------------------------------------------------------------------------------------
    select name, place, gender, count(*) as rownumber from customer3 group by name, place, gender having count(*) (GreaterThanSign )1
    delete from customer3 where id not in(select min(id) from customer3 group by name, place, gender)

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

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

    I really loved to watch this session of fetching and removing duplicate records. Thank you vaia.

  • @yogeshpandita7828
    @yogeshpandita7828 9 หลายเดือนก่อน +2

    It's really amazing to see you even covering small details. Thank you.

  • @nishapathania146
    @nishapathania146 9 หลายเดือนก่อน +1

    Very nice explain sir

  • @heemozero4035
    @heemozero4035 5 หลายเดือนก่อน

    ❤❤ thanks

  • @vinothrajr1221
    @vinothrajr1221 11 หลายเดือนก่อน

    Awesome explain

  • @Mr.nikk62
    @Mr.nikk62 8 หลายเดือนก่อน

    great Sir

  • @hanumanprasadvishwakarma9273
    @hanumanprasadvishwakarma9273 8 หลายเดือนก่อน

    without using aggregate function how we can use group by clause
    it should give error

  • @utsavsagar6665
    @utsavsagar6665 8 หลายเดือนก่อน

    Can we execute the beloq query?
    Delete from person
    Where email in(select email from person group by email
    Having count(email)>1)

    • @sunnypandita1498
      @sunnypandita1498  8 หลายเดือนก่อน

      Hi Sagar, thanks for your comment. With your query, it will delete duplicate data including original data also. But we need only duplicate data to get removed. There are several ways to get the same result. I had showed the one, there are many other simple ways too which can be used.

  • @chazc080488
    @chazc080488 7 หลายเดือนก่อน

    In mysql when trying to delete the duplicates, Im getting a message that says"table doesn't exist". How are you able to delete from a cte?

    • @sunnypandita1498
      @sunnypandita1498  6 หลายเดือนก่อน

      Hi, thanks for your comment. Table doesn't exist means there is no existing table. So, first need to create the table with same structure which is used in the session and next we need to execute CTE and next query together to perform the deletion..