Challenge - 5 Problems
PostgreSQL Views Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of a simple view query
Given a table employees with columns
id, name, and salary, and a view high_earners defined as CREATE VIEW high_earners AS SELECT name, salary FROM employees WHERE salary > 50000;, what will be the output of SELECT * FROM high_earners; if the table contains:id | name | salary
1 | Alice | 60000
2 | Bob | 45000
3 | Carol | 70000PostgreSQL
CREATE TABLE employees (id INT, name TEXT, salary INT); INSERT INTO employees VALUES (1, 'Alice', 60000), (2, 'Bob', 45000), (3, 'Carol', 70000); CREATE VIEW high_earners AS SELECT name, salary FROM employees WHERE salary > 50000; SELECT * FROM high_earners;
Attempts:
2 left
💡 Hint
Remember the view filters employees with salary greater than 50000.
✗ Incorrect
The view selects only the name and salary columns for employees earning more than 50000. Bob earns less, so he is excluded.
🧠 Conceptual
intermediate1:30remaining
Why use views for security?
Why are views useful for controlling access to sensitive data in PostgreSQL?
Attempts:
2 left
💡 Hint
Think about how views can limit what data is visible.
✗ Incorrect
Views act like windows showing only specific parts of data, so users can see only what the view allows, protecting sensitive columns or rows.
📝 Syntax
advanced2:00remaining
Identify the correct syntax to create a view with a join
Which option correctly creates a view named
employee_departments that shows employee names and their department names from tables employees (columns: id, name, dept_id) and departments (columns: id, dept_name)?Attempts:
2 left
💡 Hint
Remember the correct order: CREATE VIEW view_name AS SELECT ...
✗ Incorrect
Option A uses the correct syntax with CREATE VIEW, AS, and a proper JOIN clause. Other options have syntax errors or missing keywords.
❓ optimization
advanced1:30remaining
Performance impact of materialized views
What is the main advantage of using a materialized view instead of a regular view in PostgreSQL?
Attempts:
2 left
💡 Hint
Think about how storing results affects query speed and freshness.
✗ Incorrect
Materialized views save the query output physically, so queries run faster but require manual refresh to update data.
🔧 Debug
expert2:30remaining
Why does this view fail to update?
Consider a view
employee_info created as CREATE VIEW employee_info AS SELECT id, name, salary * 1.1 AS adjusted_salary FROM employees;. When trying to run UPDATE employee_info SET adjusted_salary = 70000 WHERE id = 1;, it fails. Why?Attempts:
2 left
💡 Hint
Think about what happens when a view has columns calculated from expressions.
✗ Incorrect
Views with computed columns are not automatically updatable because the database cannot map changes back to base tables without special rules.