DBMS Topics
Views in SQL
Last Updated : 21 May, 2026
A view is a virtual table defined by a stored SQL query. It does not physically store data — instead, it dynamically retrieves data from the underlying base tables whenev
What is a View?
A view is a virtual table defined by a stored SQL query. It does not physically store data — instead, it dynamically retrieves data from the underlying base tables whenever it is queried.
Creating Views
-- Basic view
CREATE VIEW CS_Employees AS
SELECT emp_id, name, salary
FROM Employee
WHERE dept_id = (SELECT dept_id FROM Department WHERE dept_name = 'Computer Science');
-- View with join
CREATE VIEW EmployeeDetails AS
SELECT e.emp_id, e.name, e.salary, d.dept_name, d.location
FROM Employee e
JOIN Department d ON e.dept_id = d.dept_id;
-- View with aggregate
CREATE VIEW DeptSalaryStats AS
SELECT d.dept_name,
COUNT(e.emp_id) AS emp_count,
AVG(e.salary) AS avg_salary,
MAX(e.salary) AS max_salary
FROM Department d
LEFT JOIN Employee e ON d.dept_id = e.dept_id
GROUP BY d.dept_name;
-- View with alias columns
CREATE VIEW SalarySummary (department, headcount, average_pay) AS
SELECT dept_id, COUNT(*), AVG(salary)
FROM Employee
GROUP BY dept_id;Using Views
Once created, a view is used exactly like a table:
-- Query a view
SELECT * FROM EmployeeDetails WHERE dept_name = 'Mathematics';
-- Filter and sort
SELECT name, salary FROM CS_Employees ORDER BY salary DESC;
-- Join a view with a table
SELECT v.name, v.salary, p.project_title
FROM CS_Employees v
JOIN Works_On w ON v.emp_id = w.emp_id
JOIN Project p ON w.proj_id = p.proj_id;Modifying and Dropping Views
-- Redefine a view (replace existing)
CREATE OR REPLACE VIEW CS_Employees AS
SELECT emp_id, name, salary, hire_date
FROM Employee
WHERE dept_id = 1;
-- Drop a view
DROP VIEW CS_Employees;
DROP VIEW IF EXISTS EmployeeDetails;Updatable Views
Some views can be used to INSERT, UPDATE, or DELETE data, which propagates to the base table.
A view is updatable if:
- Based on a single table (no joins)
- No GROUP BY, HAVING, DISTINCT, aggregate functions
- No subqueries in SELECT list
- Includes the primary key of the base table
-- Updatable view
CREATE VIEW CS_Staff AS
SELECT emp_id, name, salary FROM Employee WHERE dept_id = 1;
-- This UPDATE affects the Employee base table
UPDATE CS_Staff SET salary = 85000 WHERE emp_id = 101;
-- WITH CHECK OPTION ensures updates/inserts satisfy the view's WHERE
CREATE VIEW CS_Staff AS
SELECT emp_id, name, salary FROM Employee WHERE dept_id = 1
WITH CHECK OPTION;
-- Now: INSERT INTO CS_Staff ... will fail if dept_id != 1Non-Updatable Views
Views are NOT updatable if they contain:
- JOIN across multiple tables
- GROUP BY or HAVING
- DISTINCT
- Aggregate functions (SUM, COUNT, AVG, etc.)
- UNION
- Subqueries
Advantages of Views
| Advantage | Explanation |
|---|---|
| Security | Hide sensitive columns (salary, SSN) from certain users |
| Simplicity | Encapsulate complex joins/aggregations into a simple name |
| Data independence | Applications use views; underlying table changes don't break apps |
| Reusability | Define complex logic once; reuse in multiple queries |
| Logical data independence | Rename or restructure tables; keep views the same |
Views vs. Materialized Views
| Feature | View (Standard) | Materialized View |
|---|---|---|
| Data storage | Not stored | Physically stored on disk |
| Freshness | Always current (live query) | Stale until refreshed |
| Performance | May be slow (runs query every time) | Fast (pre-computed) |
| Refresh | Automatic (real-time) | Manual or scheduled |
| Storage cost | None | Disk space required |
| Support | All RDBMS | Oracle, PostgreSQL (not standard MySQL) |
-- Materialized view (PostgreSQL)
CREATE MATERIALIZED VIEW dept_stats AS
SELECT dept_id, COUNT(*), AVG(salary)
FROM Employee
GROUP BY dept_id;
-- Refresh when base data changes
REFRESH MATERIALIZED VIEW dept_stats;View Diagram
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Views in SQL.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this DBMS topic.
Search Terms
dbms, database management system, database notes, sql, unit, views, views in sql
Related DBMS Topics