Skip to content
Advertisement

Delete statement in SQL Server [closed]

I saw a delete statement written in below format and I need help in understanding it.

I tried above in online SQL Server database but script is failing. I am not sure how similar script is running in production environment.

Advertisement

Answer

The DELETE statement has the following syntax:

When pinpointing the parts from your example, you get:

Being <object> the following:

So what your DELETE statement is doing is joining table t1 with t2 via a condition on the WHERE clause, and then deleting all matching records from table t1.


Regular SQL syntax requires is to filter your rows from a WHERE clause:

Transact-SQL has a few extensions to this base format. First, the FROM is optional, so you can write:

and second, you can filter rows from your deleting table through joining against other table sources (which is pretty common), so they give you the option to move some of your WHERE conditions to an optional, new FROM <table_source> clause.

The FROM from the first point is still optional, you can also write:

And you can put your joining conditions on either a JOIN:

or the WHERE:

But I suggest using the former, since it’s easier to read for bigger queries.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement