How do you insert data into a PostgreSQL table?
Why Interviewers Ask This
Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for PostgreSQL development. It reveals whether you understand the building blocks that more complex concepts rely on.
Answer
Use the INSERT INTO statement. Single row: INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com');. Multiple rows: INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com'), ('Carol', 'carol@example.com');. Return the inserted row: INSERT INTO users (name) VALUES ('Dave') RETURNING id, name; — the RETURNING clause is a PostgreSQL extension very useful for getting generated IDs. Insert from a query: INSERT INTO archive SELECT * FROM users WHERE created_at < '2023-01-01';. Use ON CONFLICT for upsert behavior (covered under intermediate topics).
Pro Tip
If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.