Views for security and abstraction in SQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using views in databases, it's important to understand how the time to get results changes as data grows.
We want to know how the database handles queries on views as the underlying tables get bigger.
Analyze the time complexity of this view query.
CREATE VIEW EmployeeNames AS
SELECT EmployeeID, FirstName, LastName
FROM Employees
WHERE Active = 1;
SELECT * FROM EmployeeNames WHERE LastName = 'Smith';
This view shows active employees' names. The query fetches all active employees named 'Smith'.
Look at what repeats when the query runs.
- Primary operation: Scanning the Employees table rows to find active employees.
- How many times: Once per query, but it checks each row in the Employees table.
As the Employees table grows, the work to find matching rows grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 row checks |
| 100 | About 100 row checks |
| 1000 | About 1000 row checks |
Pattern observation: The number of operations grows roughly in direct proportion to the number of rows.
Time Complexity: O(n)
This means the time to get results grows linearly with the number of rows in the Employees table.
[X] Wrong: "Using a view makes queries instantly faster regardless of data size."
[OK] Correct: Views are saved queries, not stored results. The database still processes the underlying data each time, so bigger tables mean more work.
Understanding how views affect query time helps you explain database design choices clearly and confidently in interviews.
What if we added an index on the LastName column? How would the time complexity change?
Practice
VIEW in SQL?Solution
Step 1: Understand what a VIEW is
A VIEW is a virtual table created by a SELECT query that shows data without storing it separately.Step 2: Identify the purpose of a VIEW
It helps users see only the data they need, hiding sensitive details and simplifying complex queries.Final Answer:
To provide a simplified and secure way to access specific data -> Option BQuick Check:
VIEW = Simplify + Secure access [OK]
- Thinking views store data permanently
- Confusing views with backups
- Believing views improve hardware speed
EmployeeView showing only EmployeeID and Name from Employees table?Solution
Step 1: Recall the correct SQL syntax for creating a view
The syntax is: CREATE VIEW view_name AS SELECT columns FROM table;Step 2: Match the syntax with options
CREATE VIEW EmployeeView AS SELECT EmployeeID, Name FROM Employees; matches the correct syntax exactly.Final Answer:
CREATE VIEW EmployeeView AS SELECT EmployeeID, Name FROM Employees; -> Option AQuick Check:
CREATE VIEW ... AS SELECT ... [OK]
- Using MAKE VIEW instead of CREATE VIEW
- Confusing CREATE VIEW with CREATE TABLE
- Incorrect keyword order
Employees with columns EmployeeID, Name, Salary, and a view EmployeeView defined as:CREATE VIEW EmployeeView AS SELECT EmployeeID, Name FROM Employees;
What will the query
SELECT * FROM EmployeeView; return?Solution
Step 1: Understand the view definition
The view selects only EmployeeID and Name columns from Employees.Step 2: Determine what SELECT * from the view returns
It returns only the columns defined in the view, which are EmployeeID and Name.Final Answer:
Only EmployeeID and Name columns -> Option CQuick Check:
View columns = Selected columns only [OK]
- Expecting all original table columns
- Thinking missing columns cause syntax errors
- Confusing view columns with table columns
CREATE VIEW SalesView SELECT OrderID, Amount FROM Sales;
What is the error and how to fix it?
Solution
Step 1: Identify the syntax error in CREATE VIEW
The correct syntax requires AS keyword after the view name.Step 2: Correct the statement
Add AS after SalesView: CREATE VIEW SalesView AS SELECT OrderID, Amount FROM Sales;Final Answer:
Missing AS keyword; fix by adding AS after view name -> Option DQuick Check:
CREATE VIEW ... AS SELECT ... [OK]
- Omitting AS keyword
- Misplacing FROM keyword
- Thinking SELECT is disallowed in views
PublicEmployeeData that hides the Salary column from the Employees table but allows users to see EmployeeID, Name, and Department. Which SQL statement correctly creates this view and ensures security by restricting access to sensitive data?Solution
Step 1: Identify columns to include and exclude
We want EmployeeID, Name, Department but NOT Salary to protect sensitive data.Step 2: Choose the correct SELECT statement for the view
CREATE VIEW PublicEmployeeData AS SELECT EmployeeID, Name, Department FROM Employees; selects only the allowed columns, hiding Salary effectively.Final Answer:
CREATE VIEW PublicEmployeeData AS SELECT EmployeeID, Name, Department FROM Employees; -> Option AQuick Check:
View excludes Salary to secure sensitive data [OK]
- Including Salary column accidentally
- Using WHERE Salary IS NULL which filters rows, not columns
- Selecting too few columns missing Department
