PostgreSQL — Delete
Basic Delete
DELETE FROM users WHERE id = 1;
Delete All Rows
DELETE FROM users;
With Subquery
DELETE FROM users
WHERE id IN (SELECT id FROM inactive_users);
Returning
DELETE FROM users
WHERE id = 1
RETURNING *;
Using Limit
DELETE FROM logs
WHERE created_at < '2024-01-01'
LIMIT 1000;
Truncate vs Delete
| Feature | DELETE | TRUNCATE |
|---|---|---|
| Speed | Slower | Faster |
| WHERE clause | Yes | No |
| RETURNING | Yes | No |
| Triggers | Yes | No |
| Rollback | Yes | Yes |
Truncate
TRUNCATE TABLE logs;
TRUNCATE TABLE logs RESTART IDENTITY;
Mini Practice
- Delete specific rows
- Delete all rows
- Use RETURNING
- Compare DELETE and TRUNCATE
Up Next
Continue with Order By — sorting results.
Related Topics
Frequently Asked Questions about Delete
What is Delete in PostgreSQL?
Delete is a fundamental concept in PostgreSQL. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Delete?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Delete.
Why is Delete important in PostgreSQL?
Delete is essential for PostgreSQL development. Understanding this concept will help you write better code and solve real-world problems more effectively.