Bird
Raised Fist0
SQLquery~20 mins

View as a saved query mental model in SQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is a SQL view best described as?
easy
A. A backup copy of a database
B. A physical table storing data permanently
C. A saved query that acts like a virtual table
D. A user account with special permissions

Solution

  1. Step 1: Understand what a view is

    A view is not a physical table but a saved SQL query that can be treated like a table.
  2. Step 2: Compare options to definition

    A saved query that acts like a virtual table matches the definition exactly. The other options describe different database concepts like backups, physical tables, or user permissions.
  3. Final Answer:

    A saved query that acts like a virtual table -> Option C
  4. Quick Check:

    View = saved query acting like table [OK]
Hint: Remember: Views are saved queries, not real tables [OK]
Common Mistakes:
  • Thinking views store data physically
  • Confusing views with backups
  • Assuming views are user accounts
2. Which of the following is the correct syntax to create a view named EmployeeView showing all columns from Employees table?
easy
A. VIEW CREATE EmployeeView SELECT * FROM Employees;
B. CREATE TABLE EmployeeView AS SELECT * FROM Employees;
C. SELECT * INTO EmployeeView FROM Employees;
D. CREATE VIEW EmployeeView AS SELECT * FROM Employees;

Solution

  1. Step 1: Recall the syntax for creating a view

    The correct syntax starts with CREATE VIEW, followed by the view name, AS, then the SELECT query.
  2. Step 2: Check each option

    CREATE VIEW EmployeeView AS SELECT * FROM Employees; matches the correct syntax. CREATE TABLE EmployeeView AS SELECT * FROM Employees; creates a table, not a view. SELECT * INTO EmployeeView FROM Employees; is for SELECT INTO which creates a table. VIEW CREATE EmployeeView SELECT * FROM Employees; is invalid syntax.
  3. Final Answer:

    CREATE VIEW EmployeeView AS SELECT * FROM Employees; -> Option D
  4. Quick Check:

    CREATE VIEW ... AS SELECT ... [OK]
Hint: Use CREATE VIEW ... AS SELECT ... to define views [OK]
Common Mistakes:
  • Using CREATE TABLE instead of CREATE VIEW
  • Confusing SELECT INTO with view creation
  • Incorrect keyword order in syntax
3. Given the table Products with columns id, name, and price, and the view created as:
CREATE VIEW CheapProducts AS SELECT id, name FROM Products WHERE price < 50;
What will the query SELECT * FROM CheapProducts; return if Products contains:
id | name    | price
1  | Pen     | 10
2  | Notebook| 60
3  | Eraser  | 30
medium
A. Rows with id 1, 2, and 3 showing all columns
B. Rows with id 1 and 3 showing id and name columns
C. Rows with id 2 only showing id and name columns
D. No rows returned because price is not selected

Solution

  1. Step 1: Understand the view definition

    The view selects id and name from Products where price is less than 50.
  2. Step 2: Apply the filter to the data

    Products with price less than 50 are id 1 (price 10) and id 3 (price 30). The view returns only id and name columns.
  3. Final Answer:

    Rows with id 1 and 3 showing id and name columns -> Option B
  4. Quick Check:

    View filters price < 50 and selects id, name [OK]
Hint: View returns filtered columns and rows as defined [OK]
Common Mistakes:
  • Expecting all columns in view output
  • Including rows that don't meet WHERE condition
  • Thinking view stores data separately
4. You created a view with:
CREATE VIEW ActiveUsers AS SELECT id, name FROM Users WHERE active = 1;
But running SELECT * FROM ActiveUsers; gives an error: ERROR: relation "activeusers" does not exist
What is the most likely cause?
medium
A. The view was not created successfully or was dropped
B. The SELECT query inside the view has syntax errors
C. The Users table does not have an 'active' column
D. You must refresh the view before selecting from it

Solution

  1. Step 1: Analyze the error message

    The error says the relation (view) "activeusers" does not exist, meaning the view is missing.
  2. Step 2: Consider causes for missing view

    This usually means the view was never created or was dropped. Syntax errors or missing columns cause errors during creation, not this runtime error. Views do not require refreshing.
  3. Final Answer:

    The view was not created successfully or was dropped -> Option A
  4. Quick Check:

    Missing view = creation failed or dropped [OK]
Hint: Check if view exists before querying it [OK]
Common Mistakes:
  • Assuming syntax errors cause this runtime error
  • Thinking views need refreshing like materialized views
  • Ignoring case sensitivity or schema issues
5. You want to create a view RecentOrders that always shows orders placed in the last 7 days from the Orders table with columns order_id, customer_id, and order_date. Which SQL statement correctly creates this view?
hard
A. CREATE VIEW RecentOrders AS SELECT order_id, customer_id, order_date FROM Orders WHERE order_date > CURRENT_DATE - INTERVAL '7 days';
B. CREATE VIEW RecentOrders AS SELECT order_id, customer_id FROM Orders WHERE order_date > CURRENT_DATE - INTERVAL '7 days';
C. CREATE VIEW RecentOrders AS SELECT * FROM Orders WHERE order_date > DATEADD(day, -7, GETDATE());
D. CREATE VIEW RecentOrders AS SELECT order_id, customer_id FROM Orders WHERE order_date > SYSDATE - 7;

Solution

  1. Step 1: Identify needed columns and date filter

    The view should include order_id, customer_id, and order_date columns and filter orders from last 7 days.
  2. Step 2: Check each option's correctness

    CREATE VIEW RecentOrders AS SELECT order_id, customer_id FROM Orders WHERE order_date > CURRENT_DATE - INTERVAL '7 days'; misses order_date column, so it won't show the date. CREATE VIEW RecentOrders AS SELECT order_id, customer_id, order_date FROM Orders WHERE order_date > CURRENT_DATE - INTERVAL '7 days'; includes all needed columns and uses correct standard SQL interval syntax. CREATE VIEW RecentOrders AS SELECT * FROM Orders WHERE order_date > DATEADD(day, -7, GETDATE()); uses SQL Server syntax (DATEADD, GETDATE) which may not be standard. CREATE VIEW RecentOrders AS SELECT order_id, customer_id FROM Orders WHERE order_date > SYSDATE - 7; uses SYSDATE which is Oracle-specific and may not work in standard SQL.
  3. Final Answer:

    CREATE VIEW RecentOrders AS SELECT order_id, customer_id, order_date FROM Orders WHERE order_date > CURRENT_DATE - INTERVAL '7 days'; -> Option A
  4. Quick Check:

    Include needed columns and use standard interval syntax [OK]
Hint: Include all needed columns and use standard date intervals [OK]
Common Mistakes:
  • Omitting important columns in view
  • Using non-standard date functions
  • Forgetting to filter by date correctly