0
0
PostgreSQLquery~20 mins

Why views matter in PostgreSQL - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PostgreSQL Views Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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 | 70000
PostgreSQL
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;
A[{"id": 1, "name": "Alice", "salary": 60000}, {"id": 3, "name": "Carol", "salary": 70000}]
B[]
C[{"name": "Bob", "salary": 45000}]
D[{"name": "Alice", "salary": 60000}, {"name": "Carol", "salary": 70000}]
Attempts:
2 left
💡 Hint
Remember the view filters employees with salary greater than 50000.
🧠 Conceptual
intermediate
1:30remaining
Why use views for security?
Why are views useful for controlling access to sensitive data in PostgreSQL?
AViews prevent users from running any queries on the database.
BViews automatically encrypt data to protect it from unauthorized access.
CViews can restrict which columns and rows users see without giving direct access to the base tables.
DViews delete sensitive data after a set time to protect privacy.
Attempts:
2 left
💡 Hint
Think about how views can limit what data is visible.
📝 Syntax
advanced
2: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)?
ACREATE VIEW employee_departments AS SELECT employees.name, departments.dept_name FROM employees JOIN departments ON employees.dept_id = departments.id;
BCREATE VIEW employee_departments SELECT employees.name, departments.dept_name FROM employees INNER JOIN departments WHERE employees.dept_id = departments.id;
CCREATE employee_departments VIEW AS SELECT name, dept_name FROM employees, departments WHERE employees.dept_id = departments.id;
DVIEW employee_departments AS SELECT employees.name, departments.dept_name FROM employees JOIN departments ON employees.dept_id = departments.id;
Attempts:
2 left
💡 Hint
Remember the correct order: CREATE VIEW view_name AS SELECT ...
optimization
advanced
1:30remaining
Performance impact of materialized views
What is the main advantage of using a materialized view instead of a regular view in PostgreSQL?
AMaterialized views allow users to write data back to the base tables through the view.
BMaterialized views store the query result physically, improving query speed at the cost of needing manual refresh.
CMaterialized views compress data to save disk space compared to regular views.
DMaterialized views automatically update in real-time without any performance cost.
Attempts:
2 left
💡 Hint
Think about how storing results affects query speed and freshness.
🔧 Debug
expert
2: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?
ABecause the view contains a computed column, PostgreSQL cannot update it directly without an INSTEAD OF trigger.
BBecause the view does not include the primary key column.
CBecause views cannot be queried, only updated.
DBecause the UPDATE statement syntax is incorrect for views.
Attempts:
2 left
💡 Hint
Think about what happens when a view has columns calculated from expressions.