Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I use an alias in a DELETE statement?

People also ask

Can we use alias in DELETE query?

Recently I wrote about the myth that you can't use an alias in an UPDATE statement. You can of course, and the trick is to make sure that the alias is used in the FROM clause of the statement.

Can an alias be used in an expression?

Aliases are often used to name a column that is the result of an arithmetic expression or summary function. An alias is one word only. If you need a longer column name, then use the LABEL= column-modifier, as described in column-modifier.

What happens if you don't use the WHERE clause in your DELETE statement?

If you run a DELETE statement with no conditions in the WHERE clause, all of the records from the table will be deleted.

Can you use a top in a DELETE statement?

Example - Using TOP keyword Let's look at a SQL Server example, where we use the TOP keyword in the DELETE statement. For example: DELETE TOP(10) FROM employees WHERE last_name = 'Anderson'; This SQL Server DELETE TOP example would delete the first 10 records from the employees table where the last_name is 'Anderson'.


To alias the table you'd have to say:

DELETE f FROM dbo.foods AS f WHERE f.name IN (...);

I fail to see the point of aliasing for this specific DELETE statement, especially since (at least IIRC) this no longer conforms to strict ANSI. But yes, as comments suggest, it may be necessary for other query forms (eg correlation).


The delete statement has strange syntax. It goes like this:

DELETE f FROM foods f WHERE (f.name IN ('chickens', 'rabbits'))