Practice Exercises: Views and Materialized Views in PostgreSQL
Practice PostgreSQL Views and Materialized Views through beginner, intermediate, and advanced SQL exercises using a students table and the DVD Rental database.
Section 1: Views Using the Students Table
Complete the following exercises using PostgreSQL.
Step 1: Create the Students Table
First, create a table named students with the following columns:
| Column | Data Type | Description |
|---|---|---|
| student_id | SERIAL | Unique student ID |
| student_name | VARCHAR(100) | Student's full name |
| department | VARCHAR(100) | Department name |
| marks | NUMERIC(5,2) | Student marks |
| semester | INTEGER | Current semester |
Execute the following SQL query:
CREATE TABLE students (
student_id SERIAL PRIMARY KEY,
student_name VARCHAR(100) NOT NULL,
department VARCHAR(100),
marks NUMERIC(5,2),
semester INTEGER
);Step 2: Insert Sample Records
Insert the following records into the students table:
INSERT INTO students
(student_name, department, marks, semester)
VALUES
('Ali Khan', 'Computer Science', 85, 1),
('Sara Ahmed', 'Computer Science', 92, 2),
('Usman Malik', 'Information Technology', 68, 1),
('Ayesha Noor', 'Software Engineering', 78, 3),
('Hassan Raza', 'Computer Science', 45, 2),
('Fatima Zahra', 'Information Technology', 88, 4),
('Bilal Ahmad', 'Software Engineering', 55, 1),
('Zainab Ali', 'Computer Science', 95, 4),
('Hamza Tariq', 'Information Technology', 42, 2),
('Mariam Iqbal', 'Software Engineering', 81, 3);Verify the inserted records:
SELECT * FROM students;Beginner Level
- Create a View named
all_studentsthat displays all columns from thestudentstable. - Create a View named
student_namesthat displays onlystudent_idandstudent_name. - Retrieve all records from the
student_namesView. - Create a View named
cs_studentsthat displays only students from the Computer Science department. - Retrieve all records from the
cs_studentsView. - Create a View named
passed_studentsthat displays students who have obtained 50 or more marks.
Intermediate Level
- Create a View named
high_performersthat displays students who have obtained 80 or more marks. - Create a View named
student_resultsthat displays student names, departments, and marks. - Retrieve records from the
student_resultsView in descending order of marks. - Create a View named
department_averagethat displays each department and its average student marks. - Retrieve students from the
all_studentsView who belong to the Information Technology department. - Create a View that displays the total number of students in each department.
Advanced Level
- Create a View named
semester_studentsthat displays student names, departments, and semesters. - Replace the existing
student_namesView and include thesemestercolumn. - Rename the
student_namesView tostudent_basic_infousingALTER VIEW. - Create an updatable View named
cs_student_recordsthat displays student records from the Computer Science department. - Use the
cs_student_recordsView to update a student's department or marks. Verify the change in the originalstudentstable. - Create a Materialized View named
mv_department_averagethat calculates average marks for each department. - Insert a new student into the original
studentstable. Check whether the Materialized View automatically reflects the new record. - Refresh the
mv_department_averageMaterialized View and verify the updated results. - Drop the
student_basic_infoView without deleting the originalstudentstable. - Drop the
mv_department_averageMaterialized View.
Section 2: Materialized Views Using the DVD Rental Database
Use the PostgreSQL dvdrental sample database to complete the following exercises.
Beginner Level
- Create a Materialized View named
mv_customer_payment_summarythat displays each customer's ID and total payment amount. - Create a Materialized View named
mv_customer_rental_countthat displays each customer's ID and total number of rentals. - Create a Materialized View named
mv_category_film_countthat displays each film category and the total number of films in that category. - Retrieve all records from each Materialized View.
- Use
pg_matviewsto display the names of all Materialized Views in the current database.
Intermediate Level
- Create a Materialized View named
mv_customer_detailsthat displays customer ID, first name, last name, and email. - Create a Materialized View showing the total payment amount for each customer, including their first and last names.
- Create a Materialized View showing the number of rentals made by each customer, along with their names.
- Create a Materialized View showing the average rental rate for each film category.
- Create an index on the
customer_idcolumn ofmv_customer_payment_summary. - Insert a new payment record into the
paymenttable and check whethermv_customer_payment_summaryautomatically reflects the change. - Refresh
mv_customer_payment_summaryand verify whether the new payment is included.
Advanced Level
- Create a Materialized View using
WITH NO DATA. - Check the
ispopulatedcolumn inpg_matviewsfor the Materialized View created in the previous exercise. - Try to retrieve records from the unpopulated Materialized View. Observe the result.
- Refresh the Materialized View and retrieve its records.
- Create a unique index on
mv_customer_payment_summaryusing thecustomer_idcolumn. - Execute
REFRESH MATERIALIZED VIEW CONCURRENTLYonmv_customer_payment_summary. - Rename a Materialized View using
ALTER MATERIALIZED VIEW. - Drop a Materialized View using
DROP MATERIALIZED VIEW.
Additional Challenge Tasks
Complete these tasks independently to strengthen your understanding.
- Create a regular View that combines data from two or more tables in the DVD Rental database.
- Create a Materialized View that summarizes rental information by customer and month.
- Compare the behavior of a regular View and a Materialized View after inserting a new record into an underlying table.
- Create indexes on a Materialized View and investigate their effect on query performance using
EXPLAIN ANALYZE. - Write SQL queries to list all regular Views and Materialized Views in the current database.