Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MSSQL Incorrect syntax near p1

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[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
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading