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 |
How does index makes search faster?
What types of integrity are enforced by a foreign-key constraint
What are the differences between left join and inner join in sql server?
What are the different types of backup avaible in SQL SErver
What are rows and columns?
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 .
What does it mean to have quoted_identifier on? What are the implications of having it off?
How you can get the list of largest tables in a database?
What is the difference between varchar and varchar(max) datatypes?
when would you go for denormalization? : Sql server database administration
How to create new tables with "create table" statements in ms sql server?
Which table keeps the locking information?
Oracle (3253)
SQL Server (4518)
MS Access (429)
MySQL (1402)
Postgre (483)
Sybase (267)
DB Architecture (141)
DB Administration (291)
DB Development (113)
SQL PLSQL (3330)
MongoDB (502)
IBM Informix (50)
Neo4j (82)
InfluxDB (0)
Apache CouchDB (44)
Firebird (5)
Database Management (1411)
Databases AllOther (288)