Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

where clause in delete statement with cte not working as expected #850

Closed
Tejas360 opened this issue Apr 22, 2024 · 1 comment
Closed

where clause in delete statement with cte not working as expected #850

Tejas360 opened this issue Apr 22, 2024 · 1 comment

Comments

@Tejas360
Copy link

CREATE TABLE IF NOT EXISTS person(id integer , email text);
INSERT INTO person VALUES (1,'abc@gmail.com'),
                        (2,'abc@gmail.com'),
                         (3,'apple@gail.com');
WITH cte AS 
( 
SELECT email, ROW_NUMBER () over(PARTITION BY email) AS row_no  FROM person 
)DELETE FROM person USING cte WHERE row_no>1;

SELECT * FROM person ;

this code should only delete the rows with row_no value grater than 1 but it is deleting all the rows from a table ,how and why?
I checked with different row_no values like grater than 2 or 3 then it's working as expected not deleting a single row as no row_number grater than 2 or 3 but as soon as it is hitting 1 it is deleting all the rows

@drocha87
Copy link

drocha87 commented Apr 22, 2024

I don't think this is a problem related with this libray what so ever. Your query doesn't work as expected on psql either.

I'm not a sql expert, but I don't think you're using the DELETE ... USING properly. Take a look at https://www.postgresql.org/docs/current/sql-delete.html mainly in NOTESsection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants