Skip to main content
PostgreSQL
CHAPTER 06 Intermediate

INSERT, UPDATE, and DELETE Queries

Updated: May 16, 2026
6 min read

# CHAPTER 6

INSERT, UPDATE, and DELETE Queries

1. Introduction

You have successfully built an empty employees table. But an empty table is useless. A database's primary job is to manage the flow of living data. We call this Data Manipulation Language (DML). In the software industry, this is often referred to as "CUD" (Create, Update, Delete). In this chapter, we will write the essential SQL commands that power every single registration form, profile editor, and deletion button on the internet.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Use INSERT INTO to add new single or multiple rows.
  • Use UPDATE to modify existing data.
  • Understand the critical importance of the WHERE clause.
  • Use DELETE to safely erase rows.
  • Utilize the PostgreSQL-exclusive RETURNING clause.

3. Inserting Data (INSERT INTO)

When a user fills out a "Sign Up" form on a website, the backend server converts their inputs into an INSERT INTO statement and sends it to PostgreSQL.

Syntax:

sql
12
INSERT INTO table_name (column1, column2, column3) 
VALUES (value1, value2, value3);

Let's insert our first employee into the table we built in Chapter 5. *(Note: Strings and Dates must be wrapped in single quotes ''. Numbers and Booleans do not).*

sql
12
INSERT INTO employees (first_name, email, salary, hire_date, is_manager) 
VALUES ('John', 'john@company.com', 55000.00, '2024-01-15', TRUE);

*(Notice we completely ignored the emp_id column! Because we defined it as SERIAL, PostgreSQL will automatically generate ID #1 for John).*

Inserting Multiple Rows at Once: You don't need to write the INSERT INTO command 50 times. You can chain values!

sql
12345
INSERT INTO employees (first_name, email, salary) 
VALUES 
('Sarah', 'sarah@company.com', 85000.00),
('Mike', 'mike@company.com', 45000.00),
('Anna', 'anna@company.com', 120000.00);

4. Updating Data (UPDATE)

Employees get raises. People change their email addresses. We use the UPDATE command to modify existing rows.

The Golden Rule: You almost *always* must use a WHERE clause to target a specific row. If you forget the WHERE clause, PostgreSQL will update EVERY SINGLE ROW in the entire table!

sql
123456789
-- Let's give Sarah (ID 2) a massive raise!
UPDATE employees 
SET salary = 95000.00 
WHERE emp_id = 2;

-- We can update multiple columns at once
UPDATE employees 
SET salary = 50000.00, is_manager = FALSE 
WHERE emp_id = 3;

5. Deleting Data (DELETE)

When an employee quits, we must remove their record from the active database.

The Golden Rule applies here too: If you forget the WHERE clause, you will wipe out the entire table in a millisecond!

sql
123
-- Safely delete Mike from the company
DELETE FROM employees 
WHERE emp_id = 3;

6. The PostgreSQL Superpower: RETURNING

In standard SQL, if you INSERT a row, the database just replies "Success." But what if your PHP backend immediately needs to know what auto-incremented ID was assigned to that new row? PostgreSQL features the incredible RETURNING clause. It allows an INSERT, UPDATE, or DELETE statement to instantly hand data back to the application!
sql
12345678
-- Insert a user, and instantly return the ID that was generated!
INSERT INTO employees (first_name, email) 
VALUES ('David', 'david@company.com')
RETURNING emp_id;

-- Output: 
-- emp_id
-- 5

7. Mini Project: Manage Employee Records

Let's do a quick simulation of an employee lifecycle using RETURNING.
sql
123456789101112131415
-- 1. Hire a new employee and get their ID
INSERT INTO employees (first_name, salary) 
VALUES ('Eve', 70000.00)
RETURNING emp_id, first_name;

-- 2. Oops, we forgot her email. Update it and confirm!
UPDATE employees 
SET email = 'eve@company.com' 
WHERE emp_id = 6
RETURNING email;

-- 3. Eve was fired. Remove her and return the deleted record for the log!
DELETE FROM employees 
WHERE emp_id = 6
RETURNING *;

8. Common Mistakes

  • String Quote Errors: Typing VALUES (John, 55000) will fail. PostgreSQL will think John is the name of a column! Text must be wrapped in single quotes: VALUES ('John', 55000).
  • Double Quotes: In PostgreSQL, single quotes ' ' are for Strings/Text. Double quotes " " are strictly for forcing case-sensitivity on Table/Column names. Never use double quotes for string values!

9. Best Practices

  • Never rely on Table Order: When writing INSERT INTO employees VALUES ('John', 55000); (without specifying the columns in parentheses), PostgreSQL assumes you are inserting data in the exact order the table was created. If someone adds a column to the table later, your query will crash. Always explicitly declare the columns: INSERT INTO employees (name, salary) VALUES...

10. Exercises

  1. 1. Write the SQL command to update the ismanager status of all employees who make exactly $120,000 to TRUE.
  1. 2. What does the RETURNING * clause do when appended to a DELETE statement?

11. SQL Challenges

Write a single query that deletes all employees who do not have an email address (where the email is NULL), and returns the first
name of the people who were deleted.
sql
123
DELETE FROM employees 
WHERE email IS NULL 
RETURNING first_name;

12. MCQ Quiz with Answers

Question 1

Which SQL clause is absolutely critical when executing an UPDATE or DELETE command to prevent modifying the entire table at once?

Question 2

What is the primary benefit of appending RETURNING id to an INSERT statement in PostgreSQL?

13. Interview Questions

  • Q: Explain the difference between single quotes (') and double quotes (") in PostgreSQL syntax.
  • Q: Describe a backend web development scenario where the PostgreSQL RETURNING clause drastically improves application efficiency.

14. FAQs

Q: Can I undo an UPDATE command if I messed up and forgot the WHERE clause? A: By default, no. The data is destroyed. However, if you wrap your commands in a "Transaction" (which we cover in Chapter 20), you can type ROLLBACK; to time-travel and undo the mistakes!

15. Summary

You are now capable of altering the reality of a database. By mastering INSERT, UPDATE, and DELETE, you can populate empty tables with millions of rows, modify their attributes, and prune outdated records. By utilizing RETURNING, you bridge the gap between database mutations and backend application logic. Just remember: with great power comes the massive responsibility of the WHERE clause.

16. Next Chapter Recommendation

Our database is now full of employee data. How do we get it back out? How do we find all employees making over $80,000? In Chapter 7: SELECT Queries and Filtering Data, we will learn the most commonly used command in the entire SQL language.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·