MSSQL Incorrect syntax near p1

Advertisements

I am trying to solve this question from leetcode.

I wrote my query as below:

delete from person p1
where not exists
(
    select p2.email,min(p2.id)
    from person p2
    group by p2.email
    having min(p2.id)=p1.id
)

But I am getting the error:

[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near ‘p1’. (102) (SQLExecDirectW)

Why am i getting this error?

>Solution :

the error message was very clear(error syntax), you don’t have to make alias for table you want to delete.

delete from person
where not exists
(
    select p2.email,min(p2.id)
    from person p2
    group by p2.email
    having min(p2.id)=person.id
)

Leave a ReplyCancel reply