0
0
PostgreSQLquery~20 mins

CREATE VIEW syntax in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
View Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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?

CREATE VIEW high_earners AS SELECT name, salary FROM employees WHERE salary > 50000;

Then running:

SELECT * FROM high_earners;
AReturns all employees with salary less than or equal to 50000, showing all columns.
BReturns all employees with salary greater than 50000, showing only their name and salary.
CReturns all employees with salary greater than 50000, showing all columns.
DCauses a syntax error because CREATE VIEW requires AS MATERIALIZED.
Attempts:
2 left
💡 Hint
Think about what the WHERE clause filters and which columns are selected in the view.
📝 Syntax
intermediate
2: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.
ACREATE VIEW dept_summary AS SELECT department, COUNT(*) FROM employees GROUP BY department;
BCREATE VIEW dept_summary SELECT department, COUNT(*) FROM employees GROUP BY department;
CCREATE VIEW dept_summary AS SELECT department, COUNT(*) FROM employees;
DCREATE VIEW dept_summary AS (SELECT department, COUNT(*) FROM employees GROUP BY department);
Attempts:
2 left
💡 Hint
Remember the keyword AS is required before the SELECT statement in CREATE VIEW.
🔧 Debug
advanced
2:00remaining
Why does this CREATE VIEW statement fail?
Consider this statement:

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?
AThe WHERE clause is not allowed in CREATE VIEW statements.
BThe INTERVAL syntax is invalid in PostgreSQL views.
CViews cannot use non-deterministic functions like NOW() in their definition.
DThe SELECT statement must include all columns from the base table.
Attempts:
2 left
💡 Hint
Think about what functions are allowed inside view definitions.
optimization
advanced
2: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?
ADrop the view and query the base tables directly every time.
BAdd more columns to the view to reduce joins.
CRewrite the view using subqueries instead of joins.
DConvert the view to a materialized view and refresh it periodically.
Attempts:
2 left
💡 Hint
Think about caching results of expensive queries.
🧠 Conceptual
expert
2:00remaining
What happens if you update data through a view without INSTEAD OF triggers?
You have a 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?
AThe update will fail with an error because the view is not inherently updatable.
BThe update will succeed and modify the underlying tables automatically.
CThe update will partially succeed, updating only one table silently.
DThe update will create a new row in the view without affecting base tables.
Attempts:
2 left
💡 Hint
Think about how views handle data modification when based on joins.