Definition
A view is a virtual table based on the result of a
SELECT query.Features
- Does not store data; it retrieves data from underlying tables.
- Simplifies complex queries.
Syntax
CREATE VIEW view_name AS
SELECT columns
FROM table_name
WHERE condition;
Example
Create a view to show high-salaried employees:
CREATE VIEW HighSalaryEmployees AS
SELECT name, salary
FROM employees
WHERE salary > 100000;
Use the view:
SELECT * FROM HighSalaryEmployees;
Interview Questions
1. What is the difference between a table and a view?
- A table stores data physically; a view does not.
2. Can you update data through a view?
- Yes, if the view is updatable (no joins, no aggregate functions, etc.).
3. What are the advantages of using views?
- Simplifies complex queries, enhances security, and provides abstraction.