Practice PostgreSQL Views and Materialized Views through beginner, intermediate, and advanced SQL exercises using a students table and the DVD Rental database.
Complete the following exercises using PostgreSQL.
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
);
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;
all_students that displays all columns from the students table.student_names that displays only student_id and student_name.student_names View.cs_students that displays only students from the Computer Science department.cs_students View.passed_students that displays students who have obtained 50 or more marks.high_performers that displays students who have obtained 80 or more marks.student_results that displays student names, departments, and marks.student_results View in descending order of marks.department_average that displays each department and its average student marks.all_students View who belong to the Information Technology department.semester_students that displays student names, departments, and semesters.student_names View and include the semester column.student_names View to student_basic_info using ALTER VIEW.cs_student_records that displays student records from the Computer Science department.cs_student_records View to update a student’s department or marks. Verify the change in the original students table.mv_department_average that calculates average marks for each department.students table. Check whether the Materialized View automatically reflects the new record.mv_department_average Materialized View and verify the updated results.student_basic_info View without deleting the original students table.mv_department_average Materialized View.Use the PostgreSQL dvdrental sample database to complete the following exercises.
mv_customer_payment_summary that displays each customer’s ID and total payment amount.mv_customer_rental_count that displays each customer’s ID and total number of rentals.mv_category_film_count that displays each film category and the total number of films in that category.pg_matviews to display the names of all Materialized Views in the current database.mv_customer_details that displays customer ID, first name, last name, and email.customer_id column of mv_customer_payment_summary.payment table and check whether mv_customer_payment_summary automatically reflects the change.mv_customer_payment_summary and verify whether the new payment is included.WITH NO DATA.ispopulated column in pg_matviews for the Materialized View created in the previous exercise.mv_customer_payment_summary using the customer_id column.REFRESH MATERIALIZED VIEW CONCURRENTLY on mv_customer_payment_summary.ALTER MATERIALIZED VIEW.DROP MATERIALIZED VIEW.Complete these tasks independently to strengthen your understanding.
EXPLAIN ANALYZE.