How do you insert, update, delete and query data in PostgreSQL?
BeginnerHands-onPostgreSQL
Answer
Short answer
With standard SQL: INSERT, SELECT, UPDATE, DELETE. PostgreSQL adds handy extras like RETURNING (get back the rows you changed) and ON CONFLICT (upsert).
Examples
INSERT INTO customers (name, email) VALUES ('Asha', '[email protected]') RETURNING id;
SELECT id, name FROM customers WHERE email LIKE '%@ex.com' ORDER BY id DESC LIMIT 10;
UPDATE orders SET status = 'SHIPPED' WHERE id = 42 RETURNING id, status;
DELETE FROM sessions WHERE expires_at < now();
-- Upsert: insert, or update if the email already exists
INSERT INTO customers (email, name) VALUES ('[email protected]', 'Asha J')
ON CONFLICT (email) DO UPDATE SET name = EXCLUDED.name;
-- Aggregation
SELECT status, count(*), sum(total) FROM orders GROUP BY status;
Safe changes on production
BEGIN;
UPDATE orders SET status = 'CANCELLED' WHERE created_at < '2026-01-01' AND status = 'NEW';
-- psql prints: UPDATE 312 → is that the number you expected?
SELECT count(*) FROM orders WHERE status = 'CANCELLED';
COMMIT; -- or ROLLBACK;
What to say in the interview
"Standard SQL DML, plus Postgres extras like RETURNING and ON CONFLICT for upserts. On production I run changes inside BEGIN, check the affected row count, then COMMIT or ROLLBACK."
More on PostgreSQL
How do you set up PostgreSQL and log in with psql?BeginnerHow do you create a database, tables, users and query data in PostgreSQL?BeginnerHow do you back up and restore a PostgreSQL database?BeginnerWhat is PostgreSQL and why is it so popular?BeginnerHow is PostgreSQL organised: cluster, database, schema and table?BeginnerHow do you connect to PostgreSQL from the command line?Beginner