0
0
SQLquery~20 mins

View as a saved query mental model in SQL - 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
Output of a simple view query
Given the table Employees with columns id, name, and salary, and the view defined as:
CREATE VIEW HighEarners AS SELECT name, salary FROM Employees WHERE salary > 50000;

What will be the output of SELECT * FROM HighEarners; if the Employees table contains:
id | name    | salary
1  | Alice   | 60000
2  | Bob     | 45000
3  | Charlie | 70000
SQL
CREATE VIEW HighEarners AS SELECT name, salary FROM Employees WHERE salary > 50000;
A[{"name": "Alice", "salary": 60000}, {"name": "Charlie", "salary": 70000}]
B[{"id": 1, "name": "Alice", "salary": 60000}, {"id": 3, "name": "Charlie", "salary": 70000}]
C[{"name": "Bob", "salary": 45000}]
D[]
Attempts:
2 left
💡 Hint
The view filters employees with salary greater than 50000 and only selects name and salary columns.
🧠 Conceptual
intermediate
1:30remaining
Understanding view updates
Which of the following statements about SQL views is TRUE?
AA view can only be created on a single table and cannot join multiple tables.
BA view stores data physically and updates automatically when the base table changes.
CA view cannot be used in a SELECT statement.
DA view is a saved query and does not store data physically; it reflects the current data in the base tables when queried.
Attempts:
2 left
💡 Hint
Think about whether views hold data or just a query definition.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in view creation
Which option contains a syntax error when creating a view that lists product names and their categories from tables Products and Categories?
SQL
Tables:
Products(product_id, product_name, category_id)
Categories(category_id, category_name)
ACREATE VIEW ProductCategories SELECT product_name, category_name FROM Products JOIN Categories ON Products.category_id = Categories.category_id;
BCREATE VIEW ProductCategories AS SELECT product_name, category_name FROM Products JOIN Categories ON Products.category_id = Categories.category_id;
CCREATE VIEW ProductCategories AS SELECT product_name, category_name FROM Products, Categories WHERE Products.category_id = Categories.category_id;
DCREATE VIEW ProductCategories AS SELECT product_name, category_name FROM Products INNER JOIN Categories ON Products.category_id = Categories.category_id;
Attempts:
2 left
💡 Hint
Check the syntax for the CREATE VIEW statement carefully.
optimization
advanced
2:30remaining
Performance impact of views
Consider a view defined as:
CREATE VIEW LargeSales AS SELECT * FROM Sales WHERE amount > 10000;

If the Sales table has millions of rows, which approach is generally better for performance when querying large sales data?
AQuery the view directly each time to get the filtered data.
BCreate an indexed materialized view that stores the filtered data physically.
CCreate a temporary table with all Sales data and query it instead of the view.
DAvoid filtering and select all rows from Sales every time.
Attempts:
2 left
💡 Hint
Think about how data storage and indexing affect query speed.
🔧 Debug
expert
3:00remaining
Diagnose the error when querying a view
A view is created as:
CREATE VIEW ActiveUsers AS SELECT id, name FROM Users WHERE status = 'active';

When running SELECT * FROM ActiveUsers;, the error ERROR: relation "activeusers" does not exist appears. What is the most likely cause?
AThe SELECT statement inside the view has a syntax error.
BThe Users table does not have a column named status.
CThe view was created in a different schema or database than the one currently queried.
DThe view name is case-sensitive and must be quoted exactly.
Attempts:
2 left
💡 Hint
Consider how database schemas and search paths affect object visibility.