The basic syntax for DELETE is
DELETE FROM table
WHERE condition
Is there a straightforward way to use subquery/alias in a DELETE statement, something like below?
DELETE FROM (subquery) as sub
WHERE condition
Below is a minimal working table and my failed attempt to use subquery/alias:
---create table
create table orderT (
id integer PRIMARY KEY,
country_code varchar(2),
created_date date,
closed_date date);
---populate table
INSERT INTO orderT VALUES (1, 'US', now(), now() + interval '1 day' * 21);
INSERT INTO orderT VALUES (2, 'CA', now(), now() + interval '1 day' * 35);
--This does not work
DELETE
FROM
(SELECT *
FROM orderT) AS sub
WHERE sub.id = 1;
You can try the code here.
PostgreSQL 9.5
... In my actual script, the subquery is more 90 lines long.Mine is over 300 Lines! -->> please don't hide your ignorance behind impressive numbers.