Category: sql
what_is_views_in_db
Published on 22 May 2026
Explanation
A View in a database is
a virtual table created from a
SQL query. It does not store
data separately. Instead, it shows data
from one or more tables whenever
the view is queried.
Code:
CREATE VIEW employee_view AS SELECT id, name, salary FROM employees;
Explanation
Views help simplify complex queries. Instead
of writing long JOIN queries repeatedly,
developers can query the view directly.
Code:
SELECT * FROM employee_view;
Explanation
Views can improve security by exposing
only selected columns to users while
hiding sensitive data.
Code:
CREATE VIEW public_employee_view AS SELECT id, name FROM employees;
Explanation
Views can combine data from multiple
tables using JOIN operations.
Code:
CREATE VIEW employee_department_view AS SELECT e.name, d.department_name FROM employees e JOIN departments d ON e.department_id = d.id;
Explanation
Updating data through a view is
possible in some databases if the
view is simple and based on
a single table.
Code:
UPDATE employee_view SET salary = 50000 WHERE id = 1;
Explanation
Views do not physically store data
like tables. Every time the view
is accessed, the database executes the
underlying SQL query.
Code:
SELECT * FROM employee_view;
Explanation
Materialized Views are special views that
store data physically for faster
performance.
Code:
CREATE MATERIALIZED VIEW sales_summary AS SELECT product_id, SUM(amount) AS total_sales FROM sales GROUP BY product_id;
Explanation
Views can be deleted when no
longer needed.
Code:
DROP VIEW employee_view;