CREATE VIEW syntax in SQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we create a view in SQL, we define a saved query. Understanding how the time to run this query grows helps us know how it will perform as data grows.
We want to see how the cost of using a view changes when the underlying data gets bigger.
Analyze the time complexity of this view creation and usage.
CREATE VIEW RecentOrders AS
SELECT OrderID, CustomerID, OrderDate
FROM Orders
WHERE OrderDate >= DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY);
SELECT * FROM RecentOrders;
This view selects orders from the last 30 days. When we query the view, it runs this filter on the Orders table.
- Primary operation: Scanning the Orders table rows to check the OrderDate condition.
- How many times: Once for each row in Orders every time the view is queried.
As the number of orders grows, the work to find recent orders grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of operations grows directly with the number of rows in the Orders table.
Time Complexity: O(n)
This means the time to get results from the view grows linearly with the number of orders.
[X] Wrong: "Creating a view makes the query run faster automatically."
[OK] Correct: A view just saves the query. The database still runs the full query each time, so the time depends on the data size and query complexity.
Understanding how views work and their time cost shows you know how databases handle saved queries and data growth. This skill helps you write efficient queries and explain performance clearly.
"What if we added an index on OrderDate? How would that change the time complexity when querying the view?"
Practice
CREATE VIEW statement in SQL?Solution
Step 1: Understand what a view is
A view is a virtual table created by saving a SELECT query.Step 2: Identify the purpose of CREATE VIEW
CREATE VIEW stores the SELECT query so you can use it like a table without storing data.Final Answer:
To save a SELECT query as a virtual table -> Option AQuick Check:
CREATE VIEW = virtual table [OK]
- Thinking views store data physically
- Confusing CREATE VIEW with INSERT or UPDATE
- Assuming views delete or modify data
EmployeeView that selects all columns from the Employees table?Solution
Step 1: Recall the correct CREATE VIEW syntax
The syntax is: CREATE VIEW view_name AS SELECT ...Step 2: Match the syntax with options
CREATE VIEW EmployeeView AS SELECT * FROM Employees; matches the correct syntax exactly.Final Answer:
CREATE VIEW EmployeeView AS SELECT * FROM Employees; -> Option BQuick Check:
CREATE VIEW view_name AS SELECT ... [OK]
- Swapping keywords CREATE and VIEW
- Mixing table and view names incorrectly
- Using wrong keyword order
CREATE VIEW ActiveUsers AS SELECT id, name FROM Users WHERE active = 1;What will the query
SELECT * FROM ActiveUsers; return?Solution
Step 1: Understand the view definition
The view selects id and name from Users where active = 1, so only active users are included.Step 2: Analyze the SELECT from the view
Selecting * from ActiveUsers returns all columns defined in the view, filtered by active = 1.Final Answer:
Only users with active = 1 -> Option AQuick Check:
View filters rows = active users only [OK]
- Assuming view returns all table rows
- Thinking views cannot filter data
- Expecting columns not in view to appear
CREATE VIEW SalesView SELECT * FROM Sales;Solution
Step 1: Check the syntax of CREATE VIEW
The correct syntax requires AS before the SELECT statement.Step 2: Identify the missing keyword
The statement misses AS, causing a syntax error.Final Answer:
Missing AS keyword before SELECT -> Option CQuick Check:
CREATE VIEW ... AS SELECT ... [OK]
- Omitting AS keyword
- Misplacing SELECT clause
- Assuming WHERE clause is mandatory
TopProducts that shows product names and total sales only for products with sales over 1000. Which SQL statement correctly creates this view?Solution
Step 1: Understand the requirement
We need product names and total sales, only for products with total sales over 1000.Step 2: Use GROUP BY and HAVING correctly
SUM(sales) requires GROUP BY product_name, and filtering on aggregated values uses HAVING.Step 3: Check each option
CREATE VIEW TopProducts AS SELECT product_name, SUM(sales) FROM Products GROUP BY product_name HAVING SUM(sales) > 1000; uses GROUP BY and HAVING correctly; others misuse WHERE or clause order.Final Answer:
CREATE VIEW TopProducts AS SELECT product_name, SUM(sales) FROM Products GROUP BY product_name HAVING SUM(sales) > 1000; -> Option DQuick Check:
Use HAVING for aggregated filters in views [OK]
- Using WHERE with aggregate functions
- Placing WHERE after GROUP BY
- Omitting GROUP BY when using SUM
