Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2018 June SQL Puzzle: Delete Duplicate Data without Primary key, ROW_NUMBER()

SQL Puzzle: Delete Duplicate Data without Primary key, ROW_NUMBER()

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

Check the below input data and expected output for deleting the duplicate data by giving an ID. The table doesn’t have any primary key and writes logic without using ROW_NUMBER().

Input Data:

1
2
3
4
5
6
7
8
9
10
11
ID Name
----------- ----------
1 ABC
1 ABC
1 ABC
3 ERZ
3 ERZ
4 HYU
5 BNM
7 WER
7 WER

Expected Output:

1
2
3
4
5
6
7
8
9
ID Name
----------- ----------
1 ABC
3 ERZ
3 ERZ
4 HYU
5 BNM
7 WER
7 WER

Create a sample table:

1
2
3
4
5
6
CREATE TABLE tbl_TestTable (ID INT, Name VARCHAR(10))
 
INSERT INTO tbl_TestTable
VALUES (1,'ABC'),(1,'ABC'),(3,'ERZ')
,(4,'HYU'),(5,'BNM'),(1,'ABC')
,(7,'WER'),(3,'ERZ'),(7,'WER')

Solution:

1
2
3
4
5
;WITH CTE AS
(
SELECT COUNT(1) AS IDCount, ID FROM tbl_TestTable GROUP BY ID
)
DELETE TOP((SELECT IDCount FROM CTE WHERE ID = 1) - 1) FROM tbl_TestTable WHERE ID = 1

Please try the different solution for this puzzle and share it via comment...

Jun 4, 2018Anvesh Patel
SQL Server Coding Standards: Working with Error Handling and Transaction SupportSQL Server 2014: Examples of Table Variable with Indexes
Comments: 7
  1. maniva
    June 5, 2018 at 11:46 am

    thank you for sharing,

    ReplyCancel
  2. Arezou
    July 17, 2018 at 9:31 am

    Hi Mr Patel,Thank you for sharing, but i wrote this query and i got correct result.

    delete from dbo.t2
    where id in
    (
    Select id
    From dbo.t2
    Group By Name,id
    Having count(id) >1
    )

    ReplyCancel
  3. Lili Rad
    August 2, 2018 at 6:27 am

    And this code work šŸ™‚

    with cte as
    (select *, ROW_NUMBER() over (partition by id order by id) as broj from tbl_TestTable)
    delete from cte where broj > 1

    ReplyCancel
  4. Lili Rad
    August 2, 2018 at 8:57 am

    This work too:

    with cte as
    (select *, ROW_NUMBER() over (partition by id order by id) as broj from #tbl_TestTable)
    delete from cte where broj > 1

    ReplyCancel
  5. Sadaf
    August 29, 2018 at 7:56 pm

    Delete from tableName
    Where Field1 NOT IN (
    SELECT MAX(Field1) FROM tableName
    GROUP BY DuplicateField1, DuplicateField2..)

    ReplyCancel
  6. Praful Raut
    October 10, 2018 at 6:11 am

    DELETE TOP(1) FROM #TBL_TESTTABLE WHERE ID IN (SELECT ID FROM #TBL_TESTTABLE GROUP BY ID HAVING COUNT(ID) >=3)

    ReplyCancel
  7. Ovidiu-Ionel Samfirescu
    January 14, 2019 at 8:10 pm

    DELETE TOP (
    SELECT COUNT(*)-1 count FROM tbl_TestTable WHERE id =1 GROUP BY id, name) FROM tbl_TestTable WHERE id = 1

    ReplyCancel

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

June 4, 2018 7 Comments SQL PuzzleAnvesh Patel, database, database research and development, dbrnd, SQL Advance Query, sql commands, SQL Interview, SQL Problem, SQL Programming, SQL Puzzle, SQL Query, sql quiz, SQL Tips and Tricks, sqlcode
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
Ā© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....