A View is a virtual table created using a SQL SELECT statement.
It does not store data physically; instead, it stores the query definition and displays data dynamically from base tables.
Whenever a view is queried, the database executes the underlying SQL query.
CREATE VIEW employee_summary AS
SELECT
employee_id,
CONCAT(first_name, ' ', last_name) AS full_name,
department,
salary
FROM employees;
SELECT * FROM employee_summary
WHERE salary > 50000;
CREATE VIEW employee_details AS
SELECT
e.employee_id,
e.first_name,
e.last_name,
d.department_name,
d.location,
e.salary
FROM employees e
INNER JOIN departments d
ON e.department = d.department_name;
CREATE VIEW department_stats AS
SELECT
department,
COUNT(*) AS employee_count,
AVG(salary) AS avg_salary,
MIN(salary) AS min_salary,
MAX(salary) AS max_salary,
SUM(salary) AS total_payroll
FROM employees
GROUP BY department;
SELECT *
FROM department_stats
WHERE employee_count > 10;
CREATE VIEW high_earners AS
SELECT employee_id, first_name, last_name, salary
FROM employees
WHERE salary > 70000;
UPDATE high_earners
SET salary = salary * 1.05
WHERE employee_id = 101;
A view is updatable only if it:
GROUP BYDISTINCTUNIONEnsures updated data still satisfies view condition.
CREATE VIEW high_earners AS
SELECT *
FROM employees
WHERE salary > 70000
WITH CHECK OPTION;
CREATE OR REPLACE VIEW employee_summary AS
SELECT
employee_id,
CONCAT(first_name, ' ', last_name) AS full_name,
department,
salary,
hire_date
FROM employees;
DROP VIEW employee_summary;
SHOW CREATE VIEW employee_summary;
or
SELECT definition
FROM information_schema.views
WHERE table_name = 'employee_summary';
| Feature | Table | View |
|---|---|---|
| Stores data | Yes | No |
| Takes storage | Yes | Minimal |
| Always updated | Depends | Yes |
| Can be indexed | Yes | No (mostly) |
| Can simplify queries | ❌ | ✅ |
| Aspect | View |
|---|---|
| Reusable | Yes |
| Stored logic | Yes |
| Parameter support | No |
| Performance | Depends on query |
GRANT SELECT ON employee_summary TO hr_user;