PostgreSQL
databasepostgresqlGuideBeginner
PostgreSQL CRUD Operations & RETURNING Clause
Master INSERT, SELECT, UPDATE, DELETE, and upsert (ON CONFLICT) queries using PostgreSQL's powerful RETURNING clause.
Verified: 2026-09-30
Tested on:postgres 16.x
Platforms:
linuxmacoswindows
#postgresql#crud#sql#insert#returning
1. Create (INSERT) with RETURNING
In PostgreSQL, the RETURNING clause returns modified or inserted rows directly to the application without requiring a secondary SELECT query:
-- Single row insert returning generated ID
INSERT INTO users (username, email)
VALUES ('alice', 'alice@example.com')
RETURNING id, created_at;
-- Bulk insertion
INSERT INTO products (title, price, stock)
VALUES
('Mechanical Keyboard', 120.00, 50),
('Wireless Mouse', 45.00, 100),
('USB-C Hub', 35.00, 75)
RETURNING *;2. Read (SELECT)
-- Select specific columns with alias
SELECT
id,
username,
email AS contact_email
FROM users;
-- Limit and Offset
SELECT * FROM products
LIMIT 10 OFFSET 20;3. Update (UPDATE) with RETURNING
-- Update record and return updated timestamp
UPDATE users
SET
is_active = false,
updated_at = NOW()
WHERE id = 42
RETURNING id, username, is_active, updated_at;4. Delete (DELETE) with RETURNING
-- Delete inactive users and return their emails for audit logging
DELETE FROM users
WHERE is_active = false AND created_at < NOW() - INTERVAL '1 year'
RETURNING email, id;5. Upsert (INSERT ... ON CONFLICT)
Perform an atomic insert or update in a single statement:
INSERT INTO user_settings (user_id, theme, notifications)
VALUES (1, 'dark', true)
ON CONFLICT (user_id)
DO UPDATE SET
theme = EXCLUDED.theme,
notifications = EXCLUDED.notifications,
updated_at = NOW()
RETURNING *;PostgreSQL Tables & Schema Definition
Master CREATE TABLE, modern IDENTITY columns, ALTER TABLE migrations, and data type selection in PostgreSQL.
PostgreSQL WHERE Clause & Filtering
Filter query results using WHERE operators, logical combinations (AND, OR, NOT), pattern matching (LIKE, ILIKE), and NULL checks.