Views
11. Views
Section titled “11. Views”What is a View?
Section titled “What is a View?”A view is a virtual table based on a SQL query. It doesn’t store data — it’s a saved query.
-- Create viewCREATE VIEW employee_summary ASSELECT e.id, CONCAT(e.first_name, ' ', e.last_name) AS full_name, d.dept_name, e.salary, CASE WHEN e.salary >= 90000 THEN 'Senior' WHEN e.salary >= 70000 THEN 'Mid-level' ELSE 'Junior' END AS levelFROM employees eJOIN departments d ON e.dept_id = d.dept_id;
-- Use view like a tableSELECT * FROM employee_summary WHERE dept_name = 'Engineering';SELECT dept_name, AVG(salary) FROM employee_summary GROUP BY dept_name;
-- Update viewCREATE OR REPLACE VIEW employee_summary AS ...;
-- Drop viewDROP VIEW employee_summary;Updatable vs Non-Updatable Views
Section titled “Updatable vs Non-Updatable Views”A view is updatable (INSERT/UPDATE/DELETE allowed) if it:
- Doesn’t use
DISTINCT,GROUP BY,HAVING,UNION - Doesn’t use aggregate functions
- References only one table
-- WITH CHECK OPTION — prevents INSERT/UPDATE that would make row invisibleCREATE VIEW high_earners ASSELECT * FROM employees WHERE salary > 80000WITH CHECK OPTION;
UPDATE high_earners SET salary = 50000 WHERE id = 1;-- ❌ Error: fails CHECK OPTION (row would no longer be visible in view)Use Cases for Views
Section titled “Use Cases for Views”- Security: expose only certain columns to users
- Simplicity: hide complex joins from application layer
- Compatibility: rename/reshape tables without changing apps