-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add postgres cross table row deletions til
- Loading branch information
Showing
1 changed file
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Cross-Table Row Deletions | ||
|
||
*Context* | ||
Need to `DELETE` all rows from a table satisfying a certain condition based on a value in another table. | ||
|
||
*Assumptions* | ||
Table: `Person` | ||
`Person` Columns: `person_id`, `city_id`, `email` | ||
|
||
Table: `City` | ||
`City` Columns: `city_id`, `name`, `population` | ||
|
||
```sql | ||
CREATE TABLE City ( | ||
city_id INT PRIMARY KEY, | ||
name TEXT, | ||
population INT | ||
); | ||
|
||
CREATE TABLE Person ( | ||
person_id INT PRIMARY KEY, | ||
email TEXT, | ||
city_id INT, | ||
FOREIGN KEY (city_id) REFERENCES City(city_id) | ||
); | ||
|
||
INSERT INTO | ||
City (city_id, name, population) | ||
VALUES | ||
(1, 'New York', 8419000), | ||
(2, 'Los Angeles', 3971000), | ||
(3, 'Chicago', 2716000), | ||
(4, 'Houston', 2325500), | ||
(5, 'Phoenix', 1680000); | ||
|
||
INSERT INTO | ||
Person (person_id, email, city_id) | ||
VALUES | ||
(1, '[email protected]', 1), | ||
(2, '[email protected]', 2), | ||
(3, '[email protected]', 3), | ||
(4, '[email protected]', 4), | ||
(5, '[email protected]', 5); | ||
``` | ||
|
||
*Goal* | ||
Delete all people from cities with a population of less than 1,000,000 people. | ||
|
||
*Solution* | ||
Standard-compliant solution: | ||
|
||
```sql | ||
DELETE FROM Person | ||
WHERE | ||
city_id IN ( | ||
SELECT | ||
city_id | ||
FROM | ||
City | ||
WHERE | ||
population < 1000000 | ||
); | ||
``` | ||
|
||
`USING` (Extension)[^1] solution: | ||
|
||
```sql | ||
DELETE FROM Person USING City | ||
WHERE | ||
Person.city_id = City.city_id | ||
AND City.population < 1000000; | ||
``` | ||
|
||
[^1]: [This command does not conform to the SQL standard](https://www.postgresql.org/docs/current/sql-delete.html). `USING` and `RETURNING` clauses | ||
are postgres exentions. |