Views in SQL

A view in SQL is a virtual table that is based on the result of a query.
It does not store data itself, but rather displays data stored in one or more tables.

You can think of a view as a saved query that you can treat like a table.


🔹 Why use Views?

  • To simplify complex queries.
  • To provide security by restricting access to specific columns or rows.
  • To present data in a specific format without changing the underlying tables.

✅ Example

Suppose we have a table called Employees:

EmpIDNameDepartmentSalaryCity
1AliceHR4000New York
2BobIT6000Chicago
3CarolIT7000Dallas
4DavidFinance5000New York

1. Creating a View

CREATE VIEW IT_Employees AS
SELECT EmpID, Name, Salary
FROM Employees
WHERE Department = 'IT';

2. Using the View

SELECT * FROM IT_Employees;

Result:

EmpIDNameSalary
2Bob6000
3Carol7000

3. Updating Data Through a View

If the view is updatable (depends on DBMS and query), you can modify the underlying table:

UPDATE IT_Employees
SET Salary = 7500
WHERE EmpID = 2;

This will update Bob’s salary in the original Employees table.

Leave a Reply