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.
WHERE, ORDER BY, and LIMIT.WHERE clause to avoid changing every row.WHERE clause to prevent deleting an entire table.INSERT), Read (SELECT), Update (UPDATE), and Delete (DELETE).SELECT query with the same WHERE condition before executing an UPDATE or DELETE statement.The SELECT statement is used to retrieve data from a table.
SELECT column1, column2
FROM table_name;
SELECT *
FROM customer;
* means all columns.
SELECT first_name, last_name, email
FROM customer;
SELECT title, rental_rate
FROM film;
WHEREWHERE is used to retrieve records that meet a condition.
SELECT first_name, last_name
FROM customer
WHERE active = 1;
ORDER BYSELECT title, rental_rate
FROM film
ORDER BY rental_rate DESC;
DESC = highest to lowestASC = lowest to highestLIMITSELECT *
FROM film
LIMIT 10;
This displays only the first 10 records.
The INSERT statement is used to add a new record to a table.
INSERT INTO table_name (column1, column2, column3)
VALUES (value1, value2, value3);
Suppose we want to add a new customer:
INSERT INTO customer
(first_name, last_name, email, address_id, store_id, active)
VALUES
('Ali', 'Khan', 'ali@example.com', 1, 1, 1);
SELECT *
FROM customer
WHERE email = 'ali@example.com';
Important: When inserting text values, use single quotes like
'Ali'and notAli.
The UPDATE statement is used to modify existing records.
UPDATE table_name
SET column = new_value
WHERE condition;
Update a customer’s email:
UPDATE customer
SET email = 'ali.khan@example.com'
WHERE customer_id = 600;
Update a movie’s rental rate:
UPDATE film
SET rental_rate = 3.99
WHERE film_id = 1;
UPDATE customer
SET first_name = 'Muhammad',
last_name = 'Ali'
WHERE customer_id = 600;
⚠️ Important: Always use
WHERE.UPDATE customer SET active = 0;This will update every customer. Usually, you should specify the record:
UPDATE customer SET active = 0 WHERE customer_id = 600;
The DELETE statement is used to remove records from a table.
DELETE FROM table_name
WHERE condition;
DELETE FROM customer
WHERE customer_id = 600;
This removes the customer whose customer_id is 600.
⚠️ Important: Always use
WHERE.This is dangerous:
DELETE FROM customer;It deletes all records from the
customertable.Use:
DELETE FROM customer WHERE customer_id = 600;
| Statement | Purpose | Example |
|---|---|---|
SELECT |
Read or retrieve data | SELECT * FROM film; |
INSERT |
Add new data | INSERT INTO film ... |
UPDATE |
Modify existing data | UPDATE film SET ... |
DELETE |
Remove data | DELETE FROM film WHERE ... |
CRUD:
INSERTSELECTUPDATEDELETEWrite queries to:
film table.Display all films.
SELECT *
FROM film;
Display films with a rental rate greater than 3.
SELECT title, rental_rate
FROM film
WHERE rental_rate > 3;
Insert a new customer with your own sample information.
Change the email address of the customer you inserted.
Delete the customer you inserted.
After each operation, use SELECT to verify the result:
SELECT *
FROM customer
WHERE email = 'your_email@example.com';
Important Safety Rule: Before executing
UPDATEorDELETE, first run aSELECTwith the sameWHEREcondition.SELECT * FROM customer WHERE customer_id = 600;If the correct record appears, then perform:
UPDATE customer SET active = 0 WHERE customer_id = 600;
Write a query to display all records from the film table.
SELECT *
FROM film;
Display the following information from the film table:
Display films where the rental rate is greater than 3.
Display films whose length is less than 90 minutes.
Display the first name, last name, and email of all active customers.
Display all films sorted by rental rate from highest to lowest.
Display the first 10 films from the film table.
Find customers whose first name is Mary.
Insert a new customer with:
Task: After inserting the record, use SELECT to verify it.
Insert another customer using your own sample information.
Tasks:
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.
Change the last name of your inserted customer.
Example: Khan → Ahmed
Find a film with film_id = 1.
Change its rental rate to 3.99 and verify the result.
For your inserted customer, update:
using a single UPDATE statement.
Delete the customer that you inserted in Exercise 9.
Important: Use the appropriate WHERE condition.
Then verify that the customer has been deleted.
Insert a new test customer and then delete that customer.
Tasks:
SELECT.SELECT again to verify that the record no longer exists.For each task, first write a SELECT query to identify the records.
Find all films with a rental rate greater than 4.
Find all films with a rental rate equal to 2.99.
Find customers whose first name starts with A.
Find customers whose last name is Smith.
Find films with a length greater than 120 minutes.
Complete the following task independently.
Perform all four CRUD operations on the customer table.
INSERT
↓
SELECT
↓
UPDATE
↓
SELECT
↓
DELETE
↓
SELECT
Perform the following operations on the film table.
title, release_year, and rental_rate.Try these without looking at previous examples.
Find all customers whose first name starts with J.
Find all films with:
Display the 10 films with the highest rental rate.
Find customers whose email contains gmail.
Find films released after the year 2005.
Update the rental rate of a selected film and verify the change.
Create a test customer, update the customer’s information, and finally delete the customer.
Before every UPDATE or DELETE, students should first run a SELECT.
SELECT *
FROM customer
WHERE customer_id = 600;
UPDATE customer
SET active = 0
WHERE customer_id = 600;
SELECT *
FROM customer
WHERE customer_id = 600;
Remember: First
SELECT→ Check →UPDATE/DELETE→SELECTagain.This habit helps prevent accidental changes to multiple records.
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.
SELECT to read data.INSERT to create records.UPDATE to modify records.DELETE to remove records.WHERE clause when updating or deleting data.