Delete Selected SQL Server Rows with a Table Alias

SQL Server lets you identify the delete target by an alias and use a FROM clause to describe the matching rows. The alias immediately after DELETE determines which table loses rows. Other joined tables provide selection criteria and are not deleted automatically.

Build and Preview the Exact Selection

Run this demonstration in a fresh session without an active transaction:

SQL
CREATE TABLE #Items (ItemId int NOT NULL PRIMARY KEY, Label nvarchar(30));
CREATE TABLE #ApprovedRemoval (ItemId int NOT NULL PRIMARY KEY);
INSERT #Items VALUES (1, N'Keep'), (2, N'Remove'), (3, N'Keep also');
INSERT #ApprovedRemoval VALUES (2);

SELECT i.ItemId, i.Label
FROM #Items AS i
JOIN #ApprovedRemoval AS r ON r.ItemId = i.ItemId;

The preview returns only item 2. Keep the delete predicate aligned with the reviewed selection. The approval table's unique key prevents duplicate selection identifiers in this example.

Delete through the Alias and Roll Back

Continue in the same session:

SQL
SET XACT_ABORT ON;
BEGIN TRY
    BEGIN TRANSACTION;

    DELETE i
    OUTPUT deleted.ItemId, deleted.Label
    FROM #Items AS i
    JOIN #ApprovedRemoval AS r ON r.ItemId = i.ItemId;

    DECLARE @Removed int = @@ROWCOUNT;
    IF @Removed <> 1
        THROW 50002, 'Expected exactly one deleted row.', 1;

    SELECT ItemId, Label FROM #Items ORDER BY ItemId;
    ROLLBACK TRANSACTION;
END TRY
BEGIN CATCH
    IF XACT_STATE() <> 0 ROLLBACK TRANSACTION;
    THROW;
END CATCH;

SELECT ItemId, Label FROM #Items ORDER BY ItemId;
DROP TABLE #ApprovedRemoval;
DROP TABLE #Items;

OUTPUT identifies item 2, and the SELECT inside the transaction shows only items 1 and 3. The final SELECT shows all three because the delete was rolled back. The DELETE i target removes rows only from #Items.

Check the Real Table's Side Effects

Before committing an adapted operation, inspect foreign keys, cascade actions and triggers. A delete can affect dependent data even when its direct target is narrow. OUTPUT rows do not establish that the transaction committed successfully.

Be especially careful with synchronization logic that deletes rows absent from a staging table. An unexpectedly empty staging load can turn an absence test into a request to remove everything. Validate that the load completed and that its scope is correct before deriving deletion candidates. For a deliberate absence test, prefer an explicit NOT EXISTS relationship and define the treatment of null identifiers.

Do not copy private operational schemas or unexplained customer flags into a reusable cleanup script. Make the target, candidate selection and expected count visible in the actual implementation.

Microsoft References

Author: Herman Schoenfeld