Delete Duplicate Records from table which does not have primary Key
Hello friends
Greetings !
I will share an interesting Query that Delete records from table which doesn't have any Key (primary Key,Unique Key ).
Step 1 : Create a table named as empName
run the following query
CREATE TABLE [dbo].[emp](
[empId] [int] ,
[empName] [nvarchar](max) NULL
)
Step 2 : Insert records into the table empName
INSERT INTO [emp] VALUES (1,'Shubhank')
INSERT INTO [emp] VALUES (2,'Viru')
INSERT INTO [emp] VALUES (3,'Shubhank')
INSERT INTO [emp] VALUES (4,'Maths')
INSERT INTO [emp] VALUES (5,'Shubhank')
INSERT INTO [emp] VALUES (6,'Shubhank')
INSERT INTO [emp] VALUES (7,'Denis')
INSERT INTO [emp] VALUES (8,'Gates')
INSERT INTO [emp] VALUES (9,'Linux')
INSERT INTO [emp] VALUES (10,'Shubhank')
INSERT INTO [emp] VALUES (11,'Gary')
INSERT INTO [emp] VALUES (12,'Rajkishan')
INSERT INTO [emp] VALUES (13,'Shubhank')
INSERT INTO [emp] VALUES (14,'Rajiv')
INSERT INTO [emp] VALUES (15,'Mak')
INSERT INTO [emp] VALUES (16,'Gary')
INSERT INTO [emp] VALUES (17,'Raj')
step 3: Now in that table there are so many duplicated records of name 'Shubhank' having 6 records , and there is no primary key ,
then how can you delete only two records having with name 'shubhank' out of six.
Step 4 : Here is the Query run it
DELETE TOP(2) from emp Where
empName LIKE 'Shubhank%'
Step 5 : Now run the Query
SELECT COUNT(*) FROM emp Where
empName LIKE 'Shubhank%'
you will get four in result set.
Thanks
Shubhank Upadhyay
Greetings !
I will share an interesting Query that Delete records from table which doesn't have any Key (primary Key,Unique Key ).
Step 1 : Create a table named as empName
run the following query
CREATE TABLE [dbo].[emp](
[empId] [int] ,
[empName] [nvarchar](max) NULL
)
Step 2 : Insert records into the table empName
INSERT INTO [emp] VALUES (1,'Shubhank')
INSERT INTO [emp] VALUES (2,'Viru')
INSERT INTO [emp] VALUES (3,'Shubhank')
INSERT INTO [emp] VALUES (4,'Maths')
INSERT INTO [emp] VALUES (5,'Shubhank')
INSERT INTO [emp] VALUES (6,'Shubhank')
INSERT INTO [emp] VALUES (7,'Denis')
INSERT INTO [emp] VALUES (8,'Gates')
INSERT INTO [emp] VALUES (9,'Linux')
INSERT INTO [emp] VALUES (10,'Shubhank')
INSERT INTO [emp] VALUES (11,'Gary')
INSERT INTO [emp] VALUES (12,'Rajkishan')
INSERT INTO [emp] VALUES (13,'Shubhank')
INSERT INTO [emp] VALUES (14,'Rajiv')
INSERT INTO [emp] VALUES (15,'Mak')
INSERT INTO [emp] VALUES (16,'Gary')
INSERT INTO [emp] VALUES (17,'Raj')
step 3: Now in that table there are so many duplicated records of name 'Shubhank' having 6 records , and there is no primary key ,
then how can you delete only two records having with name 'shubhank' out of six.
Step 4 : Here is the Query run it
DELETE TOP(2) from emp Where
empName LIKE 'Shubhank%'
Step 5 : Now run the Query
SELECT COUNT(*) FROM emp Where
empName LIKE 'Shubhank%'
you will get four in result set.
Thanks
Shubhank Upadhyay
Comments
Post a Comment