Querying through views in SQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use views in SQL, we want to know how the time to get results changes as the data grows.
We ask: How does querying a view affect the work the database does?
Analyze the time complexity of the following code snippet.
CREATE VIEW RecentOrders AS
SELECT OrderID, CustomerID, OrderDate
FROM Orders
WHERE OrderDate >= DATE_SUB(CURRENT_DATE, INTERVAL 30 DAY);
SELECT * FROM RecentOrders WHERE CustomerID = 12345;
This code creates a view showing orders from the last 30 days, then queries it for a specific customer.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning the Orders table to find recent orders.
- How many times: Once per query, the database checks each order to see if it is recent.
As the number of orders grows, the database must check more rows to find recent ones.
| Input Size (n) | 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 orders.
Time Complexity: O(n)
This means the time to get results grows roughly in step with the number of rows in the Orders table.
[X] Wrong: "Using a view makes the query instantly faster because it stores results."
[OK] Correct: Views do not store data by default; they run the underlying query each time, so the work depends on the original table size.
Understanding how views affect query time helps you explain database behavior clearly and shows you think about efficiency in real situations.
"What if the view included an index on OrderDate? How would that change the time complexity?"
Practice
SELECT * FROM view_name; works because a view is:Solution
Step 1: Understand what a view represents
A view is not a physical table but a stored SQL query that behaves like a table.Step 2: Recognize how views are queried
You can query a view just like a table because it returns the result of its saved query.Final Answer:
A saved query that acts like a virtual table -> Option AQuick Check:
View = saved query acting like table [OK]
- Thinking views store data physically
- Confusing views with indexes
- Assuming views are backups
employee_view showing all columns from employees table?Solution
Step 1: Recall the standard syntax for creating a view
The correct syntax is: CREATE VIEW view_name AS SELECT ...Step 2: Match the syntax with options
CREATE VIEW employee_view AS SELECT * FROM employees; matches the correct syntax exactly.Final Answer:
CREATE VIEW employee_view AS SELECT * FROM employees; -> Option DQuick Check:
CREATE VIEW ... AS SELECT ... [OK]
- Swapping keywords CREATE and VIEW
- Using FROM instead of AS
- Incorrect keyword order
high_salary defined as:CREATE VIEW high_salary AS SELECT name, salary FROM employees WHERE salary > 70000;
What will this query return?
SELECT * FROM high_salary WHERE salary > 80000;
Solution
Step 1: Understand the view definition
The view returns employees with salary > 70000 only.Step 2: Apply the query filter on the view
The query further filters those results to salary > 80000, so only employees with salary above 80000 are returned.Final Answer:
All employees with salary greater than 80000 -> Option CQuick Check:
View filters 70000+, query filters 80000+ [OK]
- Thinking the second filter overrides the first
- Assuming syntax error due to repeated conditions
- Believing the result will be empty
CREATE VIEW dept_count AS SELECT department, COUNT(*) AS emp_count FROM employees GROUP BY department;
Which query will cause an error when run on this view?
Solution
Step 1: Identify columns in the view
The view has columns: department and emp_count only.Step 2: Check each query's column usage
SELECT department, salary FROM dept_count; tries to select 'salary' which does not exist in the view, causing an error.Final Answer:
SELECT department, salary FROM dept_count; causes error -> Option BQuick Check:
Querying non-existent column = error [OK]
- Selecting columns not in the view
- Assuming all original table columns exist in view
- Ignoring GROUP BY effects on columns
active_customers that shows customers with at least one order in the last 30 days.Given tables:
customers(id, name)orders(id, customer_id, order_date)Which is the correct SQL to create this view?
Solution
Step 1: Understand the requirement
The view must include customers with orders in last 30 days only.Step 2: Analyze each option's correctness
CREATE VIEW active_customers AS SELECT id, name FROM customers WHERE EXISTS (SELECT 1 FROM orders WHERE customer_id = customers.id AND order_date > CURRENT_DATE - INTERVAL '30 days'); uses EXISTS with correct date interval syntax and correlates orders to customers properly. CREATE VIEW active_customers AS SELECT c.id, c.name FROM customers c JOIN orders o ON c.id = o.customer_id WHERE o.order_date > CURRENT_DATE - INTERVAL '30 days'; uses JOIN but may duplicate customers if multiple orders exist. CREATE VIEW active_customers AS SELECT * FROM customers WHERE id IN (SELECT customer_id FROM orders WHERE order_date > CURRENT_DATE - 30); has incorrect date subtraction syntax. CREATE VIEW active_customers AS SELECT c.id, c.name FROM customers c LEFT JOIN orders o ON c.id = o.customer_id WHERE o.order_date > CURRENT_DATE - INTERVAL 30 DAY; uses LEFT JOIN but filters on order_date, which can exclude customers without recent orders incorrectly.Final Answer:
CREATE VIEW active_customers AS SELECT id, name FROM customers WHERE EXISTS (SELECT 1 FROM orders WHERE customer_id = customers.id AND order_date > CURRENT_DATE - INTERVAL '30 days'); -> Option AQuick Check:
Use EXISTS with correct date interval for filtering [OK]
- Using incorrect date interval syntax
- Using JOIN causing duplicate rows
- Filtering on LEFT JOIN columns incorrectly
