SQL CRUD Operations in PostgreSQL — SELECT, INSERT, UPDATE, DELETE
Learn SQL CRUD operations in PostgreSQL with practical examples for SELECT, INSERT, UPDATE, and DELETE. Ideal for beginners learning database queries and data manipulation in SQL.
Overview
SELECT Statement: Used to retrieve data using clauses like WHERE, ORDER BY, and LIMIT.
INSERT Statement: Used to add new records into a table using single quotes for text values.
UPDATE Statement: Used to modify existing records, requiring a WHERE clause to avoid changing every row.
DELETE Statement: Used to remove records, requiring a WHERE clause to prevent deleting an entire table.
Task: After inserting the record, use SELECT to verify it.
Exercise 10: Add another customer
Insert another customer using your own sample information.
Tasks:
Insert the customer.
Display the inserted customer.
Confirm that the information is correct.
Part C — UPDATE Practice
Exercise 11: Update email
Update the email address of the customer you inserted in Exercise 9.
Change it to:
ali.new@example.com
Then verify the change using SELECT.
Exercise 12: Update customer name
Change the last name of your inserted customer.
Example: Khan → Ahmed
Exercise 13: Update rental rate
Find a film with film_id = 1.
Change its rental rate to 3.99 and verify the result.
Exercise 14: Update multiple columns
For your inserted customer, update:
First name
Last name
Email
using a single UPDATE statement.
Part D — DELETE Practice
Exercise 15: Delete a customer
Delete the customer that you inserted in Exercise 9.
Important: Use the appropriate WHERE condition.
Then verify that the customer has been deleted.
Exercise 16: Delete another test record
Insert a new test customer and then delete that customer.
Tasks:
Insert the customer.
Find the customer using SELECT.
Delete the customer.
Use SELECT again to verify that the record no longer exists.
Part E — Important WHERE Practice
For each task, first write a SELECT query to identify the records.
Exercise 17
Find all films with a rental rate greater than 4.
Exercise 18
Find all films with a rental rate equal to 2.99.
Exercise 19
Find customers whose first name starts with A.
Exercise 20
Find customers whose last name is Smith.
Exercise 21
Find films with a length greater than 120 minutes.
Part F — CRUD Practical Task
Complete the following task independently.
Task 1 — Customer CRUD
Perform all four CRUD operations on the customer table.
C — Create: Insert a new customer.
R — Read: Display the customer's information.
U — Update: Change the customer's email.
D — Delete: Delete the customer.
Expected sequence
INSERT ↓SELECT ↓UPDATE ↓SELECT ↓DELETE ↓SELECT
Part G — Film Management Task
Perform the following operations on the film table.
Display all films.
Display only title, release_year, and rental_rate.
Find films with rental rate greater than 3.
Find films longer than 120 minutes.
Sort films by rental rate.
Update the rental rate of one test film.
Verify the update.
Part H — Challenge Tasks
Try these without looking at previous examples.
Challenge 1
Find all customers whose first name starts with J.
Challenge 2
Find all films with:
Rental rate greater than 2
Length greater than 100
Challenge 3
Display the 10 films with the highest rental rate.
Challenge 4
Find customers whose email contains gmail.
Challenge 5
Find films released after the year 2005.
Challenge 6
Update the rental rate of a selected film and verify the change.
Challenge 7
Create a test customer, update the customer's information, and finally delete the customer.
Part I — Safety Practice
Before every UPDATE or DELETE, students should first run a SELECT.
Step 1 — Check the record
SELECT *FROM customerWHERE customer_id = 600;
Step 2 — Update
UPDATE customerSET active = 0WHERE customer_id = 600;
Step 3 — Verify
SELECT *FROM customerWHERE customer_id = 600;
Remember: First SELECT → Check → UPDATE/DELETE → SELECT again.
This habit helps prevent accidental changes to multiple records.
Summary
CRUD operations are the foundation of database interaction in PostgreSQL. By learning SELECT, INSERT, UPDATE, and DELETE, we can manage data efficiently and safely in real-world database systems.
Key reminder
Use SELECT to read data.
Use INSERT to create records.
Use UPDATE to modify records.
Use DELETE to remove records.
Always use a WHERE clause when updating or deleting data.