View as a saved query mental model in SQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use a view in SQL, it acts like a saved query. Understanding how long it takes to run helps us know how it affects our database.
We want to see how the time to get results grows as the data gets bigger.
Analyze the time complexity of the following SQL view and query.
CREATE VIEW RecentOrders AS
SELECT OrderID, CustomerID, OrderDate
FROM Orders
WHERE OrderDate >= DATE_SUB(CURDATE(), INTERVAL 30 DAY);
SELECT * FROM RecentOrders WHERE CustomerID = 12345;
This view saves a query that finds orders from the last 30 days. Then we select orders for one customer from that view.
Look at what repeats when running the query.
- Primary operation: Scanning orders from the last 30 days.
- How many times: Once per query execution, scanning all recent orders.
As the number of recent orders grows, the work to find matching customer orders grows too.
| Input Size (n recent orders) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The work grows roughly in direct proportion to the number of recent orders.
Time Complexity: O(n)
This means the time to get results grows linearly with the number of recent orders.
[X] Wrong: "Using a view makes the query run instantly, no matter how much data there is."
[OK] Correct: A view is just a saved query. The database still runs the query each time, so bigger data means more work.
Understanding how views work and their time cost shows you know how databases handle saved queries, a useful skill for real projects.
"What if we added an index on CustomerID in the Orders table? How would the time complexity change?"
Practice
Solution
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.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.Final Answer:
A saved query that acts like a virtual table -> Option CQuick Check:
View = saved query acting like table [OK]
- Thinking views store data physically
- Confusing views with backups
- Assuming views are user accounts
EmployeeView showing all columns from Employees table?Solution
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.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.Final Answer:
CREATE VIEW EmployeeView AS SELECT * FROM Employees; -> Option DQuick Check:
CREATE VIEW ... AS SELECT ... [OK]
- Using CREATE TABLE instead of CREATE VIEW
- Confusing SELECT INTO with view creation
- Incorrect keyword order in syntax
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
Solution
Step 1: Understand the view definition
The view selects id and name from Products where price is less than 50.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.Final Answer:
Rows with id 1 and 3 showing id and name columns -> Option BQuick Check:
View filters price < 50 and selects id, name [OK]
- Expecting all columns in view output
- Including rows that don't meet WHERE condition
- Thinking view stores data separately
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 existWhat is the most likely cause?
Solution
Step 1: Analyze the error message
The error says the relation (view) "activeusers" does not exist, meaning the view is missing.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.Final Answer:
The view was not created successfully or was dropped -> Option AQuick Check:
Missing view = creation failed or dropped [OK]
- Assuming syntax errors cause this runtime error
- Thinking views need refreshing like materialized views
- Ignoring case sensitivity or schema issues
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?Solution
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.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.Final Answer:
CREATE VIEW RecentOrders AS SELECT order_id, customer_id, order_date FROM Orders WHERE order_date > CURRENT_DATE - INTERVAL '7 days'; -> Option AQuick Check:
Include needed columns and use standard interval syntax [OK]
- Omitting important columns in view
- Using non-standard date functions
- Forgetting to filter by date correctly
