How can we remove orphan records from a table?



How can we remove orphan records from a table?..

Answer / Jitendra Singh Yadav

In SQL Server, you can use the DELETE statement with JOIN to delete orphan records. Here's an example using self-join:

```sql
DELETE A
FROM TableA AS A
JOIN (
SELECT ParentID, MAX(ID) as MaxID
FROM TableA
GROUP BY ParentID
) AS B ON A.ParentID = B.ParentID AND A.ID < B.MaxID
```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More SQL Server Interview Questions

How does index makes search faster?

1 Answers   QuestPond,


What types of integrity are enforced by a foreign-key constraint

1 Answers  


What are the differences between left join and inner join in sql server?

1 Answers  


What are the different types of backup avaible in SQL SErver

6 Answers   Emphasis, IBM,


What are rows and columns?

1 Answers  


i have account table which consists of account name,card no and card no consists 16 digits now i want to retrieve the data if card no starts from 4 then it should print visa and if it starts from 5 then it should print master so plse help me to write simple query with out store procs .

3 Answers  


What does it mean to have quoted_identifier on? What are the implications of having it off?

2 Answers  


How you can get the list of largest tables in a database?

1 Answers  


What is the difference between varchar and varchar(max) datatypes?

1 Answers  


when would you go for denormalization? : Sql server database administration

1 Answers  


How to create new tables with "create table" statements in ms sql server?

1 Answers  


Which table keeps the locking information?

1 Answers  


Categories