Challenge - 5 Problems
View Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this CREATE VIEW query?
Given the table employees with columns id, name, salary, what will be the result of this view creation and subsequent SELECT?
Then running:
CREATE VIEW high_earners AS SELECT name, salary FROM employees WHERE salary > 50000;Then running:
SELECT * FROM high_earners;Attempts:
2 left
💡 Hint
Think about what the WHERE clause filters and which columns are selected in the view.
✗ Incorrect
The view
high_earners selects only name and salary columns from employees where salary is greater than 50000. Selecting from the view returns these filtered rows and columns.📝 Syntax
intermediate2:00remaining
Which CREATE VIEW statement is syntactically correct?
Choose the correct syntax to create a view named
dept_summary that shows department and the count of employees in each department from the employees table.Attempts:
2 left
💡 Hint
Remember the keyword AS is required before the SELECT statement in CREATE VIEW.
✗ Incorrect
Option A uses correct syntax: CREATE VIEW view_name AS SELECT ... GROUP BY ...; Option A misses AS, C misses GROUP BY, D has unnecessary parentheses causing syntax error.
🔧 Debug
advanced2:00remaining
Why does this CREATE VIEW statement fail?
Consider this statement:
It fails with an error. What is the reason?
CREATE VIEW recent_orders AS SELECT order_id, order_date FROM orders WHERE order_date > NOW() - INTERVAL '7 days';It fails with an error. What is the reason?
Attempts:
2 left
💡 Hint
Think about what functions are allowed inside view definitions.
✗ Incorrect
PostgreSQL does not allow volatile or non-deterministic functions like NOW() inside a view definition because the view must be stable and consistent. This causes the error.
❓ optimization
advanced2:00remaining
How to optimize a view that runs slow due to complex calculations?
You have a view
sales_summary that aggregates large data with complex calculations. It runs slowly when queried. What is the best way to improve performance?Attempts:
2 left
💡 Hint
Think about caching results of expensive queries.
✗ Incorrect
Materialized views store the query result physically and can be refreshed on demand, improving query speed for complex aggregations.
🧠 Conceptual
expert2:00remaining
What happens if you update data through a view without INSTEAD OF triggers?
You have a view
What will happen if no INSTEAD OF triggers are defined on this view?
employee_info based on multiple tables joined together. You try to run:UPDATE employee_info SET salary = salary + 1000 WHERE id = 5;What will happen if no INSTEAD OF triggers are defined on this view?
Attempts:
2 left
💡 Hint
Think about how views handle data modification when based on joins.
✗ Incorrect
Views based on joins are not automatically updatable. Without INSTEAD OF triggers to define how to handle updates, PostgreSQL raises an error.